Trying to create a dialog in another wxpython thread

I run the function in another thread, which should populate the dialog and then show it, but these are just seg faults, as soon as I tried to change the dialog in any way. I read that this is a common problem with WxPython and that developers are not intended to directly modify dialogs in another thread.

How do I get around this? I can just call a function in my main thread, but it will block my GUI and it is a long operation to initialize the dialog box - I would like to avoid this.

My code is similar to below.

In the main thread

# Create the dialog and initialize it thread.start_new_thread(self.init_dialog, (arg, arg, arg...)) 

Function i call

 def init_dialog(self, arg, arg, arg....): dialog = MyFrame(self, "Dialog") # Setup the dialog # .... dialog.Show() 

Even with an empty dialog and a simple call displayed inside the function, I get a segmentation error. Any help would be greatly appreciated, thanks.

+4
source share
1 answer

I made an applet to demonstrate that the GUI responds during calculations and brings up a message box after the calculations.

  import wx import threading import time class TestFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, -1, "I am a test frame") self.clickbtn = wx.Button(self, label="click me!") self.Bind(wx.EVT_BUTTON, self.onClick) def onClick(self, event): self.clickbtn.Destroy() self.status = wx.TextCtrl(self) self.status.SetLabel("0") print "GUI will be responsive during simulated calculations..." thread = threading.Thread(target=self.runCalculation) thread.start() def runCalculation(self): print "you can type in the GUI box during calculations" for s in "1", "2", "3", "...": time.sleep(1) wx.CallAfter(self.status.AppendText, s) wx.CallAfter(self.allDone) def allDone(self): self.status.SetLabel("all done") dlg = wx.MessageDialog(self, "This message shown only after calculation!", "", wx.OK) result = dlg.ShowModal() dlg.Destroy() if result == wx.ID_OK: self.Destroy() mySandbox = wx.App() myFrame = TestFrame() myFrame.Show() mySandbox.MainLoop() 

The GUI file is stored in the main thread, and calculations continue unhindered. The calculation results are available at the time of creating the dialogue, as you needed.

+2
source

All Articles