Python 3.x Tkinter-如何使小部件居中(Python)

Python 3.x Tkinter-如何使小部件居中(Python),python-3.x,tkinter,tkinter-layout,Python 3.x,Tkinter,Tkinter Layout,我从Python中的GUI开始,遇到了一个问题。 我已经在我的框架中添加了小部件,但它们总是在左侧。 我试过一些互联网上的例子,但我没有做到。 我尝试了.place,但它对我不起作用。有人能告诉我如何把部件放在中间吗?< /P> 代码: 创建窗口后,添加: window.columnconfigure(0, weight=1) 有关详细信息,请在创建窗口后添加: window.columnconfigure(0, weight=1) 更多信息参见您正在混合使用两种不同的布局管理器。我建议您使

我从Python中的GUI开始,遇到了一个问题。 我已经在我的框架中添加了小部件,但它们总是在左侧。 我试过一些互联网上的例子,但我没有做到。 我尝试了
.place
,但它对我不起作用。有人能告诉我如何把部件放在中间吗?< /P> 代码:


创建
窗口后,添加:

window.columnconfigure(0, weight=1)

有关详细信息,请在创建
窗口后添加:

window.columnconfigure(0, weight=1)

更多信息参见

您正在混合使用两种不同的布局管理器。我建议您使用

一旦您决定了要使用哪一个,就更容易帮助您:)

例如,您可以使用两行两列的网格几何体管理器,并按如下方式放置小部件:

label1 = Label(start_page, text='Welcome to the Assistant')
# we place the label in the page as the fist element on the very left 
# and allow it to span over two columns
label1.grid(row=0, column=0, sticky='w', columnspan=2) 

button1 = Button(start_page, text='Button1', command=self.button_clicked)
button1.grid(row=1, column=0)

button2 = Button(start_page, text='Button2', command=self.button_clicked)
button2.grid(row=1, column=1)

这将导致标签位于第一行,并且在两个按钮的下方相邻。

您混合了两个不同的布局管理器。我建议您使用

一旦您决定了要使用哪一个,就更容易帮助您:)

例如,您可以使用两行两列的网格几何体管理器,并按如下方式放置小部件:

label1 = Label(start_page, text='Welcome to the Assistant')
# we place the label in the page as the fist element on the very left 
# and allow it to span over two columns
label1.grid(row=0, column=0, sticky='w', columnspan=2) 

button1 = Button(start_page, text='Button1', command=self.button_clicked)
button1.grid(row=1, column=0)

button2 = Button(start_page, text='Button2', command=self.button_clicked)
button2.grid(row=1, column=1)

这将导致标签位于第一行,并且在两个按钮的下方相邻。

您希望哪个小部件居中,您希望它们从左到右居中还是从上到下居中,或者两者都居中?还有其他小部件吗?没有一种方法可以将小部件居中,因为一个小部件的位置会影响另一个小部件的位置。我只想将第一个标签居中,从左到右居中,您希望哪个小部件居中,您希望它们从左到右居中还是从上到下居中,或者两者都居中?还有其他小部件吗?没有一种方法可以使小部件居中,因为一个小部件的位置会影响另一个小部件的位置