Python learning Chapter 2-graphical user interface

Source: Internet
Author: User

1. Download and install wxPython. Note that the selected wxpython version must correspond to the python version. The specific installation process will not be described in detail.
2. Use wxPython to create a GUI application: import the wx module: import wx at the beginning. There are many ways to write wxPython programs, but the inevitable thing is to create application objects. The basic application class is called ex. App, which is responsible for all the initialization behind the scenes. The simplest wxPython program should be like the following:

If wx. App cannot work, you may need to replace it with wx. PySimpleApp.
3. Window and component: A Window is also called a Frame. It is only an example of wx. Frame. Components in the wx framework are created by their parent component as the first parameter of the constructor. If you are creating a separate window, you do not need to consider the parent part. use None. The Code is as follows:
The running result is as follows:

Besides, you must call the Show method of the window before calling app. MainLoop. Otherwise, it will be hidden all the time. To add a Button to the framework, you only need to use win as the parent parameter to instantiate wx. Button. The Code is as follows:

4. tags, titles, and locations: You can use the constructor's label parameter to set the tags when creating parts. You can also use the title parameter to set the framework title. The most practical method is to use keyword parameters for the wx constructor, so that you do not need to remember the order of parameters. Example:
The running result is as follows:

Careful readers will find that there is no button missing, but it is only overwritten. In this case, we need to use the pos and size parameters to set the position and size in the constructor. The Code is as follows:

The running result is as follows:

Positions include the x and y coordinates, while dimensions include width and height. This Code contains the wx. TextCtrl object, which is a text control. by adjusting the value of the style parameter of this object, you can change the style of the text control. The value of the style parameter is actually an integer, but you do not need to specify it directly. You can specify it by bit or in combination with a style with a special name in the wx module.
5. intelligent layout: the easiest way to layout within wx is to use sizer, And the easiest tool to use is wx. BoxSizer. The sizer manages the size of the component. You just need to add the part to the sizer, add some layout parameters, and then let the sizer manage the size of the parent component on its own. Example:

The running result of this Code is the same as the running result, but relative coordinates instead of absolute coordinates are used. The wx. BoxSizer constructor has a parameter (wx. HORIZONTAL or wx. VERTICAL) that determines whether the constructor is HORIZONTAL or VERTICAL. The default value is HORIZONTAL. The Add method has several parameters. The proportion parameter changes the size of the allocated space in the hour according to the window. For example, in horizontal BoxSizer (the first one), the filename component obtains all the extra space in the table size. If all three parts set proportion to 1, the space will be equal. You can set proportion to any number. The flag parameter is similar to the style parameter in the constructor. It can be constructed using bitwise OR connected constructor constants. Wx. the EXPAND flag ensures that the component is extended to the allocated space, while the wx. LEFT, wx. RIGHT, wx. TOP, wx. BOTTON and wx. the ALL flag determines the edge to which the border parameter is applied, and the border parameter is used to set the edge width.
6. Event Processing: the Bind method of the component can be used to link the event processing function to a given event.
Assume there is a function that is responsible for opening the file and name it load. In this case, you can use this function as the event handler for loadButton: loadButton. Bind (wx. EVT_BUTTON, load ). When you click the button, the function will be called. A symbolic constant named wx. EVT_BUTTON represents a button event. The wx framework has such event constants for various events, similar to various events in java swing. With event processing, a complete example is generated as follows:

7. Examples and comparisons of multiple GUI toolkit in python:
Use Tkinter:

Use Jython and Swing:

Author: uohzoaix

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.