2

I have attached two pictures down below showing the layout that I am currently getting and the layout which I want in the top frame of the GUI, ignore the midframe and bottomframe. In order to keep the code as short as possible I haven't included the rest of the code inside. Current LayoutLayout that I want

Picture on Left= Layout I am getting. Picture on right = Layout I need

from Tkinter import *

root= Tk()
topFrame = Frame(root,bg="grey")
topFrame.pack()
midFrame = Frame(root,bg="lightblue",borderwidth=2,relief=GROOVE)
midFrame.pack()
bottomFrame= Frame(root,bg="lightgreen")
bottomFrame.pack(side=BOTTOM)

label1 = Label(topFrame, text="Upload Acitivity File:")
label1.pack(padx=5, pady=10)
first_button=Button(topFrame,text="Button 1")
first_button.pack()



label2 = Label(topFrame, text="Select the Activity")
label2.pack(padx=5,pady=10,side=LEFT)

b1 = Radiobutton(topFrame, text="Walking",value=1)
b1.pack(padx=5, pady=10,side=LEFT)
b2 = Radiobutton(topFrame, text="Running",value=2)
b2.pack(padx=10, pady=10)

root.mainloop()

1 Answer 1

2

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 tk

root = 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 frame1
label1 = 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 frame2
label2 = 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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.