How to do it...

When we created an instance of a wxPython GUI from our tkinter GUI, we could no longer use the tkinter GUI controls until we closed the one instance of the wxPython GUI. Let's improve on this now.

Our first attempt might be to use threading from the tkinter button callback function.

For example, our code might look as follows:

def wxPythonApp(): 
import wx
app = wx.App()
frame = wx.Frame(None, -1, "wxPython GUI", size=(200,150))
frame.SetBackgroundColour('white')
frame.CreateStatusBar()
menu= wx.Menu()
menu.Append(wx.ID_ABOUT, "About", "wxPython GUI")
menuBar = wx.MenuBar()
menuBar.Append(menu,"File")
frame.SetMenuBar(menuBar)
frame.Show()
app.MainLoop()

def tryRunInThread():
runT = Thread(target=wxPythonApp)
runT.setDaemon(True)
runT.start()
print(runT)
print('createThread():', runT.isAlive())

action = ttk.Button(win, text="Call wxPython GUI", command=tryRunInThread)

At first, this seems to be working, which would be intuitive, as the tkinter controls are no longer disabled and we can create several instances of the wxPython GUI by clicking the button. We can also type into the wxPython GUI and select the other tkinter widgets:

Control_Frameworks_NOT_working.py

However, once we try to close the GUIs, we get an error from wxWidgets, and our Python executable crashes:

In order to avoid this, instead of trying to run the entire wxPython application in a thread, we can change the code to make only the wxPython app.MainLoop run in a thread:

def wxPythonApp(): 
import wx
app = wx.App()
frame = wx.Frame(None, -1, "wxPython GUI", size=(200,150))
frame.SetBackgroundColour('white')
frame.CreateStatusBar()
menu= wx.Menu()
menu.Append(wx.ID_ABOUT, "About", "wxPython GUI")
menuBar = wx.MenuBar()
menuBar.Append(menu,"File")
frame.SetMenuBar(menuBar)
frame.Show()

runT = Thread(target=app.MainLoop)
runT.setDaemon(True)
runT.start()
print(runT)
print('createThread():', runT.isAlive())

action = ttk.Button(win, text="Call wxPython GUI", command=wxPythonApp)
action.grid(column=2, row=1)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.119.170