4

I have a program here that has two buttons in it. I am trying to change their position to be a space between them as currently they are directly below each other. What should I do to change the position of buttons?

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!

Kate Orlova
  • 3,225
  • 5
  • 11
  • 35
brenda
  • 73
  • 1
  • 2
  • 9

3 Answers3

9

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.

babygame0ver
  • 447
  • 4
  • 16
  • Not sure I can agree that "place" is best. It is more of a last resort when you just can't make grid or pack do what you want! And CSS has lots of grid layout systems that are pretty useful for many websites. Using place mean you have to do all the screen size, widget size, size change calculations yours self. The flexibility of the grid system (particularly if you nest frames within frames) it likely to be less work and more responsive. One app I built for there are a pair of side by side frames packed togethre, with each containing a seperate grid. – Duke Bouvier Apr 22 '20 at 19:36
2

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) 
j_4321
  • 15,431
  • 3
  • 34
  • 61
Mawty
  • 428
  • 3
  • 10
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.

j_4321
  • 15,431
  • 3
  • 34
  • 61