我正在尝试创建不同的框架并切换/销毁它们,以便您可以像在普通 iOS 应用程序中那样在窗口之间移动。
为此,我需要将小部件(组件)放置在框架(容器)中。
但是,当我尝试向框架添加按钮时,它不会将其打包到右侧。
这是我的代码:
从 tkinter 导入 *
root=Tk()
root.geometry('500x500')
root.title('Good morning :)')
frame1=Frame(root,width=500,height=500,bg='green')
frame1.pack()
button1=Button(frame1,text='Hello')
button1.pack(side='bottom')
最佳答案
您需要展开 Frame 以填充整个顶层窗口,并且您需要告诉 Button 打包在 side='right'
上。而不是 side='bottom'
.
你需要运行 root.mainloop()
在末尾。
from tkinter import *
root = Tk()
root.geometry('500x500')
root.title('Good morning :)')
frame1 = Frame(root, bg='green')
frame1.pack(expand=True, fill=BOTH)
button1 = Button(frame1, text='Hello')
button1.pack(side=RIGHT)
root.mainloop()
此外,您不需要 Frame 语句中的尺寸,因为它将扩展到几何中规定的完整 500x500,并将额外的关键字参数传递给 pack() 函数。默认情况下,Frame 的大小仅足以容纳其中的小部件,因此它只会与 Button 一样大,除非您告诉它扩展到顶级根小部件的完整大小。
关于button - Python Tkinter : Placing buttons in frame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47423318/