我使用Python解析日志文件中的条目,并使用Tkinter显示条目内容,到目前为止,它非常出色。输出是标签小部件的网格,但有时显示的行数比屏幕上的要多。我想添加一个滚动条,看起来应该很简单,但是我想不出来。文档意味着只有列表、文本框、画布和条目小部件支持滚动条接口。所有这些似乎都不适合显示小部件网格。在画布小部件中放置任意的小部件是可能的,但是您似乎必须使用绝对的协调,这样我就不能使用网格布局管理器了?我尝试过将小部件网格放到一个框架中,但这似乎不支持滚动条接口,所以这是行不通的:mainframe = Frame(root, yscrollcommand=scrollbar.set)有人能提出解决这个限制的办法吗?我讨厌用PyQt重写,并将我的可执行映像大小增加这么多,只是为了添加一个滚动条!在Tkinter中向一组小部件添加滚动条
2 回答
慕斯王
TA贡献1864条经验 获得超2个赞
概述
Frame
scrollregion
row*(lineheight+spacing)
面向对象的解决方案
import tkinter as tkclass Example(tk.Frame): def __init__(self, root): tk.Frame.__init__(self, root) self.canvas = tk.Canvas(root, borderwidth=0, background="#ffffff") self.frame = tk.Frame(self.canvas, background="#ffffff") self.vsb = tk.Scrollbar(root, 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): '''Put in some fake data''' 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): '''Reset the scroll region to encompass the inner frame''' self.canvas.configure(scrollregion=self.canvas.bbox("all"))if __name__ == "__main__": root=tk.Tk() Example(root).pack(side="top", fill="both", expand=True) root.mainloop()
程序解决方案
import tkinter as tkdef populate(frame): '''Put in some fake data''' 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): '''Reset the scroll region to encompass the inner frame''' 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
tkinter
添加回答
举报
0/150
提交
取消