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

Source: Internet
Author: User

Python supports a lot of toolkits, but without a toolkit that is considered standard, the user chooses a larger 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 binaries release.

But on Unix you have to compile the installation yourself.

Another increasingly popular tool is 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. Note Select the Python version that corresponds to the binary version of your PC

2. After downloading the Wxpython, we strongly recommend downloading the demo version (demo), the sample program is very detailed

------

Creating a sample GUI program

The simplest example program is as follows:

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

Windows and Components

The window is also called the frame and it 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. If you are creating a separate window, you do not need to consider the parent part and use none. And the app is called. Mainloop need to call the window 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

#在框架上添加按钮也很简单, simply instantiate WX using win as the parent parameter. button.

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


    • Listing 3: Adding tags and headings 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 ()


Seems to have lost a button, actually hidden, if you want to show it, the inside of the post,size will have to use.

    • 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. Textctrl object), default editable, 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参数, you can 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. Simply add the part to the dimension, plus some layout parameters, and let the dimension manager manage the dimensions of the parent component on its own. In the example above, you need to add a background component (WX. Panel) to create some nested WX. Boxsizer, and then use the Setsizer method of the panel to set its sizing device.

    • Listing 5: Using the 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方法有几个参数, the proportion parameter is based on the amount of space allocated when the window changes size # For example: horizontal Boxsizer, the filename component gets all the extra space when the size is changed # If these 3 components all set Proprotion to 1, then they will get equal space # can be said proportion set to any 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参数, you can construct #wx by using either the | or the join construct symbol constants. 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 border parameter applies to, setting 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 nothing happens when you click the button.

------

The completed program

The rest of the work requires two handler functions: Load and save. When an event handler is called, event object events is the only parameter

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

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

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: The final 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 toolkits

Can see the following simplest example, there is only one window, with the Hello tag button, when the button is clicked, 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 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 can also use Jython and swing

Java implementations of Jython:python, such as Wxpython and tkinter, cannot be used. The only available GUI toolkit is the Java Standard Library package

In the AWT and swing.




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

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.