关闭Tkinter后运行wxPython [英] Running wxPython after closing Tkinter

查看:168
本文介绍了关闭Tkinter后运行wxPython的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有两个应用程序,一个开发了一个Tkinter界面,另一个使用wxPython构建。两者都相当复杂。在完成运行Tkinter应用程序后,我想在Tkinter应用程序中选择一个按钮后运行wxPython应用程序。有没有切换事件循环,以便Tkinter应用程序可以无缝切换到wxPython GUI?



虽然以下功能在 root.destroy后工作在Tkinter应用程序中:
os.system('python wxGUI.py')



最终的程序需要捆绑到一个独立的应用程序为多个操作系统,所以这个解决方案将只有在我创建一个单独的 py2app py2exe 为wxPython应用程序调用它(这不是理想的)。

解决方案

可能最简单的方法是将wxPython放在一个单独的线程中,当你想要隐藏Tkinter应用程序调用wxPython应用程序。我只是把这个例子在一起,似乎对我有用:

  import Tkinter 
import wxapp
从线程导入导入wx

线程

########################### ###########################################
class WxThread (线程):


#--------------------------- -------------------------------------------
def __init __(自我):

线程.__初始__(自)
self.start()

#--------- -------------------------------------------------- -----------
def run(self):

app = wx.App(False)
frame = wxapp .MyFrame()
app.MainLoop()


######################## ##################################################### b class MyApp(object):


#------------------------ ----------------------------------------------
def __init __(self,parent):
构造函数
self.root = parent
self.root.title =Tkinter App

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

btn = Tkinter.Button(self.frame,text =打开wxPython应用程序,
command = self.run_wx)
btn .pack()

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

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

这是我在 wxapp.py 模块中所拥有的:

  import wx 

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


#----------------------------------------------- -----------------------
def __init __(self):
构造函数
wx。框架___(self,None,title =wxPython App)
panel = wx.Panel(self)
self.Show()

您可能需要尝试一下运行两个不同GUI工具包的主要问题之一是它们的主循环可能会相互干扰。您可能不得不使用多处理模块而不是线程模块来解决这个问题。我不太确定但是,这应该让你开始。


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

解决方案

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.

这篇关于关闭Tkinter后运行wxPython的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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