Tkinter:如何制作一个按钮中心?

It'*_*lem 2 python tkinter centering

我正在使用Python制作一个程序,我希望在中心使用一组按钮.如何使用pack()创建按钮中心?

Ado*_*rea 11

如果这不能解决您的问题

button.pack(side=TOP)
Run Code Online (Sandbox Code Playgroud)

您需要使用该方法

button.grid(row=1,col=0)
Run Code Online (Sandbox Code Playgroud)

row=1,col=0窗口中其他窗口小部件的位置依赖值

或者你可以使用 .place(relx=0.5, rely=0.5, anchor=CENTER)

button.place(relx=0.5, rely=0.5, anchor=CENTER)
Run Code Online (Sandbox Code Playgroud)

示例使用.place():

from tkinter import *  # Use this if use python 3.xx
#from Tkinter import *   # Use this if use python 2.xx
a = Button(text="Center Button")
b = Button(text="Top Left Button")
c = Button(text="Bottom Right Button")

a.place(relx=0.5, rely=0.5, anchor=CENTER)
b.place(relx=0.0, rely=0.0, anchor=NW)
c.place(relx=1.0, rely=1.0, anchor=SE)
mainloop
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 请注意,对于网格管理器,您最终可能必须使用“columnspan”或“rowspan”选项(请参见此处:http://effbot.org/tkinterbook/grid.htm),甚至可能需要使用“Grid.columnconfigure”和`Grid.rowconfigure`方法将权重从0更改为1(请参见此处:http://stackoverflow.com/questions/7591294/how-to-create-a-self-resizing-grid-of-buttons-in -tkinter/)。无论哪种情况,展示您在上面使用您提到的其他几何管理器所做的事情的示例都会有所帮助。 (2认同)