We will start from a simple wxPython GUI, which looks as follows:
Embed_tkinter.py
Next, we will try to invoke a simple tkinter GUI.
The following is the entire code to do this in a simple, non-OOP way:
#=============================================================
def tkinterApp():
import tkinter as tk
from tkinter import ttk
win = tk.Tk()
win.title("Python GUI")
aLabel = ttk.Label(win, text="A Label")
aLabel.grid(column=0, row=0)
ttk.Label(win, text="Enter a name:").grid(column=0, row=0)
name = tk.StringVar()
nameEntered = ttk.Entry(win, width=12, textvariable=name)
nameEntered.grid(column=0, row=1)
nameEntered.focus()
def buttonCallback():
action.configure(text='Hello ' + name.get())
action = ttk.Button(win, text="Print", command=buttonCallback)
action.grid(column=2, row=1)
win.mainloop()
#=============================================================
import wx
app = wx.App()
frame = wx.Frame(None, -1, "wxPython GUI", size=(270,180))
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)
textBox = wx.TextCtrl(frame, size=(250,50), style=wx.TE_MULTILINE)
def tkinterEmbed(event):
tkinterApp()
button = wx.Button(frame, label="Call tkinter GUI", pos=(0,60))
frame.Bind(wx.EVT_BUTTON, tkinterEmbed, button)
frame.Show()
#======================
# Start wxPython GUI
#======================
app.MainLoop()
Running the preceding code starts a tkinter GUI from our wxPython GUI after clicking the wxPython button widget. We can then enter text into the tkinter textbox and, by clicking its button, the button text gets updated with the name:
Embed_tkinter.py
After starting the tkinter event loop, the wxPython GUI is still responsive because we can type into the TextCtrl widget while the tkinter GUI is up and running.
We can also create several tkinter GUI instances by clicking the wxPython GUI button several times. We cannot, however, close the wxPython GUI while any tkinter GUIs are still running. We have to close them first:
Embed_tkinter.py