Tkinter 為List 等小組件提供捲軸、文字方塊、畫布和條目。但是,顯示帶有捲軸的標籤小部件網格可能具有挑戰性,因為這些小部件本身不支援捲軸。
1.使用window_create 的文字小工具:
2.有嵌入框架的畫布:
3。在畫布上直接繪圖:
import tkinter as tk class Example(tk.Frame): def __init__(self, parent): tk.Frame.__init__(self, parent) self.canvas = tk.Canvas(self, borderwidth=0, background="#ffffff") self.frame = tk.Frame(self.canvas, background="#ffffff") self.vsb = tk.Scrollbar(self, orient="vertical", command=self.canvas.yview) self.canvas.configure(yscrollcommand=self.vsb.set) self.vsb.pack(side="right", fill="y") self.canvas.pack(side="left", fill="both", expand=True) self.canvas.create_window((4,4), window=self.frame, anchor="nw", tags="self.frame") self.frame.bind("<Configure>", self.onFrameConfigure) self.populate() def populate(self): for row in range(100): tk.Label(self.frame, text="%s" % row, width=3, borderwidth="1", relief="solid").grid(row=row, column=0) t = "this is the second column for row %s" % row tk.Label(self.frame, text=t).grid(row=row, column=1) def onFrameConfigure(self, event): self.canvas.configure(scrollregion=self.canvas.bbox("all")) if __name__ == "__main__": root = tk.Tk() example = Example(root) example.pack(side="top", fill="both", expand=True) root.mainloop()
import tkinter as tk def populate(frame): for row in range(100): tk.Label(frame, text="%s" % row, width=3, borderwidth="1", relief="solid").grid(row=row, column=0) t = "this is the second column for row %s" % row tk.Label(frame, text=t).grid(row=row, column=1) def onFrameConfigure(canvas): canvas.configure(scrollregion=canvas.bbox("all")) root = tk.Tk() canvas = tk.Canvas(root, borderwidth=0, background="#ffffff") frame = tk.Frame(canvas, background="#ffffff") vsb = tk.Scrollbar(root, orient="vertical", command=canvas.yview) canvas.configure(yscrollcommand=vsb.set) vsb.pack(side="right", fill="y") canvas.pack(side="left", fill="both", expand=True) canvas.create_window((4,4), window=frame, anchor="nw") frame.bind("<Configure>", lambda event, canvas=canvas: onFrameConfigure(canvas)) populate(frame) root.mainloop()
以上是如何為 Tkinter 標籤網格新增捲軸?的詳細內容。更多資訊請關注PHP中文網其他相關文章!