Frontend Development 14 min read

wxPython Window and Dialog Tutorial with Complete Code Samples

This article provides a comprehensive guide to creating various wxPython GUI elements—including Frame windows, toolbars, MDI and MiniFrame windows, multiple types of dialogs, and clipboard operations—complete with explanatory text, screenshots, and full Python code examples.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
wxPython Window and Dialog Tutorial with Complete Code Samples

This article provides a comprehensive guide to creating various wxPython GUI elements—including basic Frame windows, frames with toolbars and status bars, MDI and MiniFrame windows, a range of dialogs (about, message, text entry, file, font, color), and clipboard operations—accompanied by explanatory text, screenshots, and full Python code examples.

Basic Frame window

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import wx

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, u'自定义窗口', size=(300, 100))  # call parent initializer

if __name__ == '__main__':
    app = wx.PySimpleApp()
    myFrame = MyFrame()
    myFrame.Show()
    app.MainLoop()

Frame with toolbar and status bar

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import wx

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, u'自定义窗口', size=(300, 200))
        # load toolbar icons
        png_save = wx.Image('./icons/save_page.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap()
        png_home = wx.Image('./icons/go_home.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap()
        png_cfg  = wx.Image('./icons/settings.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap()
        png_forward = wx.Image('./icons/go_forward.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap()
        png_back = wx.Image('./icons/go_back.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap()
        toolbar = self.CreateToolBar(wx.TB_HORIZONTAL | wx.TB_TEXT)
        toolbar.AddSimpleTool(100, png_save, "Save page")
        toolbar.AddSimpleTool(200, png_home, "Go home")
        toolbar.AddSimpleTool(220, png_back, "Go back")
        toolbar.AddSimpleTool(230, png_forward, "Go Forward")
        toolbar.AddSimpleTool(400, png_cfg, "Settings")
        toolbar.Realize()
        self.CreateStatusBar()

if __name__ == '__main__':
    app = wx.PySimpleApp()
    myFrame = MyFrame()
    myFrame.Show()
    app.MainLoop()

MDI (Multiple Document Interface) window

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import wx

class MDIFrame(wx.MDIParentFrame):
    def __init__(self):
        wx.MDIParentFrame.__init__(self, None, -1, u"MDI窗口", size=(300,200))
        menubar = wx.MenuBar()
        menu = wx.Menu()
        menu.Append(5000, u"新建(&N)")
        menu.Append(5001, u"退出(&X)")
        menubar.Append(menu, u"文件(&F)")
        self.SetMenuBar(menubar)
        self.Bind(wx.EVT_MENU, self.OnNewWindow, id=5000)
        self.Bind(wx.EVT_MENU, self.OnExit, id=5001)

    def OnExit(self, evt):
        self.Close(True)

    def OnNewWindow(self, evt):
        win = wx.MDIChildFrame(self, -1, u"子窗口")
        win.Show(True)

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = MDIFrame()
    frame.Show()
    app.MainLoop()

MiniFrame (non‑resizable window)

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import wx

class MiniFrame(wx.MiniFrame):
    def __init__(self):
        wx.MiniFrame.__init__(self, None, -1, u'不能最小化和最大化的窗口', pos=(100,100), size=(300,200), style=wx.DEFAULT_FRAME_STYLE | wx.CLOSE_BOX)
        panel = wx.Panel(self, -1, size=(300,200))

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = MiniFrame()
    frame.Show()
    app.MainLoop()

Dialog examples (about dialog, message dialog, text entry dialog, file dialog)

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import wx

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, u"自定义对话框", size=(300,200))
        menuHelp = wx.Menu()
        aboutItem = menuHelp.Append(-1, u"关于(&A)")
        menuBar = wx.MenuBar()
        menuBar.Append(menuHelp, u"帮助(&H)")
        self.SetMenuBar(menuBar)
        self.Bind(wx.EVT_MENU, self.ShowAboutDlg, aboutItem)

    def ShowAboutDlg(self, event):
        dlg = MyDialog(self, -1, u"关于")
        dlg.ShowModal()
        dlg.Destroy()

class MyDialog(wx.Dialog):
    def __init__(self, parent, id, title):
        wx.Dialog.__init__(self, parent, id, title, size=(100,100))
        panel = wx.Panel(self)
        okBtn = wx.Button(self, wx.ID_OK, u"确定")
        okBtn.Bind(wx.EVT_BUTTON, self.CloseDlg)
        self.Show()

    def CloseDlg(self, event):
        self.Close()

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = MyFrame()
    frame.Show()
    app.MainLoop()

Clipboard operations

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import wx
from wx import xrc

class MyApp(wx.App):
    def OnInit(self):
        self.res = xrc.XmlResource('clipboard.xrc')
        self.init_frame()
        return True

    def init_frame(self):
        self.frame = self.res.LoadFrame(None, 'MyFrame2')
        self.panel = xrc.XRCCTRL(self.frame, 'm_Panel1')
        self.text1 = xrc.XRCCTRL(self.panel, 'm_textCtrl4')
        self.text2 = xrc.XRCCTRL(self.panel, 'm_textCtrl5')
        self.frame.Bind(wx.EVT_BUTTON, self.OnCopy, id=xrc.XRCID('m_button6'))
        self.frame.Bind(wx.EVT_BUTTON, self.OnPaste, id=xrc.XRCID('m_button7'))
        self.frame.Bind(wx.EVT_BUTTON, self.OnQuit, id=xrc.XRCID('m_button8'))
        self.frame.Show()

    def OnCopy(self, event):
        data = wx.TextDataObject()
        data.SetText(self.text1.GetValue())
        if wx.TheClipboard.Open():
            wx.TheClipboard.SetData(data)
            wx.TheClipboard.Close()
        else:
            wx.MessageBox('不能打开剪贴板', '错误')

    def OnPaste(self, event):
        data = wx.TextDataObject()
        success = False
        if wx.TheClipboard.Open():
            success = wx.TheClipboard.GetData(data)
            wx.TheClipboard.Close()
        else:
            wx.MessageBox('不能打开剪贴板', '错误')
        if success:
            self.text2.SetValue(data.GetText())
        else:
            wx.MessageBox('格式不匹配', '错误')

    def OnQuit(self, event):
        self.frame.Close(True)

if __name__ == '__main__':
    app = MyApp(False)
    app.MainLoop()

The article also mentions how to download and explore the official wxPython demo package, providing URLs and brief instructions for running demo scripts via a helper run.py wrapper.

GUIPythonDesktopClipboardDialogwxpython
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.