Differences between revisions 40 and 41
Revision 40 as of 2007-05-04 15:30:53
Size: 7041
Editor: 87-198-29-9
Comment: Removed duplicate line
Revision 41 as of 2007-05-04 15:35:10
Size: 7040
Editor: 87-198-29-9
Comment: Fixed escape character
Deletions are marked like this. Additions are marked like this.
Line 71: Line 71:
Then experiment with changing the arguments passed to the control’s constructor until Then experiment with changing the arguments passed to the control's constructor until

How to learn wxPython.

TableOfContents

We have had a lot of questions on the newsgroup asking how to go about learning wxPython. This page gives some pointers on how to do just that.

First of all, if you are new to Python, and especially if you are new to programming in general, it makes the most sense to learn Python itself first, without any GUI stuff. This can be frustrating if your goal is to write GUI programs, and particularly if you have experience with an environment like Visual Basic in which GUI programming is integrated into the environment. It is worth it, however, to take some time to learn about Python. Python is a very powerful language capable of doing many things both with and without GUIs.

Without first understanding topics such as functions, modules, and classes, adding wxPython to the mix will only increase the confusion.

Using wxPython requires a pretty good understanding of Object Oriented (OO) programming in Python, and all of the tutorials and references that I will refer to later assume this knowledge.

Learn Python

The Python website has an excellent guide for beginners that takes you step-by-step through the process of learning the language. http://www.python.org/about/gettingstarted/

Learn wxPython

Once you have a fundamental understanding of programming in Python, you can start to learn wxPython for GUI programming. I suggest starting with the wxPython wiki ["Getting Started"] section.

Another couple of good places to look in the Wiki are the ObstacleCourse and the FullyFunctionalDemo.

There is also a very old, brief Tutorial available. You can find it here:

http://wxpython.org/tutorial.php

It can be helpful, but it is written using an outdated coding style, so read the ["wxPython Style Guide"] before viewing it.

wx.Frame, rather than the old wxFrame.

Between these two, you should have a basic grasp of how wxPython works, and be ready to make your own application. To do this, you will want two things: examples and a reference.

Choose a Good Editor

Features such as auto-completion, code highlighting, and an interactive window make for faster programming with less errors.

See http://wiki.python.org/moin/PythonEditors for a list of editors.

The Comprehensive Demo Files

The wxPython demo is your best source of examples. It is distributed with wxPython, and demonstrates virtually all the features of wxPython. When you run it, you can see how different features work, and see the source code that made it happen. Many of us look for something in the demo that is close to what we want to do, copy it, and then alter it to fit our needs.

If you can't find it in your installation: Here are the most recent versions are downloadable from SourceForge. Just get the wxPythonDemo archive from that has the same version number as your wxPython installation from here: https://sourceforge.net/project/showfiles.php?group_id=10718

Another source of examples is the ["wxPython Cookbook"].

Play with the Demo Code

Once you find a control you are interested in using, copy the demo code to your editor and delete all the unnecessary code until you find the statements that "do the work." Then experiment with changing the arguments passed to the control's constructor until you understand how it works. Only then add the control to a larger application you are developing.

Study Other People's Code

Charles Simonyi, the legendary Microsoft programmer, grew up in Hungary and had few books available to him from which to learn computer science. He was able to get the source code for a compiler and studied the printouts as musicians would study musical scores.

There are quite a few gems in the sample code that is distributed with wxPython but are not in the Demo program, e.g., FrogEdit and its underlying wx.lib.editor module. I had assumed that an editor would be a wx.TextCtrl on steroids. I was surprised to see that the text was drawn to the screen using a DC (device control) and learned some good techniques. I am also "reading" Transana and Task Coach, two sophisticated applications written in Python and wxPython, to learn how large applications are structured. There is a huge difference between the small examples on this wiki, and actual real-world code, so the process of analyzing large applications is integral to learning wxPython.

[http://ShowMeDo.com ShowMeDo.com] hosts a set of [http://showmedo.com/videos/series?name=PythonWxPythonBeginnersSeries five tutorial videos] which will be useful to those just starting with wxPython.

The API

The reference manual for wxPython is the same as the reference for the C++ version of wxWidgets, and can be found here:

[http://www.wxpython.org/onlinedocs.php]

As of this writing, that version is a bit out of date, so you can try the wxWidgets site instead:

[http://www.wxwidgets.org/manuals/2.6.2/wx_contents.html]

It is written in C++ syntax, with notes inserted when the Python implementation differs from the C++. The fact that the docs are written for C++ is unfortunate, not because they aren't useful to Python programmers, but because is can be very intimidating to new users who don't know C++. In fact, many Python programmers who don't know a bit of C++ find that the docs can be very useful. It only takes a little while to learn to translate from the C++ in the docs to the equivalent Python. One of the best ways to learn to do this is to compare code from the examples in the wxPython demo to what is in the reference manual. There is also the ["C++GuideForwxPythoneers"] page on this site to help you further.

There is also a new, automatically generated reference in Python syntax. You can find it here:

[http://www.wxpython.org/docs/api/]

I keep the wxWidgets documentation open in one window and the wxPython Demo in another as I use PythonWin to develop my programs. I often refer to these resources often and copy code into my application.

Coding Style

It will serve you well to use a nice, modern style for your wxPython code. Due to API changes over the years, and just plain experience, wxPython code style has changed quite a bit over the years, and gotten much more Pythonic.

Unfortunately, a lot of the examples you'll find have not been updated to reflect this style. As you will benefit from using good, modern, style, make sure to study ["wxPython Style Guide"] right when you get started, to establish good habits.

Live Help

The wxPython-users mailing list is famous for its helpful and friendly members. You can subscribe to it here: http://wxpython.org/maillist.php. I often find techniques that I may need for the future and save them to a special folder. After I developed some ability in wxPython, I try to solve some of the questions posed to the community before I read the answer posted by a more experienced programmer.

Comments

wxPython by Example (last edited 2012-06-28 08:10:48 by SWM-HIGH-SPEED2-22)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.