我的代码使用垂直滚动框架(来自here)。目前“名称:Ryan”,并且输入框未在同一行中对齐。我想对齐输入框和标签,使它们在同一列上,我用pack()
方法搞砸了,但我无法修复它。
if __name__ == "__main__":
class SampleApp(Tk):
def __init__(self, *args, **kwargs):
root = Tk.__init__(self, *args, **kwargs)
self.label = Label(text="Choose the info to exclude (if any) on the \n left."
"Write the number of the tags that should \n be associated with the information on the right.")
self.label.pack()
self.frame = VerticalScrolledFrame(root)
self.frame.pack(side=LEFT)
self.frame2=VerticalScrolledFrame(root)
self.frame2.pack()
buttons = []
resource=[]
for i in range(10):
buttons.append(Checkbutton(self.frame.interior, text=str(i)+". "+ "Button" ))
buttons[-1].pack()
label=[]
for i in range(10):
resource.append(Entry(self.frame2.interior, width=3))
label.append(Label(self.frame2.interior,text="Name: Ryan"))
label[-1].pack()
resource[-1].pack()
app = SampleApp()
app.mainloop()
输出:
答案 0 :(得分:2)
如果您想在网格中进行布局,最好选择使用grid
而不是pack
。
例如:
self.frame2.interior.grid_columnconfigure(1, weight=1)
for i in range(10):
resource.append(Entry(self.frame2.interior, width=3))
label.append(Label(self.frame2.interior,text="Name: Ryan"))
label[-1].grid(row=i, column=0, sticky="e")
resource[-1].grid(row=i, column=1, sticky="ew")
答案 1 :(得分:1)
尝试创建用于保存每一行的中间帧,如下所示:
class SampleApp(Tk):
def __init__(self, *args, **kwargs):
root = Tk.__init__(self, *args, **kwargs)
self.label = Label(text="Choose the info to exclude (if any) on the \n left."
"Write the number of the tags that should \n be associated with the information on the right.")
self.label.pack()
self.frame = VerticalScrolledFrame(root)
self.frame.pack()
buttons = []
resource=[]
label=[]
for i in range(10):
frame = Frame(self.frame.interior)
frame.pack(side=TOP)
buttons.append(Checkbutton(frame, text=str(i)+". "+ "Button" ))
resource.append(Entry(frame, width=3))
label.append(Label(frame,text="Name: Ryan"))
buttons[-1].pack(side=LEFT)
label[-1].pack(side=LEFT)
resource[-1].pack(side=LEFT)
使用side=TOP
将帧堆叠到一列中,并使用side=LEFT
将每个帧的内容排列成一行。