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!
While I'm not entirely sure tkinter has an explicit way to solve this problem (or, at least, solve it simply), my suggestion is to start using the
grid()
method.It requires a bit more code than
pack()
(see the effbot article for more info), but is much more versatile.Again, I don't think it will directly solve this problem, but it's more versatile, and if you structure the gui correctly, and use the
sticky
keyword properly, nice spacing is often a given.To increase the vertical space between the buttons, use
pady
argument in thepack
method:lbl.pack(pady=10)
.For horizontal spacing, there is a
padx
argument. This also works with thegrid
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.
Mine goes like this:
you can even add an image with .png extension and goes like this:
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
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.