Well, here's the tutorial like you asked for. I've expanded on it quite a bit. Could people please read over it, in case I've made mistakes? There's a better version at my blog because it features screenshots. Link: http://fuse.baywords.com/wxpython-tutorial/
Tutorial: GUI programming with wxPython
Index
Introduction
Start
Sizers
Putting it in classes
Event handling
Binding the enter key
Creating dropdown menus
Message and file browsing dialogues
Appendix
Introduction
Hi there! So you've made a few scripts in Python. They work really well, you maybe know your way around file IO, string manipulation, and list splicing (and heck maybe even regular expressions and other cool stuff)... but you're thinking "Is that it? Surely there's more to Python." So this is it: GUI programming. Here I explain how to make the move from the command line to full-blown graphical programmes like you see in KDE, Mac or Windows.
GUI programming is fast, self-contained, and simple. So you can code your functions first, or your GUI first - it's up to you. GUI programming is like LEGO: you have a few core blocks and you build everything else from those blocks. There's not much you can't build, and once you understand how those few core blocks work, where they fit, GUI programming will become second nature. There are numerous GUI packages available to use with Python. Tkinter comes with Python, whilst Qt and GTK are used to programme KDE and Gnome for linux, respectively. Which do I suggest you use? wxPython. It's not used by Gnome, KDE, or Windows, but it is, in my opinion, the most 'Pythonic' GUI package. It's powerful and efficient. It also inherits the native look, so you don't need to worry about it not 'fitting in' or looking correct.
To start with, download wxPython here: http://www.wxpython.org/download.php
Windows, Linux/UNIX, and Mac are supported. You might like to note the words of Guido van Rossum , creator of Python:
"wxPython is the best and most mature cross-platform GUI toolkit, given a number of constraints. The only reason wxPython isn't the standard Python GUI toolkit is that Tkinter was there first."
Once you've installed that, load up IDLE or your IDE of choice, and create a new script:
Start
import wx
# always add this line, or your code won't compile - the wx module contains the GUI code you'll use
"""The start of our wxPython GUI tutorial"""
app = wx.App(redirect=False) # create a wx application
window = wx.Frame(None, title = 'Sample GUI App') # create a window
btn = wx.Button(window)
# create a button 'widget' on the window - note how the button receives the window. This creates a tree-like structure where the window is the parent node and anything else like buttons are child nodes.
window.Show() # make the frame (and hence button) visible
app.MainLoop() # keep things going
Moderator's note: Some programming editors don't like the extra long comment lines, so run the above code without them ...
import wx
"""The start of our wxPython GUI tutorial"""
app = wx.App(redirect=False)
window = wx.Frame(None, title = 'Sample GUI App')
btn = wx.Button(window)
window.Show()
app.MainLoop()
This makes a window. It then puts a button on the window. Yes, the button takes up the whole window. Have a read, understand it, fiddle if you can, then go on. Please compile this code now to confirm it runs.
Note the bit 'redirect=False'. This is so that errors go to the interpreter instead of pop up in their own window. You may want to turn this back to True (or just delete it) later on, but for now you'll likely be encountering errors that crash the programme and require you to kill it to exit - so you want a record of the error. To kill the programme when you've made an error, right-click it in the taskbar, then select close, wait a bit, then press 'End Task' at the prompt. If this isn't working, press ctrl+alt+delete, go to processes and kill pythonw.exe (make sure it's the one that's about 23mb, not the 7mb ones, which are where you're typing your code). Onwards:
import wx
"""Example with custom sizes and positions for widgets and frames."""
app = wx.App(redirect=False)
window = wx.Frame(None, title = 'Sample GUI App',
pos = (100,100), size = (400,500))
helloBtn = wx.Button(window, label = 'Hello',
pos = (400 - 60 - 15, 10), size = (60,25))
byeBtn = wx.Button(window, label = 'Bye',
pos = (400 - 120 - 15, 10), size = (60,25))
printArea = wx.TextCtrl(window,
pos = (10, 10), size = (400 - 120 - 15 - 10, 25),
style = wx.TE_READONLY)
window.Show()
app.MainLoop()
Compile and run this code first. Now look at the placement of widgets and what they are. Notice wx.TextCtrl is a text box. It's quite flexible - allowing multiline text with scroll bars, read only mode, write mode, etc. I've set it to read only for what I am about to do with it. You modify many features of a 'widget' by changing or adding things to the 'style' parameter, as above.
Also look at the position and size parameters. Fiddle with them a bit - they move and change the size of the widget. Compile and run, etc. Press the buttons. Note how they do nothing? That's because we need 'event handlers'.
Before I go on to event handlers, I'd like to make this GUI more flexible. For example, press the maximise button (top right). Notice how the programme doesn't expand to fit the screen? Let's fix that. It's fairly simple. Just add an extra 'node' in the tree; put a layer between the window frame and the widgets. It's called a 'sizer':