PythonWxPythonBeginnersSeries

From Wiki

Jump to: navigation, search

This is the support page for Kyran Dale's wxPython Beginners Series video set. Please feel free to dip-in and extend this information.


This is the very start of a page showing other ways of making a simple GUI, first shown in this series by Kyran Dale. These improvements were made by Gerold Penz in the german forum www.python-forum.de (Original thread). Translation by User:Horstjens.

Contents

improved code for the video "A First wxPython Application"

#!/usr/bin/env python 
# -*- coding: iso-8859-1 -*- 
import wx 
class MainWindow(wx.Frame): 
    def __init__(self, parent = None, id = -1, title = "Small Editor"): 
        wx.Frame.__init__( 
            self, parent, id, title, size = (400, 200), 
            style = wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE) 
        self.control = wx.TextCtrl(self, -1, style = wx.TE_MULTILINE) 
        self.Show(True) 
app = wx.PySimpleApp() 
frame = MainWindow() 
app.MainLoop()

comments:

  • 1. Shebang (Magic Line) added
  • 2. coding added
  • 3. shorter lines
  • 4. standard-values for the method __init__ added
  • 5. The variable "id" forwarded to wx.Frame.__init__ (instead to (?) wx.ID_ANY)


improved code for the video "Adding a Menubar to Our Sample Editor"

#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-

import wx

class MainWindow(wx.Frame):
    def __init__(self, parent = None, id = -1, title = "Small Editor"):
        wx.Frame.__init__(
            self, parent, id ,title, size = (400,200),
            style = wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE
        )
        self.control = wx.TextCtrl(self, -1, style = wx.TE_MULTILINE)
        #--- START NEW CODE: ---------------------------------------------------
        self.CreateStatusBar()
        #------ Setting up the menu.
        filemenu = wx.Menu()
        filemenu.Append(-1, "&About", "Information about this program")
        filemenu.AppendSeparator()
        filemenu.Append(-1, "E&xit", "Terminate the program")
        #------ Creating the menu.
        menubar = wx.MenuBar()
        menubar.Append(filemenu, "&File")
        self.SetMenuBar(menubar)
        #--- END NEW CODE ------------------------------------------------------
        self.Show(True)

app = wx.PySimpleApp()
frame = MainWindow()
app.MainLoop()

comments:

most important change: no longer are special ID's for menu-entry's necessary. The number "-1" means that the ID will be created automatically.

improved code for the video "An Introduction to Event-handling"

#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-

import wx

class MainWindow(wx.Frame):

    def __init__(self, parent = None, id = -1, title = "Small Editor"):
        # Init
        wx.Frame.__init__(
                self, parent, id, title, size = (400,200),
                style = wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE
        )

        # TextCtrl
        self.control = wx.TextCtrl(self, -1, style = wx.TE_MULTILINE)

        # StatusBar
        self.CreateStatusBar()

        # Filemenu
        filemenu = wx.Menu()

        # Filemenu - About
        menuitem = filemenu.Append(-1, "&About", "Information about this program")
        self.Bind(wx.EVT_MENU, self.OnAbout, menuitem) # here comes the event-handler
        # Filemenu - Separator
        filemenu.AppendSeparator()

        # Filemenu - Exit
        menuitem = filemenu.Append(-1, "E&xit", "Terminate the program")
        self.Bind(wx.EVT_MENU, self.OnExit, menuitem) # here comes the event-handler

        # Menubar
        menubar = wx.MenuBar()
        menubar.Append(filemenu,"&File")
        self.SetMenuBar(menubar)

        # Show
        self.Show(True)


    def OnAbout(self,event):
        message = "A sample editor\n in wxPython"
        caption = "About Sample Editor"
        wx.MessageBox(message, caption, wx.OK)


    def OnExit(self,event):
        self.Close(True)  # Close the frame.


app = wx.PySimpleApp()
frame = MainWindow()
app.MainLoop()

# destroying the objects, so that this script works more than once in IDLEdieses Beispiel
del frame
del app

comments:

I made several comment lines in this improved example so that anyone can see better what is happening where.

The most important changes: 1.) you can bind the Event-Handler to an object and to a fitting event with the method **Bind()**. That means that you no longer have to worry about Event-ID's. In the example above are only 2 ID's, but in an outgrown program there are lots. The events are binded toward their menu-entry's right after creating of the events.

2.) Instead of wx.MessageDialog() i used wx.MessageBox(). That save the troublesome handling of the dialog. You lose some flexibility while doing so but that is no big deal.

Personal tools