PythonTkinter布局管理

PythonTkinter布局管理,python,layout,tkinter,Python,Layout,Tkinter,我在下面附上了两张图片,显示了我当前获得的布局以及我想要在GUI顶部框架中的布局,忽略中间框架和底部框架。为了使代码尽可能简短,我没有将其余的代码包含在内。 左边的图片=我得到的布局。右边的图片=我需要的布局 from Tkinter import * root= Tk() topFrame = Frame(root,bg="grey") topFrame.pack() midFrame = Frame(root,bg="lightblue",borderwidth=2,relief=GRO

我在下面附上了两张图片,显示了我当前获得的布局以及我想要在GUI顶部框架中的布局,忽略中间框架和底部框架。为了使代码尽可能简短,我没有将其余的代码包含在内。

左边的图片=我得到的布局。右边的图片=我需要的布局

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()

通过将GUI划分为有意义的部分并为每个部分创建一个框架,可以更容易地在tkinter中对齐小部件。您可以重复此操作,直到对齐在每个子帧中都是直接的

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()