Any idea why execfile() call in Tkinter button cause "python.exe has stopped working"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Haiyan
    New Member
    • Jul 2010
    • 17

    Any idea why execfile() call in Tkinter button cause "python.exe has stopped working"

    Dear Experts:

    I made one GUI interface with Tkinter. I have some buttons on the GUI and one button is designed as "start test" which will invoke one "start test" window. I put one "ok" and one "cancel" button in this window. When I click the "ok" button it will use execfile() to call another python file which will start multiple threads to run. The multiple threads did finish but I immediately got "python.exe has stopped working" error message. Do you have any suggestions? My operation system is Windows Vista with service pack 1. Looks like it is not related to OS and it could be related to my script. I attached part of my script below.

    Thank you very much!

    Haiyan

    Related scripts in GUI side:
    Code:
       def pressed_start_test(self):
          self.Start_Test_Window = Toplevel()
          self.Start_Test_Window.title("Start Test for Selected Slots")
    
          self.startTest_Label1 = Label( self.Start_Test_Window, text = "Start Test for Selected Slots?", width = 40, padx=5, pady=5, relief = RAISED )
          self.startTest_Label1.grid( row = 0, column = 0, columnspan = 2, sticky = W+E+N+S )
    
          self.startTest_button1 = Button( self.Start_Test_Window, text = "OK", width = 20,
                               padx=5, pady=5,
                               command = self.pressed_startTest_OK,
                               font = "Arial 9 bold")
          self.startTest_button1.grid( row = 1, column = 0, columnspan = 1, sticky = W+E+N+S )
          self.startTest_button1.bind( "<Enter>", self.rolloverEnter )
          self.startTest_button1.bind( "<Leave>", self.rolloverLeave )
    
          self.startTest_button2 = Button( self.Start_Test_Window, text = "Cancel", width = 20,
                               padx=5, pady=5,
                               command = self.pressed_startTest_Cancel,
                               font = "Arial 9 bold")
          self.startTest_button2.grid( row = 1, column = 1, columnspan = 1, sticky = W+E+N+S )
          self.startTest_button2.bind( "<Enter>", self.rolloverEnter )
          self.startTest_button2.bind( "<Leave>", self.rolloverLeave )
    
       def pressed_startTest_OK(self):
          valid_checked_button_list = self.get_checked_button_name()
    
          f = open('selected_slot_driveSN.list', 'w')
          p = pickle.Pickler(f)
          p.dump(valid_checked_button_list)
          f.close()
    
          self.Start_Test_Window.destroy()
          execfile("BootScript.py", globals())
    
    
       def pressed_startTest_Cancel(self):
          self.Start_Test_Window.destroy()

    Related scripts in BootScript.py:

    Code:
    for i in range(len(selected_slot__list)):
       boat_slot_thread = IOProcessThread(selected_slot__list[i][1],       
                                         selected_slot__list[i][0][1]-1)   # Slot Number
       boat_slot_thread.start()
       time.sleep(1)
       print >> PC_process_log, "Boat_%d_Slot_%d_%s started..." %(selected_slot__list[i][0][0],
                                                                  selected_slot__list[i][0][1],
                                                                  selected_slot__list[i][1])
    The thread class definition:

    Code:
    class IOProcessThread(threading.Thread):
    
       #-------------------------------------------------------------------------------------------------------
       # Override Thread's __init__ method to accept the parameters needed:
       def __init__ ( self, SN, slot ):
          threading.Thread.__init__ ( self )
          self.SN = SN
          self.slot = slot
    
       #-------------------------------------------------------------------------------------------------------
       def run ( self ):
          testStartTimeList = str(datetime.now())[0:19].split()
          filenametuple = (self.SN, testStartTimeList[0], testStartTimeList[1].replace(':', '.'))# File name can't have ':', so replace with '.'
          filename_log = '_'.join(filenametuple) + '.txt'    # Souce test out file
          filename_csv = '_'.join(filenametuple) + '.csv'    # Date only test out file for database
          testout_log = open(filename_log, 'w')
          testout_csv = open(filename_csv, 'w')
          stateMachine('INIT', self.slot, testout_log, testout_csv)
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    I am guessing here - Try hiding the window with withdraw() until execfile() is finished. You could also try exiting the main loop with quit().

    Comment

    • Haiyan
      New Member
      • Jul 2010
      • 17

      #3
      Originally posted by bvdet
      I am guessing here - Try hiding the window with withdraw() until execfile() is finished. You could also try exiting the main loop with quit().
      Hi, bvdet:

      Thanks a lot for your suggestions!

      I did try following 4 options and they didn't work:

      1:
      self.Start_Test _Window.withdra w()
      execfile("BootS cript.py", globals())

      2:
      execfile("BootS cript.py", globals())
      self.Start_Test _Window.withdra w()

      3:
      execfile("BootS cript.py", globals())
      self.Start_Test _Window.destroy ()

      4:
      self.Start_Test _Window.destroy ()
      execfile("BootS cript.py", globals())

      But the main GUI quit before the execfile() call will make it better and Python won't crash. You know I hope to keep the main Tkinter GUI alive to show the test status.

      boat1.quit() # quit the GUI main loop
      execfile("BootS cript.py", globals())

      Looks like it is not related to the multiple threads in the execfile("BootS cript.py", globals()) call. I changed from "BootScript .py" to another simple python script with 20 threads running to print out something, and it works fine.

      Then I tear the "BootScript .py" to small parts to debug which part caused the Python crash. It turns out one DLL I used ctypes to import, and python crashes after running the destructor of this DLL. I have to use this DLL file and I didn't find a way to handle this situation yet. I will post my finding with this thread if I can figure out this problem.

      I tried to use C# to generate one quick GUI and it works fine for the "BootScript .py" call with multiple threads run.

      Thanks a lot for your help!

      Haiyan

      Comment

      • Haiyan
        New Member
        • Jul 2010
        • 17

        #4
        Now I moved the destructor running out of main GUI and only run after main GUI terminated. The Python didn't get crashed any more.

        Comment

        Working...