[英]How to "stack" buttons when deleting one of them in a row? Python, Tkinter
[英]How can I stack buttons rather than queue them in Tkinter?
目前,我正在显示3个基本按钮:
from tkinter import *
root = Tk()
topFrame = Frame(root)
topFrame.pack(side=TOP)
leftFrame = Frame(root)
leftFrame.pack(side=LEFT)
botFrame = Frame(root)
botFrame.pack(side=BOTTOM)
button1 = Button(leftFrame, text="Button 1", fg="Black")
button2 = Button(leftFrame, text="Button 2", fg="Black")
button3 = Button(leftFrame, text="Button 3", fg="Black")
button1.pack(side=LEFT)
button2.pack(side=LEFT)
button3.pack(side=LEFT)
root.mainloop()
此刻的3个按钮将停留在窗口的左框架上,但是随后将排成一行,而不是一个接一个地堆叠,我该如何解决?
探索grid
功能。 将您的pack
声明更改为
button1.grid(row=0,column=0)
button2.grid(row=1,column=0)
button3.grid(row=2,column=0)
您明确地告诉他们与side=LEFT
。 您需要side=TOP
以便将它们放置在框架中空白区域的顶部。
button1.pack(side=TOP)
button2.pack(side=TOP)
button3.pack(side=TOP)
使用pack时,值TOP,LEFT,RIGHT和BOTTOM会告诉小部件它们应占据剩余空间的哪一侧。 第一次使用LEFT时,它将为小部件保留整个框架的左侧。 下次使用LEFT时,是指小部件中的剩余空间(左边缘除外) ,因为该空间中已经有小部件。 最终效果是,LEFT导致小部件从左到右排列,RIGHT使它们从右到左排列,依此类推。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.