如果使用mouswheel滚动,画布会离开scrollregion

时间:2016-03-27 12:12:38

标签: python-3.x scroll tkinter mousewheel tkinter-canvas

问题:即使第一项应位于画布的最顶部,您也可以向上滚动

如何重现问题:在画布上至少有一个项目并使用鼠标滚动(按空格键制作项目)

注意:一旦项目比画布本身宽,问题就会停止

示例代码

import tkinter as tk

class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.bind("<MouseWheel>", self.scroll)
        self.bind("<KeyPress>", self.keypressed)
        self.TargetCanvas = None
        self.makeui()

    def makeui(self):
        self.Canvas = tk.Canvas(self, bg = "white")
        self.Canvas.grid(row = 0, column = 0, sticky = "W")
        sby = tk.Scrollbar(self, orient = "vertical", command  = self.Canvas.yview)
        sby.grid(row = 0, column = 1, sticky = "ns")
        self.Canvas.configure(yscrollcommand = sby.set)
        self.CanvFrame = tk.Frame(self.Canvas, bg = "white")
        self.Canvas.create_window((0, 0), window = self.CanvFrame, anchor = "nw")
        self.CanvFrame.bind("<Configure>", lambda event: self.Canvas.configure(scrollregion = self.Canvas.bbox("all")))
        self.Canvas.bind("<Enter>", lambda event: self.settarget(event, True))
        self.Canvas.bind("<Leave>", lambda event: self.settarget(event, False))
        tk.Label(self, text = "space = add item, c = clear all").grid(row = 1, column = 0)



    def settarget(self, event, check):
        if check:
            self.TargetCanvas = event.widget
        else:
            self.TargetCanvas = None

    def scroll(self, event):
        if self.TargetCanvas is not None:
            direction = 0
            if event.num == 5 or event.delta == -120:
                direction = 1
            if event.num == 4 or event.delta == 120:
                 direction = -1
            self.TargetCanvas.yview_scroll(direction, tk.UNITS)

    def keypressed(self, event):
        if event.keysym == "space":
           self.additem()
        elif event.keysym == "c":
            self.clearall()

    def clearall(self):
        for i in self.CanvFrame.winfo_children():
            i.destroy()
        x = tk.Label(self.CanvFrame)
        #kick in frame resizing
        x.pack()
        self.update()
        self.Canvas.configure(scrollregion = self.Canvas.bbox("all"))
        x.destroy()


    def additem(self):
        tk.Label(self.CanvFrame, text = "asd").pack(side = tk.TOP)


def main():
    App().mainloop()

if __name__ == "__main__":
   main()

0 个答案:

没有答案