Advanced Python Learning (ii)

Source: Internet
Author: User
Tags bitwise

This section learns the graphical user interface

------------------------

This section describes how to create a graphical user interface (GUI) of a Python program, which isa window with buttons and text boxes. Here are the WxPython :

: http://www.wxpython.org/download.php

Download the appropriate version according to your operating system.

Installation:

The installation process is very simple, take the Win7 system as an example, click on the downloaded exe file, keep the installation directory consistent with the native Python directory is OK .

Create and display a frame

Import WX  # needs to be imported WX module app = wx. APP () win = WX. Frame (None) win. Show () app. Mainloop ()   

Run:

====================================================

Let's create a text editor that requires the following features:

* It can open a text file of the given file name;

* It can edit file files

* It can save text files

* It can exit the program normally

One, set the button position

Import Wxapp = WX. APP () win = WX. Frame (none,title =  " editor " Size= (410,335 '  open  '  save  WX. HScroll) app. Mainloop ()                

Run:

It may be noted here that the position and dimensions of the control (button, input box), position and dimensions include a pair of values: The position includes the x and y coordinates, and the dimensions include the width and height.

The code is relatively simple, and the button control (open, save) is not explained. Note that the text control, the default text box (textfield), is a single line of textthat can be compiled, there is no scrollbar, in order to create a text area (text areas) as long as you use the style parameter to adjust the style, the value of the style parameter is actually an integer, but you do not have to specify it directly, you can use the bitwise OR operator or Union wx.te_multiline to get the multiline file area, as well as WX. HScroll to get the horizontal scroll bar.

Second, the Intelligent layout

Although it is easy to understand the geometric position of each component, it is tedious to adjust.

ImportWxapp =Wx. APP () win = WX. Frame (None,title ="Editor", Size= (410,335)) bkg =Wx. Panel (Win) Loadbutton = wx. Button (bkg, label =‘Open it‘) Savebutton = wx. Button (bkg, label = " save "  WX. Textctrl (bkg) contents = wx. Textctrl (bkg, style = Wx.te_multiline | WX. HScroll) hbox = WX. Boxsizer () Hbox. ADD (filename, proportion =1, flag = WX. EXPAND) Hbox. ADD (Loadbutton, proportion =0,flag = WX. Left, border = 5) Hbox. ADD (Savebutton, proportion =0,flag = WX. Left, border = 5) vbox = WX. Boxsizer (WX. VERTICAL) VBox. ADD (hbox,proportion = 0,flag = wx. EXPAND | Wx. All, border = 5) VBox. ADD (contents, proportion = 1,flag=wx. EXPAND | Wx. Left | Wx. BOTTOM | Wx. Right, border = 5) bkg. Setsizer (VBox) win. Show () app. Mainloop ()             

Running this code, the window will be the same as above, but the relative coordinates are used instead of the absolute coordinates.

Wx. the Boxsizer constructor has a parameter that determines whether it is horizontal or vertical, and the default is horizontal.

The Add method has several parameters, and theproportion parameter sets the proportions according to the space allocated when the window changes size. The flag parameter is similar to the style parameter in a constructor , and you can construct symbolic constants using bitwise OR operator joins.

Second, add event processing

The above interface is done, but we don't have any effect on clicking the button.

In GUI terminology, actions performed by the user (such as clicking a button) are called events. We need to get the program to take care of these events and respond.

Suppose you write a function that is responsible for opening the file and load it , and then you can use the function as an event handler for Loadbutton as follows:

Loadbutton.bind (WX. Evt_button, load)

  

Let's do the rest of the work, and now we need two event handlers:load ( open ) and save (save). When an event is called, it receives an event object as its only parameter, which includes information about what happened, but here you can ignore this method because the program only cares about what happens when clicked.

def Load (event):    file = open (filename. GetValue ())    contents. SetValue (File.read ())    file.close () 

The last section has just learned the opening /reading of the file , and the file name is obtained using the filename object GetValue method. In order to introduce text into the text area, use contents only. SetValue can be.

The Save function is similar to load , except that it needs to write ('w') and the Write method for the file Processing Section , GetValue is used to get information from the text area.

def Save (Event):    file = open (filename. GetValue (),'w') file.write (contents. GetValue ()) file.close ()    

Everything is available, and they are eventually assembled with the following content:

ImportWxDefLoad (event): File =Open (filename. GetValue ()) contents. SetValue (File.read ()) File.close ()DefSave (Event): File = open (filename. GetValue (),‘W‘) File.write (contents. GetValue ()) file.close () app =Wx. APP () win = WX. Frame (None,title ="Editor", Size= (410,335)) bkg =Wx. Panel (Win) Loadbutton = wx. Button (bkg, label =‘Open it‘) Loadbutton.bind (WX. Evt_button,load) Savebutton = wx. Button (bkg, label =‘ save  ") Savebutton.bind (WX. evt_button,save) filename = WX. Textctrl (bkg) contents = wx. Textctrl (bkg, style = Wx.te_multiline | WX. HScroll) hbox = WX. Boxsizer () Hbox. ADD (filename, proportion =1, flag = WX. EXPAND) Hbox. ADD (Loadbutton, proportion =0,flag = WX. Left, border = 5) Hbox. ADD (Savebutton, proportion =0,flag = WX. Left, border = 5) vbox = WX. Boxsizer (WX. VERTICAL) VBox. ADD (hbox,proportion = 0,flag = wx. EXPAND | Wx. All, border = 5) VBox. ADD (contents, proportion = 1,flag=wx. EXPAND | Wx. Left | Wx. BOTTOM | Wx. Right, border = 5) bkg. Setsizer (VBox) win. Show () app. Mainloop ()            

Run:

Here are some steps to play this little program:

1. Text box input aaa.txt

2, text area input hello.world!

3. Click Save (there will be no prompt to save the success, but it has succeeded)

4. Close the editor (just for Fun) and rerun to open.

5, File box input aaa.txt, click the "Open" button, the last edited content (Hello.world) appeared in the text area.

Advanced Python Learning (ii)

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.