How to I position buttons in tkinter?

倖福魔咒の 提交于 2019-12-01 23:47:34

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

 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

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.

If you find your answer a thumbs up will be appreciated.

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:

buttonEnter = PhotoImage(file="buttonEnter.png") #image file must be inserted 
buttonEnter1 = Button(object1, image=buttonEnter, width=20, height=4).place(x=0, y=0) 

To increase the vertical space between the buttons, use pady argument in the pack method: lbl.pack(pady=10).

For horizontal spacing, there is a padx argument. This also works with the grid method.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!