Python Tkinter Layout management Python Tkinter Layout management tkinter tkinter

Python Tkinter Layout management


Aligning widgets in tkinter can be made much easier by dividing the GUI into meaningful sections and creating a frame for each. You can repeat this operation multiple times until the alignment is straightforward in each subframe.

import Tkinter as tkroot = tk.Tk()# Create two frames on top of each other (bg color can help debugging)frame1 = tk.Frame(root, bg="yellow")frame2 = tk.Frame(root, bg="blue")frame1.pack(side=tk.TOP)frame2.pack(side=tk.TOP)# Place label1 and button1 side-by-side in frame1label1 = tk.Label(frame1, text="Upload Activity File:")label1.pack(side=tk.LEFT)button1 = tk.Button(frame1,text="Button 1")button1.pack(side=tk.LEFT)# Place label2, b1 and b2 side-by-side in frame2label2 = tk.Label(frame2, text="Select the Activity")label2.pack(side=tk.LEFT)b1 = tk.Radiobutton(frame2, text="Walking", value=1)b1.pack(side=tk.LEFT)b2 = tk.Radiobutton(frame2, text="Running", value=2)b2.pack(side=tk.LEFT)root.mainloop()

Screenshot