how to use a variable with a GUI in python 3
Question: I'm trying to get this so when you press 'HELLO' five time the text turn red
just its not adding any thing on when i add anything to the viable.this is the
code.

    
    
        from tkinter import *
        class application(Frame):
        global t
      t=1  
       def info(self):
          print ("test")
        global t
        t=t+5
    
      def createWidgets(self):
        global t
        t=t
        self.exet= Button(self)
        self.exet["text"] = "QUIT"
        self.exet["fg"] = "red"
        self.exet["command"] = self.quit
    
        self.exet.pack({"side": "left"})
    
        self.hi = Button(self)
        self.hi["text"] = "HELLO",
        if t == 5:
            self.hi["fg"] = "red"
    
        self.hi["command"] = self.info
        self.hi.pack({"side": "left"})
    
      def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.createWidgets()
    

Thanks to anyone helps!


Answer: You have a couple of problems here: First, you use a global variable and then
you include it within the scope of your functions. Instead of this, you should
use an instance variable (`self.t`, or even `self.counter` for better
readability). Secondly, you are checking the value of the counter in
`createWidgets`, which is only called once by the `__init__` method. You
should increase and check its value in the event handler function on the
button.

    
    
    class application(Frame):
    
        def info(self):
            self.counter += 1
            print(self.counter)
            if self.counter == 5:
                self.hi["fg"] = "red"
    
        def createWidgets(self):
            self.counter = 0
            self.exet= Button(self)
            self.exet["text"] = "QUIT"
            self.exet["fg"] = "red"
            self.exet["command"] = self.quit
            self.exet.pack({"side": "left"})
    
            self.hi = Button(self)
            self.hi["text"] = "HELLO",
            self.hi["command"] = self.info
            self.hi.pack({"side": "left"})
    
        def __init__(self, master=None):
            Frame.__init__(self, master)
            self.pack()
            self.createWidgets()
    

