Wxpython Introduction
WxPython is a good set of GUI graphics libraries in the Python language, allowing Python programmers to easily create complete GUI user interfaces with full function keys. WxPython is provided to the user as an excellent cross-platform GUI library wxWidgets python package and Python module. just like Python and wxwidgets, Wxpython is also an open source software with a very good cross-platform capability to run on 32-bit Windows, the vast majority of Unix or Unix-like systems, the Macintosh OS X.
This article address: http://www.cnblogs.com/archimedes/p/wxpython-begin.html, reprint please indicate source address.
First look at an example, which creates a window with a text box to display the mouse position:
ImportWXclassMyFrame (WX. Frame):def __init__(self): WX. Frame.__init__(Self, None,-1,"My Frame", size = (300, 300)) Panel= WX. Panel (self,-1) panel. Bind (WX. Evt_motion, self. OnMove) wx. Statictext (panel,-1,"Pos:", pos = (10, 12))Self.posctrl = wx. Textctrl (Panel,-1,"", pos = (40, 10)) defOnMove (Self, event): Pos=event. GetPosition () Self.posCtrl.SetValue ("%s,%s"%(Pos.x, pos.y))if __name__=='__main__': App=WX. Pysimpleapp () frame=MyFrame () frame. Show (True) app. Mainloop ()
The following interface appears after running:
The beautiful interface is an essential part of a GUI program, and Wxpython can do this, along with Python's powerful features and concise syntax, which is used in the Python GUI as a mainstream.
Create the smallest empty Wxpython program
#Coding=utf-8ImportWx#Import a required Python packageclassAPP (WX. APP):#subclasses of Wxpython application classes defOnInit (self):#defining an application's initialization methodframe = wx. Frame (parent = None, title ='Bare') frame. Show ()returnTrueapp= APP ()#to create an instance of an application classApp. Mainloop ()#Enter the main event loop for this application
The following interface appears after running:
1. Import Wxpython
The first thing you need to do is to import this main Wxpython package, which is named WX:
Import WX
Once the package is imported, you can refer to the Wxpython classes, functions, and constants (they are prefixed with WX) as follows:
The Class App (WX. APP):
The import order needs to be noted: You must first import WX before you import anything from Wxpython. in general, the order of module import in Python is irrelevant. But in Wxpython, it's a complex module . When you first import the WX module, Wxpython will perform some initialization work on other Wxpython modules. For example, some sub-packages in Wxpython, such as the XRC module, which do not work correctly before the WX module is imported, must be imported in the following order:
Import WX from Import XRC
The import order for the above is only for Wxpython modules, the Python module import order is OK. For example:
Import SYS Import WX Import OS from Import XRC Import Urllib
2 working with applications and frameworks
Once you have imported the WX module, you will be able to create your application (application) objects and frame objects. Each Wxpython program must have a application object and at least one frame object. The Application object must be WX. An instance of the app or one of the subclasses that you defined in the OnInit () method. When your application starts, the OnInit () method will be wx. Called by the app parent class.
The following code demonstrates how to define our WX. Sub-category of the app:
class App (WX. APP): def OnInit (self): 'Bare') frame. Show () return True
Above, we define a subclass called App. We usually create a frame object in the OnInit () method. The WX above. Frame accepts three parameters, only the first one is required, and the rest has default values.
Call the Show () method to make the frame visible, otherwise it is not visible. We can set the visibility of the frame by giving show () a Boolean parameter:
Frame. Show (False) # makes the frame invisible. Frame. Show (True) # true is the default value that makes the frame visible. Frame. Hide () # is equivalent to frame. Show (False)
To define an application's initialization method Note: We do not define a __init__ () method for our application class. In Python, this means that the parent method Wx.app.__init () __ will be called automatically when the object is created. If you define your own __init__ () method, do not forget to call the __init () __ method of its base class, as shown in the following example:
class App (WX. APP): def__init__ (self): Wx.app. __init__ (self)
If you forget to do so, Wxpython will not be initialized and your OnInit () method will not be called.
Create an application instance and enter its main event loop
This step is to create WX. An instance of the app subclass and calls its Mainloop () method:
App = App () app. Mainloop ()
Once in the main event loop, control is forwarded to WxPython. The WxPython GUI program primarily responds to user mouse and keyboard events. when all the frames of an application are closed, this app. The Mainloop () method returns and the program exits.
Extend this minimal empty Wxpython program
#Coding=utf-8#!/usr/bin/env python #1#on operating systems such as Linux and UNIX, it tells the operating system#how to find the interpreter that executes the program. If the program is given executable permissions (for example, using the chmod command),#We can run this program by simply typing the name of the program under the command line: This line will be ignored on other operating systems. But a cross-platform that contains code that it can implement""""spare.py is a atarting point for a wxpython program."""#This is the document string, and when the first sentence in the module is a string, the string is#The module's document string and is stored in the module's __doc__ property. ImportWXclassFrame (WX. Frame):#define our own frame class as WX. Sub-class of frame PassclassApp (WX. APP):defOnInit (self): Self.frame= Frame (Parent=none, title='Spare')#use a reference to a frame instance as a property of the application instanceself.frame.Show () self. Settopwindow (Self.frame)#The Settopwindow () method is an optional method that lets Wxpython #method knows which frame or dialog box will be considered primary. A Wxpython program can have several #framework, which has a top-level window that is designed to be an application returnTrueif __name__=='__main__':#Python is often used to test whether the module is run as a program independently or by another model. #block is imported. We do this by examining the __name__ property of the module.App =app () app. Mainloop ()
The result is the same as above, but the program structure is more complete
Implement the final hello.py program:
#Coding=utf-8#!/usr/bin/env pythonImportWXclassFrame (WX. Frame):#define a WX. A subclass of frame so that we can control the content and appearance of the frame in more capacity """Frame class that displays an image.""" def __init__(Self, image, Parent=none, id=-1, POS=WX. Defaultposition, title='Hello, wxpython!.'):#add an image parameter to the constructor of our frame. This value is passed through our application #class provides when you create an instance of a framework """Create a Frame instance and display image.""" #with WX. Staticbitmap control to display this image, it requires a bitmaptemp =image. Converttobitmap () Size=temp. GetWidth (), temp. GetHeight () wx. Frame.__init__(self, parent, ID, title, pos, size) self.bmp= WX. Staticbitmap (Parent=self, bitmap=temp)classAPP (WX. APP):#defines a wx with the OnInit () method. Subclass of App """Application class.""" defOnInit (self):#an Image object was created with a file named Wxpython.jpg in the same directory as hello.pyImage = Wx. Image ('wxpython.jpg', WX. Bitmap_type_jpeg) Self.frame=Frame (image) Self.frame.Show () self. Settopwindow (Self.frame)returnTruedefMain ():#the main () function creates an instance of an application and starts the Wxpython event loopApp =app () app. Mainloop ()if __name__=='__main__': Main ()
Resources:
"WxPython in Action"
Wxpython Simple Introduction