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?
#1
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",
                                  variable=self.efs_chb_var,
                                  bg='black',
                                  fg='Light Blue',
                                  selectcolor='black',
                                  font=("Comic Sans MS", 8, "bold"))
        self.efs_avg_chb.grid(row=3, column=1, pady=(20, 10), sticky=W)
        self.efs_avg_chb.select()
        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
        else:
            self.my_var = 1
        print(self.my_var)
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.
Reply


Messages In This Thread
Global Variable in Event Function? What am I doing wrong here? - by p_hobbs - Nov-13-2019, 05:39 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] [Solved]Help getting variable from Function in PyQt Extra 6 1,553 Jul-06-2022, 10:19 PM
Last Post: Extra
  [Tkinter] Passing variable to function. KDog 2 2,188 May-25-2021, 09:15 PM
Last Post: KDog
  return a variable from a function snakes 3 2,839 Apr-09-2021, 06:47 PM
Last Post: snakes
  [Tkinter] tkinter global variable chalao_adda 6 11,185 Nov-14-2020, 05:37 AM
Last Post: chalao_adda
  Call local variable of previous function from another function with Python3 & tkinter Hannibal 5 4,507 Oct-12-2020, 09:16 PM
Last Post: deanhystad
  [PyQt] How to write event function Squalo 0 1,407 Sep-17-2020, 11:04 AM
Last Post: Squalo
  [Tkinter] Unable to Access global Variable when switching between frames tziyong 1 3,519 Nov-03-2019, 01:08 AM
Last Post: balenaucigasa
  [WxPython] How to return a variable from an event handler Shaba1 2 3,960 Jun-13-2019, 02:47 PM
Last Post: Shaba1
  tkinter - global variable not working albry 2 7,111 Jan-26-2019, 04:22 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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