Python Forum
Global Variable in Event Function? What am I doing wrong here?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Global Variable in Event Function? What am I doing wrong here?
I'm using tkinter to create a data entry form, and I'm having issues with getting the state of a checkbutton. So I determined the simplest way is to set a variable, and change that variable each time the checkbutton is clicked. I've tried everything google has suggested for how to do this, and can't seem to get it to work. So, here is what I've gotten so far, but it tells me that I've got an invalid syntax when trying to set the variable as a global variable.

class efs_form():
    def __init__(self):
        # Average Checkbox
        self.efs_chb_var = IntVar()
        self.efs_avg_chb = Checkbutton(self.efs_ef,
                                  text="Include in Average",
                                  fg='Light Blue',
                                  font=("Comic Sans MS", 8, "bold"))
        self.efs_avg_chb.grid(row=3, column=1, pady=(20, 10), sticky=W)
        self.my_var = 1
        self.var = self.efs_chb_var.get()
        self.efs_avg_chb.bind("<Button-1>", self.chb_click)
    def chb_click(self, event):
        global self.my_var
        If self.my_var == 1:
            self.my_var = 0
            self.my_var = 1
From what I understand (which is very limited), you have to set a variable to global in the function that you are changing the variable. What am I doing wrong here?

I figured this out. I actually don't need to declare the variable as a global variable in the function, and then my "if" statement shouldn't be capitalized. I got it to work just fine now.
Okay well the first mistake is using a Global Variable -- any programmer that has experienced the pain of global variables will tell never use one unless absolutely necessary which means most of the time do not use one no matter how tempting it might seem. Take extra time to figure out how to not make it global as that will save you immense amount of time later on.

To fix your code though delete the entire global statement on line 18 as it is not needed in this context

self. anything declared within the __init__ function causes that variable to be available to the ENTIRE class through out the lifetime of that instance of that class object.

not knowing Tkinter though I cannot speak to your usage of the Checkbutton's Signal and Slot mechanics if after deleting that line if it still does not seem to work I would strongly suggest you looking into your usage of the Checkbutton's event handling syntax -- either that or perhaps a Tkinter aficionado will chime in

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Passing variable to function. KDog 2 326 May-25-2021, 09:15 PM
Last Post: KDog
  return a variable from a function snakes 3 756 Apr-09-2021, 06:47 PM
Last Post: snakes
  [Tkinter] tkinter global variable chalao_adda 6 784 Nov-14-2020, 05:37 AM
Last Post: chalao_adda
  Call local variable of previous function from another function with Python3 & tkinter Hannibal 5 1,013 Oct-12-2020, 09:16 PM
Last Post: deanhystad
  [PyQt] How to write event function Squalo 0 419 Sep-17-2020, 11:04 AM
Last Post: Squalo
  [Tkinter] Unable to Access global Variable when switching between frames tziyong 1 1,171 Nov-03-2019, 01:08 AM
Last Post: balenaucigasa
  [WxPython] How to return a variable from an event handler Shaba1 2 1,990 Jun-13-2019, 02:47 PM
Last Post: Shaba1
  tkinter - global variable not working albry 2 4,306 Jan-26-2019, 04:22 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020