0

I am having a little trouble ending my program when I click on my End Program button. The current code that I have keeps making the program freeze when I try to exiting via the button.
Also, I am having some issues with my Auto Button not working.
Any help would be greatly appreciated.

The end area is the def endProgram (line 190)

from tkinter import *
import time
from sys import exit

class MyGui(Frame):
   state = 0
   
   def __init__(self, master=None):
      Frame.__init__(self)
      self.master.title( "Lights")
      self.grid(sticky=W+E+N+S)
      self.master.rowconfigure(0, weight=1)
      self.master.columnconfigure(0, weight=1)
 
      self.canvas = Canvas(self, width=1000, height=850)
      self.canvas.grid(row=0, rowspan=7, column=0, columnspan=5)

      self.canvas.create_polygon(50, 175, 175, 50, 475, 50, 600, 175, 600, 475, 475, 600, 175, 600, 50, 475, outline='black', fill='white', width=3, tags="roadMap")

      self.canvas.create_polygon(150, 187.5, 187.5, 150, 237.5, 150, 275, 187.5, 275, 237.5, 237.5, 275, 187.5, 275, 150, 237.5, outline='black', fill='white', width=3)
      self.canvas.create_polygon(375, 187.5, 412.5, 150, 462.5, 150, 500, 187.5, 500, 237.5, 462.5, 275, 412.5, 275, 375, 237.5, outline='black', fill='white', width=3)
      self.canvas.create_polygon(150, 412.5, 187.5, 375, 237.5, 375, 275, 412.5, 275, 462.5, 237.5, 500, 187.5, 500, 150, 462.5, outline='black', fill='white', width=3)
      self.canvas.create_polygon(375, 412.5, 412.5, 375, 462.5, 375, 500, 412.5, 500, 462.5, 462.5, 500, 412.5, 500, 375, 462.5, outline='black', fill='white', width=3)

      self.canvas.create_oval(250, 75, 300, 125, width=2, tags="I2W")
      self.canvas.create_oval(300, 125, 350, 175, width=2, tags="I2S")
      self.canvas.create_oval(350, 75, 400, 125, width=2, tags="I2E")
      self.canvas.create_oval(475, 300, 525, 350, width=2, tags="I3W")
      self.canvas.create_oval(525, 250, 575, 300, width=2, tags="I3N")
      self.canvas.create_oval(525, 350, 575, 400, width=2, tags="I3S")
      self.canvas.create_oval(350, 525, 400, 575, width=2, tags="I4E")
      self.canvas.create_oval(300, 475, 350, 525, width=2, tags="I4N")
      self.canvas.create_oval(250, 525, 300, 575, width=2, tags="I4W")
      self.canvas.create_oval(75, 350, 125, 400, width=2, tags="I5S")
      self.canvas.create_oval(125, 300, 175, 350, width=2, tags="I5E")
      self.canvas.create_oval(75, 250, 125, 300, width=2, tags="I5N")
      self.canvas.create_oval(350, 300, 400, 350, width=2, tags="I1E")
      self.canvas.create_oval(300, 250, 350, 300, width=2, tags="I1N")
      self.canvas.create_oval(250, 300, 300, 350, width=2, tags="I1W")
      self.canvas.create_oval(300, 350, 350, 400, width=2, tags="I1S")

      self.canvas.create_rectangle (300, 300, 350, 350, width=2, tags="SI1")
      self.canvas.create_rectangle (300, 75, 350, 125, width=2, tags="SI2")
      self.canvas.create_rectangle (525, 300, 575, 350, width=2, tags="SI3")
      self.canvas.create_rectangle (300, 525, 350, 575, width=2, tags="SI4")
      self.canvas.create_rectangle (75, 300, 125, 350, width=2, tags="SI5")
      
      self.canvas.create_text(325, 325, text="I-1", font=("Times",16))
      self.canvas.create_text(325, 100, text="I-2", font=("Times",16))
      self.canvas.create_text(550, 325, text="I-3", font=("Times",16))
      self.canvas.create_text(325, 550, text="I-4", font=("Times",16))
      self.canvas.create_text(100, 325, text="I-5", font=("Times",16))

      self.ChangeI1Butt = Button(self, text="Change I-1", command=self.changeI1)
      self.ChangeI1Butt.grid(row=0, column=4, sticky=W+E+N+S)

      self.ChangeI2Butt = Button(self, text="Change I-2", command=self.changeI2)
      self.ChangeI2Butt.grid(row=1, column=4, sticky=W+E+N+S)

      self.ChangeI3Butt = Button(self, text="Change I-3", command=self.changeI3)
      self.ChangeI3Butt.grid(row=2, column=4, sticky=W+E+N+S)

      self.ChangeI4Butt = Button(self, text="Change I-4", command=self.changeI4)
      self.ChangeI4Butt.grid(row=3, column=4, sticky=W+E+N+S)

      self.ChangeI5Butt = Button(self, text="Change I-5", command=self.changeI5)
      self.ChangeI5Butt.grid(row=4, column=4, sticky=W+E+N+S)

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

      self.EndProgram = Button(self, text="End Program", command=self.endProgram)
      self.EndProgram.grid(row=5, column=2, sticky=W+E+N+S)
   
   def labelColor(self):
    
      if self.state == 1:
         self.state = 0
         self.label1.configure(fg="red")
      else:
         self.state = 1
         self.label1.configure(fg="green")
         
   def changeI1 (self) :
      if self.state == 0:
         self.canvas.itemconfigure("I1N", fill="yellow")
         self.canvas.itemconfigure("I1S", fill="yellow")
         self.canvas.itemconfigure("I1W", fill="red")
         self.canvas.itemconfigure("I1E", fill="red")
         self.update()
         time.sleep(1)
         self.canvas.itemconfigure("I1W", fill="green")
         self.canvas.itemconfigure("I1E", fill="green")
         self.canvas.itemconfigure("I1N", fill="red")
         self.canvas.itemconfigure("I1S", fill="red")
         self.state = 2
      else:
         self.canvas.itemconfigure("I1W", fill="yellow")
         self.canvas.itemconfigure("I1E", fill="yellow")
         self.update()
         time.sleep(1)
         self.canvas.itemconfigure("I1W", fill="red")
         self.canvas.itemconfigure("I1E", fill="red")
         self.canvas.itemconfigure("I1N", fill="green")
         self.canvas.itemconfigure("I1S", fill="green")
         self.state = 0

   def changeI2 (self) :
      if self.state == 0:
         self.canvas.itemconfigure("I2S", fill="yellow")
         self.canvas.itemconfigure("I2W", fill="red")
         self.canvas.itemconfigure("I2E", fill="red")
         self.update()
         time.sleep(1)
         self.canvas.itemconfigure("I2W", fill="green")
         self.canvas.itemconfigure("I2E", fill="green")
         self.canvas.itemconfigure("I2S", fill="red")
         self.state = 3
      else:
         self.canvas.itemconfigure("I2W", fill="yellow")
         self.canvas.itemconfigure("I2E", fill="yellow")
         self.update()
         time.sleep(1)
         self.canvas.itemconfigure("I2W", fill="red")
         self.canvas.itemconfigure("I2E", fill="red")
         self.canvas.itemconfigure("I2S", fill="green")
         self.state = 0

   def changeI3 (self) :
      if self.state == 0:
         self.canvas.itemconfigure("I3N", fill="yellow")
         self.canvas.itemconfigure("I3S", fill="yellow")
         self.canvas.itemconfigure("I3W", fill="red")
         self.update()
         time.sleep(1)
         self.canvas.itemconfigure("I3W", fill="green")
         self.canvas.itemconfigure("I3N", fill="red")
         self.canvas.itemconfigure("I3S", fill="red")
         self.state = 4
      else:
         self.canvas.itemconfigure("I3W", fill="yellow")
         self.update()
         time.sleep(1)
         self.canvas.itemconfigure("I3W", fill="red")
         self.canvas.itemconfigure("I3N", fill="green")
         self.canvas.itemconfigure("I3S", fill="green")
         self.state = 0

   def changeI4 (self) :
      if self.state == 0:
         self.canvas.itemconfigure("I4N", fill="yellow")
         self.canvas.itemconfigure("I4W", fill="red")
         self.canvas.itemconfigure("I4E", fill="red")
         self.update()
         time.sleep(1)
         self.canvas.itemconfigure("I4W", fill="green")
         self.canvas.itemconfigure("I4E", fill="green")
         self.canvas.itemconfigure("I4N", fill="red")
         self.state = 5
      else:
         self.canvas.itemconfigure("I4W", fill="yellow")
         self.canvas.itemconfigure("I4E", fill="yellow")
         self.update()
         time.sleep(1)
         self.canvas.itemconfigure("I4W", fill="red")
         self.canvas.itemconfigure("I4E", fill="red")
         self.canvas.itemconfigure("I4N", fill="green")
         self.state = 0

   def changeI5 (self) :
      if self.state == 0:
         self.canvas.itemconfigure("I5N", fill="yellow")
         self.canvas.itemconfigure("I5S", fill="yellow")
         self.canvas.itemconfigure("I5E", fill="red")
         self.update()
         time.sleep(1)
         self.canvas.itemconfigure("I5E", fill="green")
         self.canvas.itemconfigure("I5N", fill="red")
         self.canvas.itemconfigure("I5S", fill="red")
         self.state = 6
      else:
         self.canvas.itemconfigure("I5E", fill="yellow")
         self.update()
         time.sleep(1)
         self.canvas.itemconfigure("I5E", fill="red")
         self.canvas.itemconfigure("I5N", fill="green")
         self.canvas.itemconfigure("I5S", fill="green")
         self.state = 0

   def endProgram (self):
      if self.state == 0:
         exit(0)
      else:
         pass

   def fillAuto(self):

      if self.state == 0:
         self.state = 1
         self.ChangeAutoButt.configure(text="Auto Off")
         self.ChangeI1Butt.configure(state=DISABLED)
         self.ChangeI2Butt.configure(state=DISABLED)
         self.ChangeI3Butt.configure(state=DISABLED)
         self.ChangeI4Butt.configure(state=DISABLED)
         self.ChangeI5Butt.configure(state=DISABLED)
      else:
         self.state = 0
         self.ChangeAutoButt.configure(text="Auto On")
         self.ChangeI1Butt.configure(state=NORMAL)
         self.ChangeI2Butt.configure(state=NORMAL)
         self.ChangeI3Butt.configure(state=NORMAL)
         self.ChangeI4Butt.configure(state=NORMAL)
         self.ChangeI5Butt.configure(state=NORMAL)

def main():
   MyGui().mainloop()   

if __name__ == "__main__":
   main()

Edited by algrandjean1: n/a

2
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by woooee
0

sys.exit() is never a good idea. In this case Tkinter's quit() is the preferred way to exit. The program runs on my Slackware system with this modification to endProgram()

def endProgram (self):
      if self.state == 0:
         self.master.quit()
      else:
         print ("state is not zero -->", self.state)
0

Thanks a lot man. That fixed it up. It still freezes when I try to run it in IDLE, but when I run it from terminal (mac) it works fine. Must just be some problem with IDLE.
Once again, thanks.

0

You can't run Tkinter apps from Idle because Idle is written in Tkinter and 2 instances of the Tkinter class confuses it. You can also clean up a lot of the redundant code.

""" instead of changeI1(), changeI2(), etc. use a list to store the buttons and
          have them all point to the same function, providing the button number.
      """    
      from functools import partial
      self.button_list = []
      for b in range(4):
         but = Button(self, text="Change I-"+str(b+1))
         but.bind("<Button-1>", partial(self, changeI, but_num=b)
         but.grid(row=b, column=4, sticky=W+E+N+S)
         self.button_list.append(but)

      """ all buttons call this function.  The button number will look up the button
          number in a dictionary
      """
      def changeI((self, event, but_num):
         ## this is an example only
         ## but ditto for button numbers 3, & 4
         change_dict={1:(("I1N", "yellow", "green"), ("I1S", "yellow", "red"),
                         ("I1W", "red", "green"), ("I1E", "red", "green")),
                      2:(("I2S", "yellow", "red"), ("I2W", "red", "green"),
                         ("I2E", "red", "green"))}

         for tag, color_1, color_2 in change_dict[but_num]:
             self.canvas.itemconfigure(tag, fill=color_1)
         self.update()
         time.sleep(1)
         for tag, color_1, color_2 in change_dict[but_num]:
             self.canvas.itemconfigure(tag, fill=color_2)

      # use a for loop to update the buttons
      else:
         self.state = 0
         self.ChangeAutoButt.configure(text="Auto On")
         for but in self.button_list:
             but.configure(state=NORMAL)

         #self.ChangeI1Butt.configure(state=NORMAL)
         #self.ChangeI2Butt.configure(state=NORMAL)
         #self.ChangeI3Butt.configure(state=NORMAL)
         #self.ChangeI4Butt.configure(state=NORMAL)
         #self.ChangeI5Butt.configure(state=NORMAL)

Edited by woooee: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.