I am trying to create a very simple wxPython GUI which monitors external data and displays a button Monitoring turns on / off when Monitoring is turned on, GUI updates some wx StaticLabels with real-time data. When Monitoring is OFF, GUI idles
The way I tried to make it was with a fairly simple Python thread layout when the 'Start Monitoring' button is clicked, the program produces a thread that provides real time information Updates labels with When the 'stop monitoring' button is clicked, the thread .join () is called, and it should be closed.
The startup works and updating real-time data works great, but when I click 'Stop', the whole program freezes I get it on Windows 7 64-bit I'm running, so I've always been "responding to Windows Directive".
This is the relevant code:
class MonGUI (wx.Panel): def __init __ (self, parent): wx.panel.__ init __ (self, Parent) ... ... Create other code for GUI here ... ... # Create the thread that updates VFO information yourself.monThread = Thread (none, target = self.monThreadWork) self.monThread.daemon = True self.runThread = False def monThreadWork (self): while self.runThread: ... ... update StaticLabels with information ... (This part is working) ... Monitor when pressed # button On / Off Def OnClick (self, event): If self.isMonitoring: self.button.SetLab El ("Start monitoring") self.isMonitoring = False self.runThread = False self.monThread.join () Other: self.button.SetLabel ("Stop Monitoring") self.isMonitoring = True # Start the Monitor thread! Self.RunThread = true self.monThread.start ()
I'm sure there is a better way to do this, but I am quite new to GUI programming and Python threads, and < / P>
So, why stop the thread from stopping the whole thing by clicking on the button?
This is likely to hang, which blocks the calling thread to the thread whose joins ()
method is terminated - either normally or without exception - or until the timeout is out of date.
Do you have some internal loop in your thread, or a blocking call that waits for some source of data that can never come? When I wrote a basic serial program that caught the comm port data, it sometimes hangs because my thread reading work is blocked until it is anything.
I do some debugging edit: I also use a boolean flag instead , Such as: It should not be done in different ways, but it is a little safer way to do this. print <
in #init code ... self.runThread = threading.Event () # when the thread starts ... self.runThread.set () self.monThread.start () # In thread While the self Robtread.iset (): Passed #things kill the thread ... self Rethhed.Coller () should itself .monThread.join ()
Comments
Post a Comment