Python 使用Tkinter将按钮与底部对齐

Python 使用Tkinter将按钮与底部对齐,python,tkinter,Python,Tkinter,有人能帮我在Tkinter中将按钮与屏幕底部对齐吗。我正在关注youtube教程,他们在代码中编写了我所写的内容,但它将按钮与底部对齐。我正在mac上使用python 3.7 from tkinter import * root = Tk() #makes a blank popup, under the variable name 'root' topFrame = Frame(root) topFrame.pack() bottomFrame = Frame(root) bottomFr

有人能帮我在Tkinter中将按钮与屏幕底部对齐吗。我正在关注youtube教程,他们在代码中编写了我所写的内容,但它将按钮与底部对齐。我正在mac上使用python 3.7

from tkinter import *

root = Tk() #makes a blank popup, under the variable name 'root'

topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM)

button1 = Button(topFrame, text='Button 1', fg='red')
button2 = Button(topFrame, text='Button 2', fg='blue')
button3 = Button(topFrame, text='Button 3', fg='green')
button4 = Button(topFrame, text='Button 4', fg='pink')

button1.pack(side=LEFT)
button2.pack(side=LEFT)
button3.pack(side=LEFT)
button4.pack(side=BOTTOM)

root.mainloop() #loops the program forever until its closed

我认为您应该在底部框架中添加
按钮4

button4 = Button(bottomFrame, text='Button 4', fg='pink')

我认为您应该在底部框架中添加
按钮4

button4 = Button(bottomFrame, text='Button 4', fg='pink')

我建议您使用
grid()
而不是
pack()
,它允许定位更加可控

grid()
方法创建一种带有行和列的表,允许您定位元素

以下是我心目中的布局:

 -----
|A|B|C|
 -----
| |D| |
 -----
  • A:第1行第0列
  • B:第1行第1列
  • C:第1行第2列
  • D:第2行第1列
如果这不是你想要的元素,请编辑你的帖子或评论,我会相应地编辑我的答案


记住,我们可以将
.pack()
更改为以下方法:

from tkinter import *

root = Tk() #makes a blank popup, under the variable name 'root'

topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM)

button1 = Button(topFrame, text='Button 1', fg='red')
button2 = Button(topFrame, text='Button 2', fg='blue')
button3 = Button(topFrame, text='Button 3', fg='green')
button4 = Button(topFrame, text='Button 4', fg='pink')

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

root.mainloop() #loops the program forever until its closed
pack()


我还建议不要使用tkinter import*
中的
,这是不安全的,可能会覆盖函数,并且在某些时候很可能会给您带来问题。

我建议您使用
grid()
而不是
pack()
,它允许定位更加可控

grid()
方法创建一种带有行和列的表,允许您定位元素

以下是我心目中的布局:

 -----
|A|B|C|
 -----
| |D| |
 -----
  • A:第1行第0列
  • B:第1行第1列
  • C:第1行第2列
  • D:第2行第1列
如果这不是你想要的元素,请编辑你的帖子或评论,我会相应地编辑我的答案


记住,我们可以将
.pack()
更改为以下方法:

from tkinter import *

root = Tk() #makes a blank popup, under the variable name 'root'

topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM)

button1 = Button(topFrame, text='Button 1', fg='red')
button2 = Button(topFrame, text='Button 2', fg='blue')
button3 = Button(topFrame, text='Button 3', fg='green')
button4 = Button(topFrame, text='Button 4', fg='pink')

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

root.mainloop() #loops the program forever until its closed
pack()


我还建议不要使用tkinter import*
中的
,这是不安全的,可能会覆盖功能,并且在某些情况下很可能会导致问题。

在大多数情况下,这可能被认为是不切实际的,但一种方法是简单地交换以下行:

button1.pack(side=LEFT)
与:


这将使
按钮4
填充“空腔”中的第一个空白,而不是最后一个。这在大多数情况下可能被认为是不切实际的,但一种方法是简单地交换以下行:

button1.pack(side=LEFT)
与:


这将使
按钮4
填充“空腔”中的第一个空白,而不是最后一个。进一步解释。

您期望的结果是什么?我将四个按钮水平对齐,从左到右。您希望它们垂直对齐吗?您希望底部的按钮应该在
bottomFrame
中,还是应该在
bottomFrame
的上方或下方?您期望的结果是什么?我将四个按钮水平对齐,从左到右。是否希望它们垂直对齐?您希望底部的按钮应该在
底部框架中,还是应该在
底部框架的上方或下方
?独立于首选几何图形管理器,,我一直认为应该将按钮4添加到底部框架。tkinter import*
如何不保证线程安全?@Davideb你说得很对,这就解决了问题。我只是列出了解决问题的另一种方法。@right leg tkinter import*
中我的错误
是不安全的,可能会覆盖函数,tkinter不是线程安全的。独立于首选的几何体管理器,我一直认为应该将按钮4添加到底部框架。tkinter import*
不是线程安全的吗?@Davideb你说得很对,解决了这个问题。我只是列出了解决问题的另一种方法。@right leg tkinter import*
中我的错误
是不安全的,可能会覆盖函数,tkinter不是线程安全的。