<< Basic Python Tutorials >> Learning Notes | 12th Chapter | Graphical user interface

Source: Internet
Author: User

Python supports a very great number of toolkits. But not a toolkit that is considered standard. The user chooses a greater degree of freedom. This chapter focuses on the most mature cross-platform Toolkit Wxpython. Official documentation: http://wxpython.org/

------

a rich platform :


Tkinter is actually similar to the standard because it is used in most formal Python GUI programs. And it is part of the Windows Binary publishing release.

But on Unix you have to compile the installation yourself.

There is also an increasingly popular tool for Wxpython.

This is a mature and feature-rich package, also the father of Python, Guido van Rossum's favorite

------

Download and install Wxpython

http://wxpython.org/download.php

Note:

1. Select the python version number that corresponds to the binary version number of your PC

2. After downloading Wxpython, it is highly recommended to download the demo version number (demo), the Demo sample program is very specific

------

Create a demo sample GUI program

The simplest demo sample program is as follows:

>>> Import WX #导入模块
>>> app = wx. APP () #初始化主要的引用程序类
>>> app. Mainloop () #wx中的包方法大写开头, contrary to Python's habits

Forms and Components

The form is also known as the frame, which is WX. An instance of the frame class. The parts in the WX framework are created by their parent part as the first parameter of the constructor. Assuming that a separate form is being created, there is no need to consider the parent part and use none.

and call the app. Mainloop need to call the form before the

Show method-otherwise it will remain hidden.

    • Program Listing 1:

Import Wxapp = wx. APP () win = WX. Frame (None) win. Show () app. Mainloop ()


    • Listing 2: Adding a button to a frame

#在框架上加入button也非常easy, just use win as the parent parameter to instantiate WX. button to do it.

Import Wxapp = wx. APP () win = WX. Frame (None) btn = wx. Button (Win) win. Show () app. Mainloop ()


    • Listing 3: Adding tags and titles using keyword parameters
Import Wxapp = wx. APP () win = WX. Frame (none,title= "Simple Editor") Loadbutton = wx. Button (win,label= ' Open ') Savebutton = wx. Button (win,label= ' Save ') win. Show () app. Mainloop ()


It seems that a button has been lost and is actually hidden. If you want to show it, the post,size inside will have to be used.

    • Program Listing 4: Setting the button position
Import Wxapp = wx. APP () win = WX. Frame (none,title= "Simple Editor", size= (410,335)) win. Show () #位置坐标, the value of x and y Loadbutton = wx. Button (win,label= ' Open ', pos= (225,5), size= (80,25)) Savebutton = WX. Button (win,label= ' Save ', pos= (315,5), size= (80,25)) #文本框 (WX. The Textctrl object), which is editable by default. No scroll bar filename   = wx. Textctrl (win,pos= (5,5), size= (210,25)) Contents   = wx. Textctrl (win,pos= (5,35), size= (390,260), Style=wx.te_multiline | wx. HScroll) #style參数, the ability to specify #wx.te_multiline vertical scroll bar #wx using a bitwise OR or joint style with a special name in the WX module. HScroll      horizontal scroll bar app. Mainloop ()


------

Smarter layouts

The simplest way to lay out in WX is to use a sizing device (sizer), the easiest tool to use is WX. Boxsizer. The dimension device manages the dimensions of the component. Just add the parts to the dimension, plus some layout parameters. Then let the sizing device manage the dimensions of the parent component itself. In the example above, you need to add a background component (WX. Panel). Create some nested WX. Boxsizer, and then use the Setsizer method of the panel to set its sizing device.

    • Listing 5: Using a sizing device
#代码的执行效果和上例同样, simply import Wxapp = WX with relative coordinates instead of absolute coordinates. APP () win = WX. Frame (none,title= "Simple Editor", size= (410,335)) bkg = WX. Panel (Win) Loadbutton = wx. Button (bkg,label= ' Open ') Savebutton = wx. Button (bkg,label= ' Save ') filename = wx. Textctrl (bkg) contents = wx. Textctrl (Bkg,style=wx.te_multiline | wx. HScroll) #wx. Boxsizer two parameters Wx.horizontal (vertical), WX. VERTICAL (horizontal, default) #Add方法有几个參数, proportion parameters are based on the amount of space you allocate when the form changes size # For example: horizontal Boxsizer, the filename component gets all the extra space when it changes size # Assuming that all 3 components have proprotion set to 1, they will get equal space # to be able to speak proportion set to whatever number 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) #flag參数相似于style參数, can be constructed #wx using either a | or an or join construct symbol constant. The expand flag ensures that the component expands into all allocated space. and WX. Left,wx. Right,wx. TOP, #wx. The bottom and Wx.all markers determine which edge the bounding rectangle is applied to. Used to set the edge width 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 ()
The layout is ready, but when you click on the button, nothing happens.

------

The complete procedure.

The rest of the work requires two handler functions: Load and save.

When the event handler function is invoked. Event object events is the only number of references

#文件名称使用filename对象的GetValue方法获取

#为了将文本引入文本区, just use contents. SetValue can do it.

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

#save函数差点儿和load一样, except that it has a ' W ' attribute and the Write method.

#GetValue用于从文本区获得信息

Def save (Event):    file = open (filename. GetValue (), ' W ')    file.write (contents. GetValue ())    File.close ()

    • Program Listing 6: Finally the program
import wxdef Load (event): File = open (filename. GetValue ()) contents. SetValue (File.read ()) File.close () def Save (event): File = open (filename. GetValue (), ' W ') file.write (contents. GetValue ()) file.close () app = WX. APP () win = WX. Frame (none,title= "Simple Editor", size= (410,335)) bkg = WX. Panel (Win) Loadbutton = wx. Button (bkg,label= ' Open ') 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 () 

------

Other tool Kits

To see the simplest example, there is only one form, a button with a hello tag, when you click the button. It will print the text "hello,world!"

The code is as follows:

Import Wxdef Hello (event):    print "hello,world!" App = WX. APP () win = WX. Frame (none,title= "hello,wxpython!", Size= (200,100)) button = WX. button (win,label= ' Hello ') button. Bind (WX. Evt_button,hello) win. Show () app. Mainloop ()
------

Using Tkinter

Python comes with, the following is a GUI program implemented using Tkinter

From Tkinter import *def hello ():    print (' hello,world! ')    Win = Tk () win.title (' hello,tkinter! ') Win.geometry (' 200x100 ') btn = Button (win,text= ' Hello ', Command=hello) btn.pack (Expand=yes,fill=both) Mainloop ()
------

Others are also able to use Jython and swing

The Java implementation of Jython:python, similar to Wxpython and Tkinter, is not available. The only available GUI toolkit is the Java Standard Library package

In the AWT and swing.




<< Basic Python Tutorials >> Learning Notes | 12th Chapter | Graphical user interface

Related Article

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.