Size: 6612
Comment:
|
Size: 7112
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 141: | Line 141: |
== 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. |
How to learn wxPython.
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
If you don't know programming then this link is your best bet:
http://www.python.org/doc/Newbies.html
If you are already familiar with programming but still need to learn Python, then the Python Tutorial is a good place to start:
http://www.python.org/doc/current/tut/tut.html
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 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. It can be helpful, but it is written using an outdated coding style.
You can find it here:
http://wxpython.org/tutorial.php
Note that the big style difference is that it uses a now-depricated way of importing the wxPython modules:
from wxPython import *
You shouldn't use this anymore. Instead, use:
import wx
After that, all the wxPython stuff will be in the wx namespace and can be refered to like this:
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.
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 controls 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.
The API
The reference manual for wxPython is the same as the reference for the C++ version of wxWindows, 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.
And now a channel on irc.freenode.net has been started #python.