Reputation: 91
I'm trying to create a class that handles wx.Frame and Threading at the same time (I tried to user Visual(threadins.Thread, wx.Frame) too, but I had no success. I really wouldn't want to use wx.CallAfter and I'd like to handle it in one class only. When I run the code, everything works fine, but the Close Method crashes the programand the frame remains open. Where am I wrong?
import threading
import wx
class Visual(threading.Thread):
def __init__(self, title):
screenWidth = 500
screenHeight = 400
screenSize = (screenWidth,screenHeight)
self.app = wx.App(False)
self.frame = wx.Frame(None, wx.ID_ANY,title, size=screenSize)
panel = wx.Panel(self.frame)
self.list_ctrl = wx.ListCtrl(panel, size=(-1,100),
style=wx.LC_REPORT
|wx.BORDER_SUNKEN
)
self.list_ctrl.InsertColumn(0, 'Name', width=125)
self.list_ctrl.InsertColumn(1, 'DDD')
self.list_ctrl.InsertColumn(2, 'Phone')
self.list_ctrl.InsertColumn(3, 'Desc', width=125)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)
panel.SetSizer(sizer)
self.ready = False
threading.Thread.__init__(self)
self.frame.Bind(wx.EVT_CLOSE, self.close)
self.start()
def close(self, event):
print("Close")
self.frame.Destroy()
self.app.Destroy()
self.app.ExitMainLoop()
def run(self):
self.frame.Show(True)
self.ready = True
self.app.MainLoop()
if __name__ == '__main__':
visu = Visual(title='Test')
Thanks.
Upvotes: 1
Views: 404
Reputation: 33071
wxPython requires its main loop to be in the main thread, much like every other desktop GUI toolkit (i.e. PyQt, Tkinter, etc). You should only start threads after wxPython's main loop has been started. In other words, threads should be started from within wxPython.
wxPython has three thread-safe methods. They are as follows:
Anyway, this will almost certainly require you to have a thread class and one or more wxPython related classes. This is normal and the recommended practice. See the following links for more information:
Upvotes: 4