【问题标题】:wxPython: How do you start a wxPython application from a wxPython application?wxPython:如何从 wxPython 应用程序启动 wxPython 应用程序?
【发布时间】:2014-12-09 13:04:36
【问题描述】:

我正在尝试启动一个 wxPython 应用程序,但我希望在启动之前显示一个横幅。

这样做的一种方法是启动一个 wxPython 应用程序,然后启动另一个 wxPython 应用程序,这样做的原因是因为第二个 wxPython 应用程序的 App 部分需要在启动之前进行一些处理,并且可能需要一些时间时间。

问题是如何启动另一个应用程序并知道它已经启动?

目前我这样做会阻塞整个 GUI 会话:

subprocess.check_output(["python", "src/gui.py"], stderr=subprocess.STDOUT, shell=True)

我已尝试执行以下操作,但第一个应用程序的框架似乎没有关闭:

loadCompleted, EVT_LOAD_COMPLETED = wx.lib.newevent.NewEvent()
class MyRegion(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, title="My Region")
        self.label = wx.StaticText(self, label="Hello, World!")
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.label, 0, wx.ALL, 5)
        self.SetSizer(sizer)
        self.startThread = Thread(target=self.Start)
        self.startThread.start()
        self.Bind(EVT_LOAD_COMPLETED, self.OnClose)

    def OnClose(self, result):
        self.Close()

    def Start(self):
        try:
            subprocess.check_output(["python", "src/gui.py"], stderr=subprocess.STDOUT, shell=True)
        except:
            pass
        wx.PostEvent(self, loadCompleted(result=(None)))

if __name__ == "__main__":
    app = wx.App()
    frame = MyRegion(None)
    frame.Show()
    app.MainLoop()

【问题讨论】:

    标签: python-2.7 wxpython wxwidgets


    【解决方案1】:

    我不认为启动第二个 wxPython 应用程序是可行的方法。相反,我只需在框架的 __init__ 方法中加载横幅,然后进行处理。处理完成后,您可以销毁横幅并显示您的主应用程序。这是一些伪代码:

    #
    class MyFrame(wx.Frame):
        """"""
    
        #----------------------------------------------------------------------
        def __init__(self):
            """Constructor"""
            wx.Frame.__init__(self, None, title="Test")
    
            banner = MyBanner()
    
            # do a bunch of stuff
    
            banner.Destroy() # or banner.Close()
    
            self.Show()
    

    现在,如果处理需要很长时间,您可以将其放入线程中,并让线程向 UI 发送一条消息,告诉它线程已完成。当应用程序收到消息时,它可以关闭处理程序中的横幅并在此时显示应用程序。请注意,您需要使用线程安全的方法,例如wx.CallAfterwx.PostEvent

    查看以下文章以获取想法:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多