Hello all,
I am a beginning python user and I am a bit perplexed by something that I am writing up.
What I have to do is assign buttons to colors (Traffic lightesque).
There is a red, a green and an auto.
The tricky part is that the "auto" button has to cycle between the red and green.
I have not been able to accomplish that.
When I define fillAuto, the light stays red. If I do not define fillAuto, then I get an error that fillAuto has no attributes.
Any help would be greatly appreciated.

from tkinter import *
import time

class MyGui(Frame):
   #state = 1 ---> label is green
   #state = 0 ---> label is red
   state = 0
   
   def __init__(self, master=None):    # 1
      Frame.__init__(self)    # 2
      self.master.title( "Traffic Light Control EGR 101")
      self.grid(sticky=W+E+N+S)
      self.master.rowconfigure(0, weight=1)  # 3
      self.master.columnconfigure(0, weight=1)  # 4
 
      self.canvas = Canvas(self, width=100, height=100)
      self.canvas.grid(row=0, rowspan=3, column=0)

      self.canvas.create_oval(10,10,90,90, width=2, tags="topLight")    # 5

      self.greenButt = Button(self, text="Green", command=self.fillGreen)  # 6
      self.greenButt.grid(row=0, column = 1, sticky=W+E+N+S)   # 7

      self.redButt = Button(self, text="Red", command=self.fillRed)
      self.redButt.grid(row=1, column=1, sticky=W+E+N+S)

      self.autoButt = Button(self, text="Auto", command=self.fillAuto)
      self.autoButt.grid(row=2, column=1, sticky=W+E+N+S)

      self.label1 = Label(self, text="Hello", font=16)
      self.label1.grid(row=3, column=1)

   def fillGreen(self):
      self.canvas.itemconfigure("topLight", fill="green")   # 8
      self.labelColor()
        
   def fillRed(self):
      self.canvas.itemconfigure("topLight", fill="red")
      self.labelColor()

   def fillAuto(self):
      self.canvas.itemconfigure("topLight", fill="red")
      self.labelColor()

   def labelColor(self):
    
      if self.state == 1:
         self.state = 0
         self.label1.configure(fg="red")  # 9
      else:
         self.state = 1
         self.label1.configure(fg="green")

   def auto(init):
      if self.state == 1:
         self.state = 0
         self.autoButt.configure(text="Auto On")
         self.greenButt.configure(state=NORMAL)
         self.redButt.configure(state=NORMAL)
      else:
         self.state = 1
         self.autoButt.configure(text="Auto Off")
         self.greenButt.configure(state=DISABLED)
         self.redButt.configure(state=DISABLED)

      while self.state == 1:
         self.canvas.itemconfigure("toplight", fill="red")
         self.update()
         time.sleep(1)
         self.canvas.itemconfigure("toplight", fill="green")
         self.update()
         time.sleep(1)
                                          
def main():
   MyGui().mainloop()

You must set starting state in init.

If you use a loop then you should also use "update_idletasks" or no updates will happen until the loop exits. Note that this code is for Python2.x

from Tkinter import *
import time
 
class MyGui():
   #state = 1 ---> label is green
   #state = 0 ---> label is red
   #state = 0
 
   def __init__(self, master=None):    # 1
#      Frame.__init__(self)    # 2
      self.master=master
      self.master.title( "Traffic Light Control EGR 101")
      self.master.grid()
      self.master.rowconfigure(0, weight=1)  # 3
      self.master.columnconfigure(0, weight=1)  # 4
      
      self.canvas = Canvas(self.master, width=100, height=100)
      self.canvas.grid(row=0, rowspan=3, column=0)
 
      self.canvas.create_oval(10,10,90,90, width=2, tags="topLight")    # 5
      self.state=0
      self.greenButt = Button(self.master, text="Green", command=self.fillGreen)  # 6
      self.greenButt.grid(row=0, column = 1, sticky=W+E+N+S)   # 7
 
      self.redButt = Button(self.master, text="Red", command=self.fillRed)
      self.redButt.grid(row=1, column=1, sticky=W+E+N+S)
 
      self.autoButt = Button(self.master, text="Auto", command=self.fillAuto)
      self.autoButt.grid(row=2, column=1, sticky=W+E+N+S)
 
      self.label1 = Label(self.master, text="Hello", font=16)
      self.label1.grid(row=3, column=1)
 
   def fillGreen(self):
      self.canvas.itemconfigure("topLight", fill="green")   # 8
      self.label_color()
 
   def fillRed(self):
      self.canvas.itemconfigure("topLight", fill="red")
      self.label_color()
 
   def fillAuto(self):
      for ctr in range(10):     ## cycle 10 times and quit
          print ctr
          if self.state:
             self.fillGreen()
          else:
             self.fillRed()
          self.master.update_idletasks()
          self.state=not self.state
          self.label_color()
          time.sleep(1)
 
   def label_color(self):
      if self.state == 1:
         self.state = 0
         self.label1.configure(fg="red")  # 9
      else:
         self.state = 1
         self.label1.configure(fg="green")
 
root=Tk()
MG=MyGui(root)
root.mainloop()

Ah, I see. Thanks a bunch. Appreciate it.

Please mark the thread as solved to save others the time it takes to read through this post only to realize it is solved. I think the "solved" button is just above the box where replies are posted..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.