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()