Creating a gui in Python with WxWidgets [tutorial for beginners]

Standard

Python programming is a lot of fun and sometimes we want to create a graphical application (GUI). WxWidgets can be used to create a graphical interface that looks like a native application on any operating system. First download and install WxPython, the Python bindings for wxWidgets.

Then install a GUI creator called wxglade:

Using a GUI builder such as wxGlade will save you a lot of time, regardless of the GUI library you use. You can easily make complex graphical interfaces because you can simply drag and drop.

Creating our first GUI with Python and wxWidgets:

Start wxglade. You will see its user interface:

wxglade

Press on tiny window on the top left, below the file icon.

wxglade frame

Press OK.  An empty window will now appear.  Press on the tiny [OK] button in the wxGlade panel and press on the frame. The button will now appear. Press on Application in the tree window.

wxglade2

Set the output file in the wxproperties window.

wxglade3

If you look at the window note you can select multiple programming languages and two versions of wxWidgets. Select Python and wxWidgets 2.8.  Finally press Generate code. (Do NOT name the file wx.py because the import needs wx, save it as window.py or something else).

Putting it all together:
Run:

And a window with a button will appear. Pressing the button will not do anything. To start a function when pressing the button, we need to define a so called Callback. This can be as simple as:

Finally we bind the button to the callback function using:

Pressing the button will now write a message to the command line. Instead of the boring command line message, we want to show a message box. This can be done using this command:

The full code below:

wxWidgets gui python

 

Leave a Reply