我正在使用放在画布上的几个小部件来显示几个任务的进度。为了做到这一点,我将它们放在框架中的画布上的网格中。几乎一切正常但我需要滚动条默认为窗口底部(框架)。我试图使用vsb.set强制滚动条到一个新位置,但它没有任何效果。如果我使用事件处理程序移动滚动条,我将失去对其移动的控制。我是新手,会很感激一些意见。请参阅下面的代码。
from tkinter import *
import tkinter as tk
class example(Frame):
def __init__(self, root):
Frame.__init__(self, root)
self.initUI()
def initUI(self):
self.canvas = Canvas(root, borderwidth=0, background="#ffffff", height=300, width=700)
self.frame = Frame(self.canvas, background="#ffffff")
self.vsb = 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)
root.title("Issue with Scrolling through Widgets")
self.populate()
def onFrameConfigure(self, event):
###Reset the scroll region to encompass the inner frame
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
self.canvas.bind_all("<MouseWheel>", self._on_mousewheel)
def _on_mousewheel(self, event):
self.canvas.yview_scroll(-1*int(event.delta/120),"units" )
def populate(self):
###Put in some fake data###
for row in range(20):
Txt = Text(self.frame, bg="white", borderwidth=1, height=1, width=40)
Txt.grid(row = row, column = 0, rowspan = 1, columnspan = 1, sticky = W+E+N+S)
line = "this is the first column for row %s" %row
Txt.insert(END, str(line))
Txt = Text(self.frame, bg="white", borderwidth=1, height=1, width=40)
Txt.grid(row = row, column = 1, rowspan = 1, columnspan = 1, sticky = W+E+N+S)
line = "this is the second column for row %s" %row
Txt.insert(END, str(line))
if __name__ == "__main__":
root=Tk()
example(root).pack(side="top", fill="both", expand=True)
root.mainloop()
答案 0 :(得分:0)
是的,我希望滚动条向下移动,因为新数据放在网格上。我找到了一个有效的解决方案,但我不确定它是否合适。我将onFrameConfigure函数更改为以下内容。
def onFrameConfigure(self, event): # Reset the scroll region to encompass the inner frame
global insertFlag
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
self.canvas.bind_all("<MouseWheel>", self._on_mousewheel)
if insertFlag == 1:
self.canvas.yview_scroll(20, "units")
insertFlag = 0
然后添加到填充底部的if语句来设置全局变量,如下所示。
def populate(self):
global insertFlag
for row in range(20):
Txt = Text(self.frame, bg="white", borderwidth=1, height=1, width=40)
Txt.grid(row = row, column = 0, rowspan = 1, columnspan = 1, sticky = W+E+N+S)
line = "this is the second column for row %s" %row
Txt.insert(END, str(line))
Txt.see(END)
insertFlag = 1
您如何看待,感谢您的意见。