Wxpython Study Notes

Source: Internet
Author: User
Tags wxwidgets

First, Introduction

Wxpython is a GUI toolbox for the Python programming language. He makes it easy for Python programmers to create programs with robust, powerful graphical user interfaces. It is the Python language's binding to the popular Wxwidgets cross-platform GUI ToolPak. And Wxwidgets is written in the C + + language. Like the Python language and the wxwidgets GUI tool library, Wxpython is open source software. This means that anyone can use it for free and can view and modify its source code, or contribute patches to add functionality. The Wxpython is cross-platform. This means that the same program can run on multiple platforms without modification. Today's supported platforms are: 32-bit Microsoft Windows operating system, most Unix or Unix-like systems, Apple Mac OS X. Because of the use of Python as a programming language, Wxpython is easy to write and easy to understand.

Second, basic use

Basic use of the words to this address is very detailed, I do not need to repeat it:

http://wiki.wxpython.org/Getting%20Started

Three, the common control 1. Menus (menu)

http://wiki.wxpython.org/Getting%20Started#head-33e6dc36df2a89db146142e9a97b6e36b956875f

2. Page layout (Sizer)

This stuff is more troublesome to use, refer to the following page:

http://wiki.wxpython.org/Getting%20Started#head-7455553d71be4fad208480dffd53b7c68da1a982

WxPython frame Layout explained in detail (i)

WxPython frame Layout explained in detail (ii)

3. tab page (notebook)

Http://wiki.wxpython.org/Getting%20Started#head-b20d2fc488722cdb3f6193150293d1e118734db8

4. List control (Listctrl)

This control is more powerful and is one of the controls I prefer to use. In the "Wxpythoninaction" a book in the 13th chapter has introduced (want the book electronic version and the accompanying source of the friend can ask me to)

Here are the simple uses provided in list_report.py:

Code:listctrl

For the Listctrl control, there are several places I would like to add:

1. How do I get the selected item?

The most common method is to get the first item selected: Getfirstselected (), which returns an int, the ID of an item in Listctrl.

Another method is: getnextselected (itemid), gets the first selected item after the specified Itemid, and also returns Itemid.

With these two methods, we can iterate through all the selected items:

code:getnextselected

If you want to get the value of a column for a row, use the following method:

#获取第0行, the value of column 1th
Itemtext = Self.list.GetItem (0, 1). Text

2. How do I add a right-click menu after selecting an item?

In the __INIT__ function, add the following event bindings:

Self.list.Bind (WX. Evt_context_menu, self. OnContextMenu)

Then, add the OnContextMenu method:

OnContextMenu

5. Select File dialog box (FileDialog)

Very simple to use:

FileDialog

6. Select Folder dialog box (dirdialog)

Dirdialog

Iv. some tips 1. Set shortcut keys

For example, if you want to perform an action by F5, you can use the following method in the __INIT__ function:

ACCELTBL = wx. Acceleratortable ([WX. Accel_normal, WX. Wxk_f5, Self.btnrun.GetId ())])
Self. Setacceleratortable (ACCELTBL)

There is also a very common case, which is to press the ESC key to close the window. We know that there is a very simple way to use the SetID (Wx.id_cancel) method, such as:

Self.btncancel = wx. Button (Self.panel1,-1, ' Cancel ', WX. Point (380, 280))
Self.btncancel.SetId (Wx.id_cancel)

This way, when the ESC key is pressed, the current dialog will be closed, note! Here is said dialog, that is inherited from WX. The Window object for dialog, for WX. Frame using SetID seems to have no effect.

2. Using Timers Timer

In chapter 18 of the Wxpythoninaction, there is an example, as follows:

Wx. Timer

3. What you must know when using multi-threading: WX. Callafter

In particular, when writing multithreaded cases in Wxpython, it is important to note that WX is required when a thread notifies a Window object to update state. Callafter. The same is the case for chapter 18:

Wx. Callafter

4. Need to start another GUI program in the program without losing the focus of the main window?

Normally, it is no problem to call Os.popen to run other external programs. However, in Wxpython, WX loses the current focus, even if the program is opened as a modal dialog box. To solve this problem you can use WX's own method, WX. Execute.

Wx. Execute (' Notepad ')

V. Learning Resources

1. Official: Http://wiki.wxpython.org/FrontPage

2. Woodpecker Wiki:http://wiki.woodpecker.org.cn/moin/wxpythoninaction

Wxpython Study Notes

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.