Python Tkinter:如何让按钮居中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31128780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tkinter: How to make a button center itself?
提问by It's Willem
I'm making a program in Python and I want to go with a layout that is a bunch of buttons in the center. How do I make a button center itself using pack()?
我正在用 Python 编写一个程序,我想使用一个在中心有一堆按钮的布局。如何使用 pack() 使按钮居中?
采纳答案by Adolfo Correa
If this can't resolve your problem
如果这不能解决您的问题
button.pack(side=TOP)
You'll need to use the method
您将需要使用该方法
button.grid(row=1,col=0)
the values of row=1,col=0
depend of the position of the other widget in your window
值row=1,col=0
取决于窗口中其他小部件的位置
or you can use .place(relx=0.5, rely=0.5, anchor=CENTER)
或者你可以使用 .place(relx=0.5, rely=0.5, anchor=CENTER)
button.place(relx=0.5, rely=0.5, anchor=CENTER)
Example using .place()
:
使用示例.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()