Professional Documents
Culture Documents
1 of 4
http://zetcode.com/wxpython/firststeps/
Home Contents
First Steps
In this part of the wxPython tutorial, we will create some simple examples.
Simple example
We start with a very simple example. Our first script will only show a small window.
Samsung Galaxy
FROM
2,500
Yezdi
FROM
20,000
Hyundai Santro
FROM
55,000
It will not do much. We will analyze the script line by line. Here is the code:
#!/usr/bin/python
# simple.py
import wx
app = wx.App()
frame = wx.Frame(None, -1, 'simple.py')
frame.Show()
app.MainLoop()
The first line is a she-bang followed by the path to a Python interpreter. The second line is a
comment. It provides a name for the script.
import wx
This line imports the basic wxPython modules. Namely the core, controls, gdi, misc and windows.
Technically wx is a namespace. All functions and objects from the basic modules will start with a
wx. prefix. The next line of code will create an application object.
app = wx.App()
Here we create a wx.Frame object. A wx.Frame widget is an important container widget. We will
analyze this widget in detail later. The wx.Frame widget is a parent widget for other widgets. It
has no parent itself. If we specify None for a parent parameter we indicate that our widget has no
parents. It is a top widget in the hierarchy of widgets. After we create the wx.Frame widget, we
must call the Show() method to actually display it on the screen.
2/7/2013 1:34 PM
2 of 4
http://zetcode.com/wxpython/firststeps/
app.MainLoop()
The last line enters the mainloop. The mainloop is an endless cycle. It catches and dispatches all
events that exist during the life of our application.
This was a very simplistic example. Despite this simplicity we can do quite a lot with this window.
We can resize the window, maximize it, minimize it. This functionality requires a lot of coding.
All this is hidden and provided by default by the wxPython toolkit. There is no reason for
reinventing the wheel.
Figure: simple.py
wx.Frame
wx.Frame widget is one of the most important widgets in wxPython. It is a container widget. It
means that it can contain other widgets. Actually it can contain any window that is not a frame or
dialog. wx.Frame consists of a title bar, borders and a central container area. The title bar and
borders are optional. They can be removed by various flags.
wx.Frame has the following constructor. As we can see, it has seven parameters. The first
parameter does not have a default value. The other six parameters do have. Those four
parameters are optional. The first three are mandatory.
wx.Frame(wx.Window parent, int id=-1, string title='', wx.Point pos = wx.DefaultPosition,
wx.Size size = wx.DefaultSize, style = wx.DEFAULT_FRAME_STYLE, string name = "frame")
#!/usr/bin/python
# nominimizebox.py
import wx
app = wx.App()
window = wx.Frame(None, style=wx.MAXIMIZE_BOX | wx.RESIZE_BORDER
| wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX)
window.Show(True)
app.MainLoop()
Our intention was to display a window without a minimize box. So we did not specify this flag in
the style parameter.
2/7/2013 1:34 PM
3 of 4
http://zetcode.com/wxpython/firststeps/
#!/usr/bin/python
# -*- coding: utf-8 -*# size.py
import wx
class Example(wx.Frame):
def __init__(self, parent, title):
super(Example, self).__init__(parent, title=title,
size=(250, 200))
self.Show()
if __name__ == '__main__':
app = wx.App()
Example(None, title='Size')
app.MainLoop()
In the constructor we set the width of the wx.Frame widget to 250px. The height of the widget to
200px.
Similarly, we can position our application on the screen. By default the window is placed in the
upper left corner of the screen. But it can differ on various OS platforms or even window
managers. Some window managers place application windows themselves. Some of them do
some optimalization, so that windows do not overlap. A programmer can position the window
programmatically. We already saw a pos parameter in the constructor of our wx.Frame widget.
By providing other than the default values, we can control the position ourselves.
Metho d
Move(wx.Point point)
Descriptio n
MoveXY(int x, int y)
SetPosition(wx.Point point)
2/7/2013 1:34 PM
4 of 4
http://zetcode.com/wxpython/firststeps/
if __name__ == '__main__':
app = wx.App()
Example(None, title='Move')
app.MainLoop()
There is one particular situation. We might want to display our window maximized. In this case,
the window is positioned at (0, 0) and takes the whole screen. wxPython internally calculates the
screen coordinates. To maximize our wx.Frame, we call the Maximize() method.
if __name__ == '__main__':
app = wx.App()
Example(None, title='Center')
app.MainLoop()
2/7/2013 1:34 PM