解决问题的方法与将两个小部件连接到单个滚动条的方法几乎相同:为鼠标滚轮创建自定义绑定,并使这些绑定影响两个列表框,而不只是一个列表框。
唯一真正的窍门是,您会根据平台在鼠标滚轮上获得不同的事件:Windows和Mac获取
<MouseWheel>事件,Linux获取
<Button-4>和
<Button-5>事件。
这是一个示例,在我的Mac上使用python 2.5进行了测试:
import Tkinter as tkclass App: def __init__(self): self.root=tk.Tk() self.vsb = tk.Scrollbar(orient="vertical", command=self.OnVsb) self.lb1 = tk.Listbox(self.root, yscrollcommand=self.vsb.set) self.lb2 = tk.Listbox(self.root, yscrollcommand=self.vsb.set) self.vsb.pack(side="right",fill="y") self.lb1.pack(side="left",fill="x", expand=True) self.lb2.pack(side="left",fill="x", expand=True) self.lb1.bind("<MouseWheel>", self.OnMouseWheel) self.lb2.bind("<MouseWheel>", self.OnMouseWheel) for i in range(100): self.lb1.insert("end","item %s" % i) self.lb2.insert("end","item %s" % i) self.root.mainloop() def onVsb(self, *args): self.lb1.yview(*args) self.lb2.yview(*args) def onMouseWheel(self, event): self.lb1.yview("scroll", event.delta,"units") self.lb2.yview("scroll",event.delta,"units") # this prevents default bindings from firing, which # would end up scrolling the widget twice return "break"app=App()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)