wxPython:从另一个wxApp调用一个wxApp

发布于 2024-12-14 07:00:12 字数 1162 浏览 0 评论 0原文

是否可以从另一个 wxApp 运行一个 wxApp? 我试图简单地从另一个 wxApp 的方法调用我编写的程序(称为 DataDeck),就像它是一个插件一样。

类似于:

def on_datadeck_btn_click(self, event):
        import datadeck.main
        datadeck.main.run()
        event.Skip()

其中 datadeck.main.run() 是 wxApp 的经典启动:

def run():
    app = DataDeck(0)
    app.SetAppName("DataDeck")
    app.MainLoop()

现在,它第一次正确打开 DataDeck 并且可以工作,但在我关闭 DataDeck 后它不会再次重新打开 DataDeck。这会冻结一切。

更新:基于@Mike Driscoll 答案,我详细记录了自己并得出以下解决方案:

我在 datadeck 中添加了一个“入口点”

def run_as_plugin():
    #[do some stuff related to XRC layouts and sysout redirection]
    MainGUI = datadeck.gui.maingui.MainGUI()

,其中 MainGUI() 的构造函数自动显示wxFrame。现在我的应用程序的行为就像调用者 wxApp 的一个组件一样。 因此,我将申请方法修改如下:

def on_datadeck_btn_click(self, event):
    import datadeck.main
    datadeck.main.run_as_plugin()
    event.Skip()

很简单,确实如此!我只需要修改处理标准输出重定向的对象(不是这个问题的一部分,我省略细节),一切工作正常。

Is it possible to run a wxApp from another wxApp?
I am trying to simply call a program I wrote (called DataDeck) from a method of another wxApp, like it was a plugin.

something like:

def on_datadeck_btn_click(self, event):
        import datadeck.main
        datadeck.main.run()
        event.Skip()

where datadeck.main.run() is a classic start of a wxApp:

def run():
    app = DataDeck(0)
    app.SetAppName("DataDeck")
    app.MainLoop()

Right now, it correctly opens DataDeck the first time and it works, but it won't reopen DataDeck a second time after I close it. This would freeze everything.

Update: based on @Mike Driscoll answer, I documented myself more and came to the following solution:

I added an "entry point" in datadeck

def run_as_plugin():
    #[do some stuff related to XRC layouts and sysout redirection]
    MainGUI = datadeck.gui.maingui.MainGUI()

Where the constructor of MainGUI() automatically shows the wxFrame. Now my application behaves like it was a component of the caller wxApp.
Therefore, I modify the application method as follows:

def on_datadeck_btn_click(self, event):
    import datadeck.main
    datadeck.main.run_as_plugin()
    event.Skip()

It was very simple, indeed! I just had to modify my objects that deal with stdout redirection (not part of this question, I omit the details), and everything worked fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以歌曲疗慰 2024-12-21 07:00:12

应该只在wx.App上有。根据我在网上阅读的内容,您不能在一个脚本中运行两个 wx.App 对象。不过,您可能可以使用 subprocess 模块来打开一个新进程。查看 Editra,了解一些如何使用插件的示例。它包含在 wxPython 中,或者您可以单独下载。

There should only be on wx.App. From what I've read online, you can't have two wx.App objects running in one script. You could probably do it using the subprocess module to open a new process though. Take a look at Editra to see some examples for how to do plugins. It is included with wxPython or you can download it separately.

紫南 2024-12-21 07:00:12

这是完全可行的。不知道为什么它不适合你。
这个例子工作得很好:

--main.py--

import wx

class MainFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, title='Main', size=(353,270))
        button= wx.Button(self, -1, 'call app', pos=(10,10), size=(-1,30))
        self.Bind(wx.EVT_BUTTON, self.capp, button)

    def capp(self, event):
        import datadeck
        datadeck.run()

if __name__ == '__main__':
    app = wx.App(0)
    frame = MainFrame(None)
    frame.Show()
    app.MainLoop()

--datadeck.py--

import wx

class DDFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, title='DDFrame', size=(353,270))
        button = wx.Button(self, -1, 'print something', pos=(100,100), size=(-1,30))
        self.Bind(wx.EVT_BUTTON, self.say_hello, button)

    def say_hello(self, event):
        print 'something'

class DataDeck(wx.App):
    def OnInit(self):
        frame = DDFrame(None)
        frame.Show()
        return True

def run():
    app = DataDeck(1)
    app.SetAppName("DataDeck")
    app.MainLoop()

如果你按下“调用应用程序”按钮,你就会打开新的框架。您可以打开任意多个。
创建的应用程序/框架是相互独立的。您可以关闭其中任何一个而不影响其他的。并且系统不会冻结。

It is perfectly feasible. Not sure why it doesnt work for you.
This example works perfectly:

--main.py--

import wx

class MainFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, title='Main', size=(353,270))
        button= wx.Button(self, -1, 'call app', pos=(10,10), size=(-1,30))
        self.Bind(wx.EVT_BUTTON, self.capp, button)

    def capp(self, event):
        import datadeck
        datadeck.run()

if __name__ == '__main__':
    app = wx.App(0)
    frame = MainFrame(None)
    frame.Show()
    app.MainLoop()

--datadeck.py--

import wx

class DDFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, title='DDFrame', size=(353,270))
        button = wx.Button(self, -1, 'print something', pos=(100,100), size=(-1,30))
        self.Bind(wx.EVT_BUTTON, self.say_hello, button)

    def say_hello(self, event):
        print 'something'

class DataDeck(wx.App):
    def OnInit(self):
        frame = DDFrame(None)
        frame.Show()
        return True

def run():
    app = DataDeck(1)
    app.SetAppName("DataDeck")
    app.MainLoop()

if you press the 'call app' button you get the new frame open. And you can open as many as you want.
Created aplications/frames are independent of each other. You can close any of them without affecting the others. And the system doesnt freeze.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文