0

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!

1
  • 1
    Your indentation is all over the place, can you please clean that up? Commented Apr 25, 2013 at 17:15

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.