Running wxPython after closing Tkinter

▼魔方 西西 提交于 2019-12-21 19:58:47

问题


We have two apps, one developed with a Tkinter interface and another built using 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. Is there away to switch event loops so that the Tkinter app can switch to the wxPython GUI seamlessly?

While the following does work after root.destroy in the Tkinter app: os.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).


回答1:


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. 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:

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. 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.



来源:https://stackoverflow.com/questions/31016252/running-wxpython-after-closing-tkinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!