I am searching for the syntax that will allow me to arrange widget buttons in a Tkinter GUI in a specific manner; currently I have three (3) buttons that are stacked vertically on top of one another. I wish to arrange the buttons so that they form a row of three(3) horizontally, instead.

Thank you in advance for any help offered.

Matty D

Recommended Answers

All 9 Replies

Best to use a grid ...

# using Tkinter's grid() to place widgets
import Tkinter as tk
root = tk.Tk()
# create some buttons
button1 = tk.Button(root, text='button1')
button2 = tk.Button(root, text='button2')
button3 = tk.Button(root, text='button3')
# create a label for kicks
label1 = tk.Label(root, text="hello Matty!", fg='red', bg='yellow')
# use a grid to place the buttons
# stay in the same row and add columns
button1.grid(row=0, column=0)
button2.grid(row=0, column=1)
button3.grid(row=0, column=2)
# place the label in the next row
# span across 3 columns, pady (vertical), stretch label horizontally
label1.grid(row=1, column=0, columnspan=3, pady=5, sticky=tk.E+tk.W)
# run the event loop
root.mainloop()
commented: always helpful \ matty d +2

Best to use a grid ...

# using Tkinter's grid() to place widgets
import Tkinter as tk
root = tk.Tk()
# create some buttons
button1 = tk.Button(root, text='button1')
button2 = tk.Button(root, text='button2')
button3 = tk.Button(root, text='button3')
# create a label for kicks
label1 = tk.Label(root, text="hello Matty!", fg='red', bg='yellow')
# use a grid to place the buttons
# stay in the same row and add columns
button1.grid(row=0, column=0)
button2.grid(row=0, column=1)
button3.grid(row=0, column=2)
# place the label in the next row
# span across 3 columns, pady (vertical), stretch label horizontally
label1.grid(row=1, column=0, columnspan=3, pady=5, sticky=tk.E+tk.W)
# run the event loop
root.mainloop()

Thank-you vegaseat :)
Matty

Glad to see you back!

BTW, pygtk is okay for a GUI toolkit, but more at home on a Linux machine. The Windows installation of GTK and pygtk is a little messy and convoluted.

Vegaseat:

Hi. I did not end up using that (pygtk), instead going with:

btn1.pack(side=LEFT, padx=5)

This seems to work great-- very simple and all I need. I will, in the future, use "grid" as you suggested earlier. Thanks for your help.

Matty

Okay, pack() will do just fine. However, most programmers prefer Tkinter's grid() layout manager over pack() or place(). Here is a little helper file for pack() I wrote some time ago ...

# looking at the Tkinter pack() layout manager
import Tkinter as tk
root = tk.Tk()
b1 = tk.Button(root, text="Button1")
b2 = tk.Button(root, text="Button2")
b3 = tk.Button(root, text="Button3")
b4 = tk.Button(root, text="Button4")
b5 = tk.Button(root, text="Button5")
b6 = tk.Button(root, text="Button6")
b7 = tk.Button(root, text="Button7")
# line widgets up from top down
b1.pack(side=tk.TOP)
# can also use 'top' and expand in horizontal x direction
b2.pack(side='top', fill='x', expand='yes')  
# line widgets up from left to right
b3.pack(side=tk.LEFT)
# can also use 'left' and pad in x and y directions
b4.pack(side='left', padx=10, pady=30)  
# these will line up on top of each other left of button4
b5.pack()
b6.pack()
b7.pack()
# dictionary of pack info for button2
print b2.pack_info()
root.mainloop()

Okay, pack() will do just fine. However, most programmers prefer Tkinter's grid() layout manager over pack() or place(). Here is a little helper file for pack() I wrote some time ago ...

# looking at the Tkinter pack() layout manager
import Tkinter as tk
root = tk.Tk()
b1 = tk.Button(root, text="Button1")
b2 = tk.Button(root, text="Button2")
b3 = tk.Button(root, text="Button3")
b4 = tk.Button(root, text="Button4")
b5 = tk.Button(root, text="Button5")
b6 = tk.Button(root, text="Button6")
b7 = tk.Button(root, text="Button7")
# line widgets up from top down
b1.pack(side=tk.TOP)
# can also use 'top' and expand in horizontal x direction
b2.pack(side='top', fill='x', expand='yes')  
# line widgets up from left to right
b3.pack(side=tk.LEFT)
# can also use 'left' and pad in x and y directions
b4.pack(side='left', padx=10, pady=30)  
# these will line up on top of each other left of button4
b5.pack()
b6.pack()
b7.pack()
# dictionary of pack info for button2
print b2.pack_info()
root.mainloop()

vegaseat:
Why is grid () preferred over pack () and place()? Just curious. ;)

Thanks,
Matty

When you have lots of widgets on a form and particularly if they are of different size, then the grid layout is easier to accomplish. Also changes are easier to make without everything collapsing.

Cool. Thank-you for the explanation. It sounds like the best way to go in Tkinter when dealing with various\multiple widgets.

Matty

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.