WxPython and wxpython
WxPython Introduction
WxPython is an excellent GUI graphics library in the Python language. It allows Python programmers to easily create a complete GUI with full function keys. WxPython is an excellent cross-platform GUI library. wxWidgets provides Python encapsulation and Python modules for users. Just like Python and wxWidgets, wxPython is also an open-source software with excellent cross-platform capabilities, it can run on 32-bit windows, most Unix or Unix-like systems, and Macintosh OS X.
URL: http://www.cnblogs.com/archimedes/p/wxpython-inin.html.
First, let's look at an example. It creates a window with a text box to show the mouse position:
import wxclass MyFrame(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)) def OnMove(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 is displayed after running:
A beautiful interface is an essential part of a GUI program. wxPython can do this. In addition, the powerful functions and concise Syntax of Python, it becomes a mainstream in Python GUI.
Create the smallest empty wxPython Program
# Coding = UTF-8 import wx # import a required Python package class App (wx. app): # subclass wxPython application class def OnInit (self): # define the initialization method frame = wx for an application. frame (parent = None, title = 'bare') frame. show () return Trueapp = App () # create an app instance. mainLoop () # enter the main event loop of this application
The following interface is displayed after running:
1. Import wxPython
The first thing you need to do is import the main wxPython package named wx:
Import wx
Once this package is imported, You can reference wxPython classes, functions, and constants (they are prefixed with wx), as shown below:
Class App (wx. App ):
Note the import order that you must first import wx before importing other things from wxPython. Generally, the module import sequence in Python is irrelevant. But in wxPython, it is a complicated module. When you import the wx module for the first time, wxPython needs to perform initialization for other wxPython modules. For example, some sub-packages in wxPython, such as the xrc module, cannot work correctly before the wx module is imported. We must import them in the following order:
import wx from wx import xrc
The preceding import sequence is only for the wxPython module. The import sequence of the Python module does not matter. For example:
import sysimport wximport osfrom wx import xrcimport urllib
2. Use applications and frameworks
Once you import the wx module, you can create your application object and frame object. Each wxPython program must have an application object and at least one frame object. The application object must be an instance of wx. App or an instance of a subclass defined in the OnInit () method. When your application starts, the OnInit () method will be called by the wx. App parent class.
The following code demonstrates how to define a subclass of our wx. App:
class App(wx.App): def OnInit(self): frame = wx.Frame(parent = None, id= -1, title = 'Bare') frame.Show() return True
We have defined a subclass named App. We usually create a frame object in the OnInit () method. The wx. Frame above accepts three parameters. Only the first one is required, and the rest have default values.
Call Show () to make the frame visible; otherwise, it is invisible. We can set the frame visibility by giving Show () A boolean parameter:
Frame. show (False) # make the framework invisible. frame. show (True) # True is the default value to make the framework visible. frame. hide () # equivalent to frame. show (False)
Define the initialization method of an application. Note: we have not defined a _ init _ () method for our application class. In Python, this means that the parent method wx. App. _ init () _ will be automatically called when the object is created. If you define your own _ init _ () method, do not forget to call the _ init () _ method of its base class. The example is as follows:
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
In this step, create the wx. App subclass instance and call its MainLoop () method:
app = App()app.MainLoop()
Once you enter the main event loop, control is transferred to wxPython. The wxPython GUI mainly responds to users' mouse and keyboard events. When all frameworks of an application are closed, the app. MainLoop () method returns and exits.
Extends the smallest 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 for the execution program. If the program is given executable permissions (for example, using the chmod command), # You can simply enter the program name in the command line to run the program: this line will be ignored on other operating systems. But the cross-platform "Spare that can implement code is included. py is a atarting point for a wxpython program. "" # This is a document string. When the first sentence in the module is a string, this string is # The document string of this module and is stored in the _ doc _ attribute of this module. Import wxclass Frame (wx. frame): # defines our own Frame class as wx. frame subclass passclass App (wx. app): def OnInit (self): self. frame = Frame (parent = None, title = 'spare') # Use the reference to the frame instance as an attribute of the application instance self. frame. show () self. setTopWindow (self. frame) # SetTopWindow () is an optional method, which allows the wxPython # method to know which framework or dialog box is considered as the main one. A wxPython program can have several # frameworks, one of which is the return Trueif _ name _ = '_ main _' of the top-level window designed as an application __': # Python is usually used to test whether the module runs independently as a program or is imported by another module. We can check the _ name _ attribute of this module to implement app = App () app. MainLoop ()
The running 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 pythonimport wxclass Frame (wx. frame): # define a wx. the subclass of the Frame so that we can control the content and appearance of the Frame "" 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 framework. This value is provided by our application # class "" Create a Frame instance and display image when creating an instance of the framework. "" # Use wx. staticBitmap control to display this image. It requires a bitmap temp = image. convertToBitmap () size = temp. getWidth (), temp. getHeight () wx. frame. _ init _ (self, parent, id, title, pos, size) self.bmp = wx. staticBitmap (parent = self, bitmap = temp) class App (wx. app): # defines a wx with the OnInit () method. app subclass "" Application class. "def OnInit (self): # Use with hello. py creates an image object image = wx.Image('wxPython.jpg ', wx. BITMAP_TYPE_JPEG) self. frame = Frame (image) self. frame. show () self. setTopWindow (self. frame) return Truedef main (): # The main () function creates an application instance and starts the wxPython event loop app = App () app. mainLoop () if _ name _ = '_ main _': main ()References:
WxPython in action
Wxpython beginners
Import wx, sys, osclass haha (wx. frame): def _ init _ (self): wx. frame. _ init _ (self, None,-1, title = "hello", size = (400,100) self. control = wx. textCtrl (self, style = wx. TE_MULTILINE) self. show (True) if _ name _ = '_ main _': app = wx. app () frame = haha () app. mainLoop () has been modified for you. It is okay to run it.
How to install wxPython? For beginners of Python, I am very grateful if you can provide tutorials.
In windows, go to the wxpython website to download the installation package and install the package directly. The installation will automatically find your python directory.
Linux, if ubuntu, direct apt-get install python-wxgtk2.8, redhat and so on directly download the rpm file installation.