Why is the Tkinter Scroll Bar only active when you hover over it?

心不动则不痛 提交于 2019-12-11 10:53:19

问题


I am using Tkinter to create a GUI and the scroll bar is only active when I put my mouse over it and scroll. Is there a way so I can scroll using my scroll wheel on the mouse when the mouse is not on the scroll bar and in the middle of the window?

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()

来源:https://stackoverflow.com/questions/29672739/why-is-the-tkinter-scroll-bar-only-active-when-you-hover-over-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!