简体   繁体   English

如何堆叠按钮,而不是在Tkinter中将它们排队?

[英]How can I stack buttons rather than queue them in Tkinter?

At the moment I have 3 basic buttons being displayed: 目前,我正在显示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()

The 3 buttons at the moment will stick to the left frame on the window, however then will queue next to each other rather than stack one on top of the other, how do i fix this? 此刻的3个按钮将停留在窗口的左框架上,但是随后将排成一行,而不是一个接一个地堆叠,我该如何解决?

Explore the grid function. 探索grid功能。 Change your pack statements to 将您的pack声明更改为

button1.grid(row=0,column=0)
button2.grid(row=1,column=0)
button3.grid(row=2,column=0)

You're explicitly telling them to be side-by-side with side=LEFT . 您明确地告诉他们与side=LEFT You want side=TOP so that they are placed at the top of the empty space in the frame. 您需要side=TOP以便将它们放置在框架中空白区域的顶部。

button1.pack(side=TOP)
button2.pack(side=TOP)
button3.pack(side=TOP)

When you use pack, the values TOP, LEFT, RIGHT and BOTTOM tell the widget which side of the remaining space they should occupy. 使用pack时,值TOP,LEFT,RIGHT和BOTTOM会告诉小部件它们应占据剩余空间的哪一侧。 The first time you use LEFT it will reserve the left side of the whole frame for the widget. 第一次使用LEFT时,它将为小部件保留整个框架的左侧。 The next time you use LEFT, that refers to the space remaining in the widget excluding the left edge since that already has a widget in it. 下次使用LEFT时,是指小部件中的剩余空间(左边缘除外) ,因为该空间中已经有小部件。 The net effect is that LEFT causes widgets to be arranged left-to-right, RIGHT arranges them right-to-left, and so on. 最终效果是,LEFT导致小部件从左到右排列,RIGHT使它们从右到左排列,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM