Python 如何在 tkinter 中定位按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41574168/
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
How to I position buttons in tkinter?
提问by brenda
I have a program here that has two buttons in it. I am trying to change the position of these buttons so that there is a space between them, because currently they are directly below each other. what should I do to change the position of them?
我这里有一个程序,里面有两个按钮。我试图改变这些按钮的位置,以便它们之间有一个空间,因为目前它们直接位于彼此的下方。我该怎么做才能改变它们的位置?
def menu():
import tkinter
window=tkinter.Tk()
window.title('Lightning Parties')
window.configure(background='turquoise')
lbl=tkinter.Label(window, text='Welcome to Lightning Parties!', fg='purple', bg='turquoise', font=('comicsans', 14))
lbl.pack()
#here are the buttons
lbl=tkinter.Button(window, text='Returning customer options', fg='white', bg='purple', font=('comicsans', 12),command=combine_funcs(window.destroy, customer_login))
lbl.pack()
lbl=tkinter.Button(window, text='Register as a customer', fg='white', bg='purple', font=('comicsans', 12),command=combine_funcs(window.destroy, customer_details))
lbl.pack()
any help would be gladly appreciated, thanks!
任何帮助将不胜感激,谢谢!
回答by babygame0ver
Actually in the previous semester I have also made some Tkinter application which is the project given by teacher to us. So I go to some tutorials website of Python and find three methods of placing the widgets on the output screen. The three methods are
其实上学期我也做了一些Tkinter的应用,这是老师给我们的项目。所以我访问了一些 Python 教程网站,找到了三种将小部件放置在输出屏幕上的方法。这三种方法是
1. Pack()
2. Grid()
3. Place() #the best method over Pack() and Grid()
Place() method take the coordinates in the form of the x and y. See this link for more clarification https://www.tutorialspoint.com/python/python_gui_programming.htm
Place() 方法以 x 和 y 的形式获取坐标。有关更多说明,请参阅此链接https://www.tutorialspoint.com/python/python_gui_programming.htm
https://www.tutorialspoint.com/python/tk_place.htm
https://www.tutorialspoint.com/python/tk_place.htm
See the bottom of the Page of the given link.The Place() method is defined with its proper arguments. I will prefer the Place() method over Pack() and Grid() because it works like CSS as we use in html, because it take the value of (width,height) and place your widget according to wherever you want.
请参阅给定链接的页面底部。Place() 方法是用其适当的参数定义的。与 Pack() 和 Grid() 相比,我更喜欢 Place() 方法,因为它的工作方式类似于我们在 html 中使用的 CSS,因为它采用 (width,height) 的值并根据您的需要放置小部件。
If you find your answer a thumbs up will be appreciated.
如果您找到答案,将不胜感激。
回答by Mawty
Mine goes like this:
我的是这样的:
object1 = Tk()
actionBtn = Button(object1, text="Enter", width=15, height=2, command=quit).place(x=0, y=0)
#.place() is the best thing to use, x and y determine the location in terms of geometry.
you can even add an image with .png extension and goes like this:
您甚至可以添加带有 .png 扩展名的图像,如下所示:
buttonEnter = PhotoImage(file="buttonEnter.png") #image file must be inserted
buttonEnter1 = Button(object1, image=buttonEnter, width=20, height=4).place(x=0, y=0)
回答by j_4321
To increase the vertical space between the buttons, use pady
argument in the pack
method: lbl.pack(pady=10)
.
要增加按钮之间的垂直空间,请pady
在pack
方法中使用参数:lbl.pack(pady=10)
。
For horizontal spacing, there is a padx
argument. This also works with the grid
method.
对于水平间距,有一个padx
说法。这也适用于该grid
方法。
回答by EriktheRed
I would comment on other answers, but don't have the rep.
The answers suggesting the pad
methods might not be quite what you want. pad
creates extra space between the border of the button and its contents, so using it will just make the button itself bigger.
I may be wrong, but I don't think this will affect the spacing between the widgets themselves.
我会评论其他答案,但没有代表。
建议这些pad
方法的答案可能不是您想要的。pad
在按钮的边框与其内容之间创建额外的空间,因此使用它只会使按钮本身更大。
我可能是错的,但我认为这不会影响小部件本身之间的间距。