Life is really simple, but we insist on making it complicated.  -Confucius




wxSimpleDemo

wxSimpleDemo is a (you guessed it) simple demonstration of the basic components of a (simple) application in wxPython. It shows users how to set up a wx.Frame, initialize widgets and place them in sizers, and handling events all in a OOP context. In it's current state, there are few comments in the file. This is a Pythonic faux pas and a shortcoming of mine that I am going to rectify in future versions of this app.

Python (and wxPython, respectively) is an inherently readable language; I hope that you can understand what is going on in the code for the most part. I plan to write an in-depth breakdown of this file, so stay tuned for that. If you encounter any bugs or have suggestions/comments, please send me an e-mail, colin DOT barnette AT gmail

Download

The current version is 0.1.

Prerequisites

wxSimpleDemo 0.1

Screenshot

wxSimpleDemo

Source Code

#!/usr/bin/python
import wx

# Create a class called MainFrame and have it derive properties (inherit)
# from wx.Frame.
class MainFrame(wx.Frame):
    # Define our init function and require arguments that we will pass to
    # wx.Frame.__init__()
    def __init__(self, parent, ID, title, pos=wx.DefaultPosition,
                 size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE):
        
        wx.Frame.__init__(self, parent, ID, title, pos, size, style)

        label = "Click the button and change this text!\n(Or use the Menu..)"
        # Initalize some controls..
        self.panel = wx.Panel(self, -1, style=wx.BORDER_SUNKEN)
        self.text = wx.StaticText(self.panel, -1, label,
                                  style=wx.ALIGN_CENTER | wx.ST_NO_AUTORESIZE)
        self.button = wx.Button(self.panel, -1, "Click me!")
        self.panel.SetBackgroundColour(wx.WHITE)
        
        # Initialize the menubar
        menubar = wx.MenuBar()
        self.menu = wx.Menu()
        
        self.menu.Append(101, "&Edit text..", "Edit text")
        self.menu.AppendSeparator()
        self.menu.Append(102, "&Close", "Close")
        menubar.Append(self.menu, "&Menu")
        self.SetMenuBar(menubar)
        
        # Initialize the statusbar
        self.CreateStatusBar()
        self.SetStatusText("")

        # Initialize the sizers and fill them
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        st = wx.StaticText(self.panel, -1, "")
        sizer.Add(st, 1, wx.EXPAND, 0)
        sizer.Add(self.button, 0, 0, 0)
        st = wx.StaticText(self.panel, -1, "")
        sizer.Add(st, 1, wx.EXPAND, 0)

        st = wx.StaticText(self.panel, -1, "")
        vsizer = wx.BoxSizer(wx.VERTICAL)
        vsizer.Add(st, 1, wx.EXPAND, 0)
        vsizer.Add(self.text, 1, wx.EXPAND | wx.ALL, 4)
        vsizer.Add(sizer, 1, wx.EXPAND | wx.ALL, 4)

        self.panel.SetSizerAndFit(vsizer)

        # Bind our events.
        self.Bind(wx.EVT_BUTTON, self.OnButton)
        self.Bind(wx.EVT_MENU, self.OnMenu)

    def OnMenu(self, event):
        if event.GetId() == 101:
            self.ShowEditDialog()
        else:
            self.OnClose(event)

    def OnButton(self, event):
        if event.GetId() == self.button.GetId():
            self.ShowEditDialog()

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

    def ShowEditDialog(self):
        dlg = EditDialog(self, -1, "Edit the StaticText..")
        if dlg.ShowModal() == wx.ID_OK:
            self.text.SetLabel(dlg.value)
            self.SetStatusText("Label changed to \"" + dlg.value + "\".")

class EditDialog(wx.Dialog):
    def __init__(self, parent, ID, title, pos=wx.DefaultPosition, 
                 size=wx.DefaultSize, style=wx.DEFAULT_DIALOG_STYLE):
        wx.Dialog.__init__(self, parent, ID, title, pos, size, style)

        # Initialize some controls.
        self.text = wx.TextCtrl(self, -1, "")
        self.okay = wx.Button(self, wx.ID_OK)
        self.cancel = wx.Button(self, wx.ID_CANCEL)

        st = wx.StaticText(self, -1, "")
        label = wx.StaticText(self, -1, "Enter text:")

        hsizer = wx.BoxSizer(wx.HORIZONTAL)
        hsizer.Add(st, 1, wx.EXPAND, 0)
        hsizer.Add(self.okay, 0, wx.EXPAND, 0)
        hsizer.Add(self.cancel, 0, wx.EXPAND, 0)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(label, 0, wx.EXPAND | wx.ALL, 4)
        sizer.Add(self.text, 0, wx.EXPAND | wx.ALL, 4)
        sizer.Add(hsizer, 0, wx.EXPAND | wx.ALL, 4)

        self.SetSizerAndFit(sizer)
        self.Bind(wx.EVT_BUTTON, self.OnButton)

    def OnButton(self, event):
        self.value = self.text.GetValue()
        self.EndModal(event.GetId())

if __name__ == "__main__":
    app = wx.App(redirect=False)
    mainframe = MainFrame(None, -1, "wxSimpleDemo", size=(225, 200))
    mainframe.Show()
    app.MainLoop()
    

all original content copyright © colin barnette 2008-10. all rights reserved. xhtml. css.