Python Basic Tutorial Summary 11--graphical user interface GUI

Source: Internet
Author: User

1. A rich platform

Toolkit                                Description Tkinter                using the TK platform. It's easy to get. Semi-standard.   Wxpython               based on Wxwindows. Cross-platform is becoming more and more popular.  Pythonwin              can only be used on Windows. The native Windows GUI feature is used.  javaswing              can only be used with Jython. Use the native Java GUI.    PyGTK                  uses the GTK platform and is popular on Linux.    PyQt                   uses QT platform, cross-platform.                            

2.wxpython Creating instance GUI

Instance requirements:

2.1 Simple Sample programs:

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

2.2 Windows and Components

The window is also called 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. If you are creating a separate window, you do not need to consider the parent part and use none. And the app is called. You need to call the window's Show method before Mainloop--otherwise it will remain hidden.

    • Program Listing 1:
    • Import Wxapp = wx. APP () win = WX. Frame (None) win. Show () app. Mainloop ()

2.3 tags, title and location

    • 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 ()

2.4 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 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方法有几个参数, 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 ()

  

2.5 Final Program

In GUI terminology, actions performed by the user (such as a click of a button) are called events, and the program needs to be aware of the event and respond. You can bind a function to an effect on a component that is involved in a possible event, and when an event occurs, the function is called

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 () 

  

Python Basic Tutorial Summary 11--graphical user interface GUI

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.