尝试在另一个线程wxpython中创建对话框 [英] Trying to create a dialog in another thread wxpython

查看:136
本文介绍了尝试在另一个线程wxpython中创建对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在另一个线程中运行一个函数,该线程应该填写一个对话框然后显示它,但是只要我尝试以任何方式更改该对话框,它就只会隔离故障。我已经读到这是WxPython的常见问题,开发人员无意直接更改另一个线程中的对话框。

I'm running a function in another thread that is supposed to fill out a dialog and then show it but it just seg faults as soon as I tried to alter the dialog in any way. I've read that this is a common issue with WxPython and that devs are not intended to directly alter dialogs in another thread.

如何解决此问题?我可以在主线程中调用该函数,但这会阻塞我的GUI,这是一个冗长的初始化对话框的操作-我想避免这种情况。

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

我代码类似于下面的代码。

My code is similar to the below.

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



我正在调用的函数



The function I am calling

def init_dialog(self, arg, arg, arg....):
  dialog = MyFrame(self, "Dialog")

  # Setup the dialog
  # ....
  dialog.Show()

即使有一个空白对话框并且只是一个简单的调用以显示该函数内部,我也会遇到分段错误。

Even with a blank dialog and just a simple call to show inside the function I get a segmentation fault. Any help would be greatly appreciated, thanks.

推荐答案

我制作了一个小程序来演示在计算和调用消息时保持GUI响应的能力。计算后的方框。

I have made an applet to demonstrate keeping GUI responsive during calculations and calling the 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()

GUI内容保留在主线程中,而计算继续畅通无阻。根据需要,可以在创建对话框时获得计算结果。

GUI stuff is kept in the main thread, while calculations continue unhindered. The results of the calculation are available at time of dialog creation, as you required.

这篇关于尝试在另一个线程wxpython中创建对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆