The Wxpython Tutorial
Total Page:16
File Type:pdf, Size:1020Kb
The wxPython tutorial http://www.zetcode.com/wxpython/ Home PyQt4 tutorial The wxPython tutorial wxPython Coding Tools Debugger, Browser, Editor, and more Take a Introduction Test Flight Today! First Steps www.wingware.com Menus and Toolbars Layout Management PDF & Image to PDF/A Events convert, correct, OCR, compress Dialogs ISO compliant Widgets Documents, fast www.detec.com Advanced Widgets Drag and Drop Embedded Java in18 Runtimes Standard & Databases Custom VMs & Skeletons Profiles OEM Licensing from Custom widgets www.microdoc.com Using xml resource files GDI C++ Tutorial 1000's of Great Tips and Tricks Programmers Gripts Bidding Fast & Simple Project The Tetris game Outsourcing. www.GetACoder.com The wxPython tutorial is the largest and most advanced wxPython tutorial available on the Internet. Suitable for beginners and intermediate programmers. ZetCode:: last modified November 25, 2007 (tetris) © 2007 - 2008 Jan Bodnar 1 de 1 27/04/2008 0:58 Introduction to wxPython http://www.zetcode.com/wxpython/introduction/ wxPython IDE C++ Tutorial Editor,Home Debugger, Contents Browser, and more Free 1000's of Great Programmers Bidding Fast & 30-day Trial Simple Project Outsourcing. Introduction to wxPython An Application An application is a computer program that performs a specific task or a group of tasks. Web browser, media player, word processor are examples of typical applications. A term tool or utility is used for a rather small and simple application that performs a single task. A unix cp program is an example of a such a tool. All these together form computer software. Computer software is the broadest term used to describe the operating system, data, computer programs, applications, mp3 files or computer games. Applications can be created for four different areas. Online shopping applications, wikis, weblogs are examples of popular web applications. They are accessed with a web browser. Examples of desktop applications include Maya, Opera, Open Office or Winamp. Enterprise computing is a specific area. Applications in these area are complex and large. Applications created for portables include all programs developed for mobile phones, communicators, pda's and similar. Programming languages There are currently several widely used programming languages. The following list is based on the TIOBE Programming Community Index. The numbers are from May 2007. Position Language Ratings 1 de 9 27/04/2008 0:59 Introduction to wxPython http://www.zetcode.com/wxpython/introduction/ 1 Java 19.1% 2 C 15.2% 3 C++ 10.1% 4 PHP 8.7% 5 Visual Basic 8.4% 6 Perl 6.2% 7 Python 3.8% 8 C# 3.7% 9 JavaScript 3.1% 10 Ruby 2.6% Java is the most widely used programming language. Java excels in creating portable mobile applications, programming various appliances and in creating enterprise applications. Every fourth application is programmed in C/C++. They are standard for creating operating systems and various desktop applications. C/C++ are the most widely used system programming languages. Most famous desktop applications were created in C++. May it be MS Office, Macromedia Flash, Adobe Photoshop or 3D Max. These two languages also dominate the game programming business. PHP dominates over the Web. While Java is used mainly by large organizations, PHP is used by smaller companies and individuals. PHP is used to create dynamic web applications. Visual Basic is mainly used in RAD. RAD stands for rapid application development. Perl, Python and Ruby are the most widely used scripting languages. They share many similarities. They are close competitors. The time of the C# has still not come yet. It was planned to be the next big language. Javascript is a client side programming language, which runs in a browser. It is a de facto standard language and has no competition in it's area. Python Python is a successful scripting language. It was initially developed by Guido van Rossum. It was first 2 de 9 27/04/2008 0:59 Introduction to wxPython http://www.zetcode.com/wxpython/introduction/ released in 1991. Python was inspired by ABC and Haskell programming languages. Python is a high level, general purpose, multiplatform, interpreted language. Some prefer to call it a dynamic language. It is easy to learn. Python is a minimalistic language. One of it's most visible features is that it does not use semicolons nor brackets. Python uses intendation instead. The most recent version of python is 2.5, which was released in September 2006. Today, Python is maintained by a large group of volunteers worldwide. For creating graphical user interfaces, python programmers can choose among three decent options. PyGTK, wxPython and PyQt. The offcial python "toolkit" is TkInter. It is slow, looks terrible on all platforms, it has not been updated for ages. It also depends on Tcl (the tcl language must be included), which is odd. All of the above mentioned toolkits are superior to it. It remains a mystery, why it was not excluded years ago. wxPython wxPython is a cross platform toolkit for creating desktop GUI applications. With wxPython developers can create applications on Windows, Mac and on various Unix systems. wxPython is a wrapper around wxWidgets, which is a mature cross platform C++ library. wxPython consists of the five basic modules. 3 de 9 27/04/2008 0:59 Introduction to wxPython http://www.zetcode.com/wxpython/introduction/ Controls module provides the common widgets found in graphical applications. For example a Button, a Toolbar, or a Notebook. Widgets are called controls under Windows OS. The Core module consists of elementary classes, that are used in development. These classes include the Object class, which is the mother of all classes, Sizers, which are used for widget layout, Events, basic geometry classses like Point and Rectangle. The Graphics Device Interface (GDI) is a set of classes used for drawing onto the widgets. This module contains classes for manipulation of Fonts, Colours, Brushes, Pens or Images. The Misc module contains of various other classes and module functions. These classes are used for logging, application configuration, system settings, working with display or joystick. The Windows module consists of various windows, that form an application. Panel, Dialog, Frame or Scrolled Window. wxPython API wxPython API is a set of functions and widgets. Widgets are essential building blocks of a GUI application. Under Windows widgets are calles controls. We can roughly divide programmers into two groups. They code applications or libraries. In our case, wxPython is a library that is used by application programmers to code applications. Technically, wxPython is a wrapper over a C++ GUI API called wxWidgets. So it is not a native API. e.g. not written directly in Python. The only native GUI library for an interpreted language that I know is Java's Swing library. In wxPython we have lot's of widgets. These can be divided into some logical groups. Base Widgets These widgets provide basic functionality for derived widgets. They are called ancestors. They are usually not used directly. 4 de 9 27/04/2008 0:59 Introduction to wxPython http://www.zetcode.com/wxpython/introduction/ Top level Widgets These widgets exist independently of each other. Containers Containers contain other widgets. Dynamic Widgets These widgets can be edited by users. Static Widgets These widgets display informatin. They cannot be edited by user. Other Widgets These widgets implement statusbar, toolbar and menubar in an application. 5 de 9 27/04/2008 0:59 Introduction to wxPython http://www.zetcode.com/wxpython/introduction/ Inheritance There is a specific relation among widgets in wxPython. This relation is developed by inheritance. The inheritance is a crucial part of the object oriented programming. Widgets form a hierarchy. Widgets can inherit functionality from other widgets. Existing classes are called base classes, parents, or ancestors. The widgets that inherit we call derived widgets, child widgets or descendants. The terminology is borrowed from biology. Inheritance of a button Say we use a button widget in our application. The button widget inherits from 4 different base classes. The closest class is the wx.Control class. A button widget is a kind of a small window. All widgets that appear on the screen are windows. Therefore they inherit from wx.Window class. There are objects that are invisible. Examples are sizers, device context or locale object. There are also classes that are visible but they are not windows. For example a color object, caret object or a cursor object. Not all widgets are controls. For example wx.Dialog is not a kind of control. The controls are widgets 6 de 9 27/04/2008 0:59 Introduction to wxPython http://www.zetcode.com/wxpython/introduction/ that are placed on other widgets called containers. That's why we have a separate wx.Control base class. Every window can react to events. So does the button widget. By clicking on the button, we launch the wx.EVT_COMMAND_BUTTON_CLICKED event. The button widget inherits the wx.EvtHandler via the wx.Window class. Each widget that reacts to events must inherit from wx.EvtHandler class. Finally all objects inherit from wx.Object class. This is the Eve, mother of all objets in wxPython. Setting up wxPython In this section we will show how to set up wxPython library. The process is very easy provided you have the right OS. Setting up wxPython on Windows XP. We can download binaries from the wxPython official web site. We must choose the right binaries depending on our python version. There are two basic options. win32-unicode win32-ansi Usually the unicode version is the choice. Unicode version supports other languages than english. The installer automatically finds the path to the python interpreter. The only thing we must do, is to check that we agree with the licence.