简体   繁体   English

关闭Tkinter后运行wxPython

[英]Running wxPython after closing Tkinter

We have two apps, one developed with a Tkinter interface and another built using wxPython. 我们有两个应用程序,一个使用Tkinter接口开发,另一个使用wxPython构建。 Both are fairly sophisticated. 两者都相当复杂。 When finished running the Tkinter app, I would like to have the wxPython app run after selecting a button in the Tkinter app. 完成运行Tkinter应用程序后,我希望在选择Tkinter应用程序中的按钮后运行wxPython应用程序。 Is there away to switch event loops so that the Tkinter app can switch to the wxPython GUI seamlessly? 是否可以切换事件循环,以便Tkinter应用程序可以无缝切换到wxPython GUI?

While the following does work after root.destroy in the Tkinter app: os.system('python wxGUI.py') 虽然以下在Tkinter应用程序中的os.system('python wxGUI.py')之后root.destroyos.system('python wxGUI.py')

The final program needs to be bundled into a standalone app for multiple operating systems, so this solution would only work if I create a separate py2app or py2exe for the wxPython app and call it this way (which is not ideal). 最终的程序需要捆绑到一个独立的应用程序中,用于多个操作系统,所以这个解决方案只有在我为wxPython应用程序创建一个单独的py2apppy2exe并以这种方式调用它时才会起作用(这不是理想的)。

Probably the simplest way to accomplish this would be to put wxPython into a separate thread and just hide the Tkinter app when you want to call the wxPython app. 可能最简单的方法是将wxPython放入一个单独的线程中,当你想调用wxPython应用程序时隐藏Tkinter应用程序。 I just whipped this example together and it seemed to work for me: 我只是把这个例子一起鞭打起来,它似乎对我有用:

import Tkinter
import wxapp
import wx

from threading import Thread

########################################################################
class WxThread(Thread):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """"""
        Thread.__init__(self)
        self.start()

    #----------------------------------------------------------------------
    def run(self):
        """"""
        app = wx.App(False)
        frame = wxapp.MyFrame()
        app.MainLoop()


########################################################################
class MyApp(object):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        self.root = parent
        self.root.title = "Tkinter App"

        self.frame = Tkinter.Frame(parent)
        self.frame.pack()

        btn = Tkinter.Button(self.frame, text="Open wxPython App",
                             command=self.run_wx)
        btn.pack()

    def run_wx(self):
        self.root.withdraw()
        thread = WxThread()
        thread.join()
        self.root.deiconify()

#----------------------------------------------------------------------
if __name__ == "__main__":
    root = Tkinter.Tk()
    root.geometry("800x600")
    app = MyApp(root)
    root.mainloop()

This is what I had in the wxapp.py module: 这是我在wxapp.py模块中的内容:

import wx

########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="wxPython App")
        panel = wx.Panel(self)
        self.Show()

You might have to experiment a bit as one of the main issues with running two different GUI toolkits is that their main loops can interfere with each other. 您可能需要进行一些实验,因为运行两个不同的GUI工具包的主要问题之一是它们的主循环可能会相互干扰。 You may have to use the multiprocessing module instead of the threading module to get around that. 您可能必须使用多处理模块而不是线程模块来解决这个问题。 I'm not really sure. 我不太确定。 But this should get you started anyway. 但无论如何,这应该让你开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM