Why is my boolean variable not updating when changed within functions in my tkinter custom scrollbar widget?

huangapple go评论65阅读模式
英文:

Why is my boolean variable not updating when changed within functions in my tkinter custom scrollbar widget?

问题

I am trying to make a custom scrollbar widget for tkinter in Python3. I'm using classes and functions to organize my code. I have a boolean variable that needs to be accessed by the rest of the program to determine whether the scrollbar is active so that it can be updated visually to move with the user's mouse. Changing this variable is done within two functions, however, when I observe the variable outside the function, it has not updated, which has made me believe that I'm doing something wrong with how I am handling self or just how global and local variables work inside functions and classes. I tried a number of things; however, I could not get it to work.

Here are the two functions that change the variable:

def scrollOn(self):
    self.scrolling = True

def scrollOff(self):
    self.scrolling = False

Both of these functions are triggered by tkinter bind events from a tkinter.Button:

scrollBar = tk.Button(window, bg="#000000")
scrollBar.place(x=390, y=30, width=20, height=40)
scrollBar.bind("<Button-1>", lambda s=self: scrollOn(s))
scrollBar.bind("<ButtonRelease-1>", lambda s=self: scrollOff(s))

This is all of the code:

import tkinter as tk

class scrollbar:

    scrolling = False   

    def __init__(self, window):

        scrollFrame = tk.Frame(window, bg="#666666")
        scrollFrame.place(x=10, y=10, width=400, height=400)

        scrollBounds = tk.Frame(window, bg="#444444")
        scrollBounds.place(x=390, y=30, width=20, height=360)

        upButton = tk.Button(window, text="")
        upButton.place(x=390, y=10, width=20, height=20)

        downButton = tk.Button(window, text="")
        downButton.place(x=390, y=390, width=20, height=20)

        def scrollOn(self):
            self.scrolling = True

        def scrollOff(self):
            self.scrolling = False
            
        scrollBar = tk.Button(window, bg="#000000")
        scrollBar.place(x=390, y=30, width=20, height=40)
        scrollBar.bind("<Button-1>", lambda s=self: scrollOn(s))
        scrollBar.bind("<ButtonRelease-1>", lambda s=self: scrollOff(s))

        def updateScrollbar(self):
            if self.scrolling == True:
                print("SCROLLING")
            window.after(1, lambda s=self: updateScrollbar(s))
            
        window.after(1, lambda s=self: updateScrollbar(s))

I tried a few different configurations that came to mind when trying to solve this problem; however, I couldn't solve it. Observing the variable inside the two functions returns the correct result. Any help and guidance would be much appreciated, constructive criticism on how I've written my code is appreciated too.

英文:

I am trying to make a custom scrollbar widget for tkinter in Python3. I'm using classes and functions to organise my code. I have a boolean variable that needs to be accessed by the rest of the program to determine whether the scrollbar is active so that it can be updated visually to move with the user's mouse. Changing this variable is done within two functions however when I observe the variable outside the function it has nto updated which has made me believe that I'm doing something wrong with how I am handling self, or just how global and local variables work inside functions and classes. I tried a number of things however I could not get it to work.

Here are the two functions that change the variable:

def scrollOn(self):
    self.scrolling = True
def scrollOff(self):
    self.scrolling = False

Both of these functions are triggered by tkinter bind events from a tkinter.Button:

scrollBar = tk.Button(window, bg = &quot;#000000&quot;)
scrollBar.place(x = 390, y = 30, width = 20, height = 40)
scrollBar.bind(&quot;&lt;Button-1&gt;&quot;, lambda s = self : scrollOn(s))
scrollBar.bind(&quot;&lt;ButtonRelease-1&gt;&quot;, lambda s = self : scrollOff(s))

This is all of the code:

import tkinter as tk

class scrollbar:

    scrolling = False   

    def __init__(self, window):

        scrollFrame = tk.Frame(window, bg = &quot;#666666&quot;)
        scrollFrame.place(x = 10, y = 10, width = 400, height = 400)

        scrollBounds = tk.Frame(window, bg = &quot;#444444&quot;)
        scrollBounds.place(x = 390, y = 30, width = 20, height = 360)

        upButton = tk.Button(window, text = &quot;&quot;)
        upButton.place(x = 390, y = 10, width = 20, height = 20)

        downButton = tk.Button(window, text = &quot;&quot;)
        downButton.place(x = 390, y = 390, width = 20, height = 20)

        def scrollOn(self):
            self.scrolling = True
        def scrollOff(self):
            self.scrolling = False
            
        scrollBar = tk.Button(window, bg = &quot;#000000&quot;)
        scrollBar.place(x = 390, y = 30, width = 20, height = 40)
        scrollBar.bind(&quot;&lt;Button-1&gt;&quot;, lambda s = self : scrollOn(s))
        scrollBar.bind(&quot;&lt;ButtonRelease-1&gt;&quot;, lambda s = self : scrollOff(s))

        def updateScrollbar(self):
            if self.scrolling == True:
                print(&quot;SCROLLING&quot;)
            window.after(1, lambda s = self : updateScrollbar(s))
            
        window.after(1, lambda s = self : updateScrollbar(s))

I tried a few different configurations that came to mind when trying to solve this problem however I couldn't solve it. Observing the variable inside the two functions returns the correct result. Any help and guidance would be much appreciated, constructive criticism on how I've wrote my code is appreciated too

答案1

得分: 2

self scrollOnscrollOff 不是 scrollbar,它是一个 ButtonPress 事件。移除这个参数或者将它改成其他东西。

def scrollOn():
    self.scrolling = True

def scrollOff():
    self.scrolling = False

scrollBar = tk.Button(window, bg="#000000")
scrollBar.place(x=390, y=30, width=20, height=40)
scrollBar.bind("<Button-1>", lambda s=self: scrollOn())
scrollBar.bind("<ButtonRelease-1>", lambda s=self: scrollOff())
英文:

self scrollOn and in scrollOff is not scrollbar, it's a ButtonPress event. Remove the parameter or change it to something else

def scrollOn():
    self.scrolling = True

def scrollOff():
    self.scrolling = False

scrollBar = tk.Button(window, bg=&quot;#000000&quot;)
scrollBar.place(x=390, y=30, width=20, height=40)
scrollBar.bind(&quot;&lt;Button-1&gt;&quot;, lambda s=self: scrollOn())
scrollBar.bind(&quot;&lt;ButtonRelease-1&gt;&quot;, lambda s=self: scrollOff())

huangapple
  • 本文由 发表于 2023年5月21日 21:14:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定