我正在使用Tkinter创建一个GUI,只有当我将鼠标放在它上面并滚动时,滚动条才会激活。当鼠标不在滚动条上和窗口中间时,有没有办法让鼠标滚轮滚动?
import Tkinter as Tk
from Tkinter import StringVar
class Example(Tk.Frame):
def __init__(self, root):
Tk.Frame.__init__(self, root)
self.canvas = Tk.Canvas(root, width=200, heigh=200, 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((10,10), window=self.frame, anchor="nw",
tags="self.frame")
self.frame.bind("<Configure>", self.OnFrameConfigure)
self.populate()
def populate(self):
#####Last Name######
labelText=StringVar()
labelDir=Tk.Label(self.frame, text='hello').grid(row=1, column=1)
labelDir
labelText=StringVar()
labelDir1=Tk.Label(self.frame, text='hello').grid(row=2, column=2)
labelDir1
labelText=StringVar()
labelDir2=Tk.Label(self.frame, text='hello').grid(row=1, column=1)
labelDir2
directory=StringVar(None)
self.can_lname =Tk.Entry(self.frame, text=directory,width=100).grid(row=4, column=4)
self.can_lname
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()