1

Can any one please tell me whats wrong with this piece of code.

When I press button 1 - everything is good. I want to press button2 - to stop the process started by button 1 and do an another process. I am unable to do it - MY GUI is going irresponsive.

You are welcome to edit the serial communication with PRINT statements if you like in doit and doit2 functions.

Please dont comment about how I made the GUI - it is just a quick example. Please comment on why I am unable to pass the pill2kill - when I press the button 2. And why my GUI is going to irresponsive state.

import threading
import time
import numpy as np
import serial
from Transmit import Write
from Receive import Read
import struct
import time
import serial.tools.list_ports


import wx







class windowClass(wx.Frame):
def __init__(self, parent, title):

    appSize_x = 1100
    appSize_y = 800

    super(windowClass, self).__init__(parent, title = title, style = wx.MINIMIZE_BOX | wx.SYSTEM_MENU | wx.CLOSE_BOX |wx.CAPTION, size = (appSize_x, appSize_y))

    self.basicGUI()
    self.Centre()
    self.Show()

def basicGUI(self):


    # Main Panel
    panel1 = wx.Panel(self)

    panel1.SetBackgroundColour('#D3D3D3')

    firmware_version = wx.StaticText(panel1, -1, "RANDOM1", pos = (70, 10) )

    firmware_version_text_control = wx.TextCtrl(panel1, -1, size = (70,25), pos = (105,40))

    pump_model_serial_number = wx.StaticText(panel1, -1, "RANDOM2", pos=(25, 75))
    pump_model_serial_number.SetBackgroundColour('yellow')

    model = wx.StaticText(panel1, -1, "RANDOM3", pos=(110, 100))

    self.listbox = wx.ListBox(panel1, -1, size = (300,250), pos = (20,135))

    clear_history = wx.Button(panel1, -1, 'BUTTON1', size = (225,30), pos = (40, 400))
    clear_history.SetBackgroundColour('RED')
    clear_history.Bind(wx.EVT_BUTTON, self.OnClearHistory)

    clear_history2 = wx.Button(panel1, -1, 'BUTTON2', size=(225, 30), pos=(40, 500))
    clear_history2.SetBackgroundColour('GREEN')
    clear_history2.Bind(wx.EVT_BUTTON, self.OnClearHistory2)



def OnClearHistory(self, event):

    self.pill2kill = threading.Event()
    self.t = threading.Thread(target=self.doit, args=(self.pill2kill, "task"))
    self.t.start()
    self.t.join()

def OnClearHistory2(self, event):

    self.pill2kill.set()

    self.t1 = threading.Thread(target=self.doit2)
    self.t1.start()
    time.sleep(5)
    self.t1.join()


def doit(self, stop_event, arg):

    while not stop_event.wait(1):
        print ("working on %s" % arg)

        ser = serial.Serial(3, 115200)
        c = ser.write('\x5A\x03\x02\x02\x02\x09')
        print c
        d = ser.read(7)
        print d.encode('hex')
        ser.close()


    print("Stopping as you wish.")


def doit2(self):
    #print ("working on %s" % arg)

    ser = serial.Serial(3, 115200)
    c = ser.write('\x5A\x03\x02\x08\x02\x0F') # Writing to an MCU
    print c
    d = ser.read(7)
    print d.encode('hex')
    ser.close()




def random():
    app = wx.App()
    windowClass(None, title='random')
    app.MainLoop()

random()
Jesh Kundem
  • 960
  • 3
  • 18
  • 30

2 Answers2

1

Don't use the .join commands. They are blocking the main thread.
See this SO question for an in depth description:
what is the use of join() in python threading

Community
  • 1
  • 1
Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60
1

Thread.join will block until the thread terminates. If your GUI's event handlers are blocked and unable to return to the MainLoop then other events can not be received and dispatched, and so the application appears to be frozen.

RobinDunn
  • 6,116
  • 1
  • 15
  • 14