【发布时间】:2022-01-22 17:52:37
【问题描述】:
我正在构建一个带有可滚动区域中标签列表的 gui。现在我希望标签通过网格管理器填充空白空间。所以我使用columnconfigure(0, weight=1) 和rowconfigure(0, weight=1) 方法。它适用于滚动条,但不适用于可滚动区域内的标签。显示我的问题的示例:
class app():
def __init__(self):
self.root = tk.Tk()
self.root.geometry("341x448")
self.root.minsize(340,440)
self.root.rowconfigure(0, weight=1)
self.root.columnconfigure(0, weight=1)
def display(self):
self.container = ttk.Frame(self.root)
self.container.rowconfigure(0, weight=1)
self.container.columnconfigure(0, weight=1)
self.canvas = tk.Canvas(self.container)
scrollbar = ttk.Scrollbar(self.container, orient = tk.VERTICAL, command = self.canvas.yview)
self.scrollable_frame = ttk.Frame(self.canvas)
self.scrollable_frame.bind(
"<Configure>",
lambda e: self.canvas.configure(
scrollregion=self.canvas.bbox("all")))
self.canvas.create_window((0, 0), window = self.scrollable_frame, anchor = "nw")
self.canvas.configure(yscrollcommand = scrollbar.set)
for i in range(15):
Label = ttk.LabelFrame(self.scrollable_frame, text = "Sample scrolling label")
Label.grid(row = i, column = 0, columnspan=2, sticky=tk.NSEW)
Label.columnconfigure(0, weight=1)
Button = ttk.Button(Label, text=f"Button {i}")
Button.grid(row=0, column=0, sticky=tk.NW)
self.container.grid(row = 0, column = 0, sticky = "nswe")
self.canvas.grid(row = 0, column = 0, sticky = 'nswe')
scrollbar.grid(row = 0, column = 2, sticky = "ns")
self.root.mainloop()
if __name__ =="__main__":
start = app()
start.display()
【问题讨论】:
标签: python-3.x tkinter scrollbar