12.1 Rich Platform
Before writing a Python GUI program, you need to decide which GUI platform to use. Wxpython----Cross-platform Pythongui Toolkit
Make sure that the binary version that you select corresponds to the version of Python, for example, Wxpython compiled for python2.3 is not available for python2.4.
12.3.1 Start
The WX module needs to be started:
Import WX
There are many ways to write Wxpython programs, but the inevitable thing is to create application objects. The basic application class is called Ex. APP, which is responsible for all the initialization behind the scenes. The simplest Wxpython program should look like this:
Import WX
App = WX. App
App. Mainloop ()
If WX. The app does not work and may need to be replaced with WX. Pysimpleapp
12.3.2 Windows and Components
The window is also called the frame, it is just 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, use none, as shown in Listing 12-1. And the app is called. You need to call the window's Show method before mainloop-----Otherwise it will remain hidden.
Create and display a frame
Import WX
App = WX. APP ()
Win = WX. Frame (None)
Win. Show ()
App. Mainloop ()
Adding buttons to the frame is also simple----simply instantiate WX using win as the parent parameter. Button can
Add a button on the frame
Import WX
App = WX. APP ()
Win = WX. Frame (None)
BTN = wx. Button (Win)
Win.show ()
App. Mainloop ()
12.3.3 label, title, and location
You can use the label parameter of the constructor to set their label when creating a part. Similarly, you can also use the title parameter to set the title of the frame.
Add tags and headings using keyword parameters
Import WX
App = 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 ()
Note the layout of the button to display the hidden button. A very basic approach is to use the POS and size parameters to set the position and size within the constructor.
Set Button position
Import WX
App = WX. APP ()
Win = WX. Frame (none,title= "Simple Editor", size= (410,335))
Win. Show ()
Loadbutton = wx. Button (win,label= ' Open ', pos= (315,5), size= (80,25))
Savebutton = wx. Button (win,label= ' Save ', pos= (315,5), size= (80,25))
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)
App. Mainloop ()
Both the position and the dimension include a pair of values: The position includes the x and Y coordinates, and the dimensions include the width and height.
Two text controls were created, each using a custom style. The default text control-style text box is a single line of editable text with no scroll bars, so you can use the style parameter to adjust the style for creating text areas. The value of the style parameter is actually an integer. But without specifying it directly, you can specify it using a bitwise OR operator or a style with a special name in the WX module. In this example, I joined Wx.te_multine to get the multiple lines of text and wx.hscroll to get the horizontal scrollbar.
12.3.4 more intelligent Layouts
Lets the component be resized and positioned with the component in the window.
Using a sizing device
Import WX
App = WX. 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)
Hbox = wx. Boxsizer ()
Hbox. ADD (filename,proportion=1,flag=wx. EXPAND)
VBox. 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 (contenxs,proportion=1,flag=wx. EXPAND | Wx. Left | Wx. BOTTOM | Wx. RIGHT,BORDER=5)
bkg. Setsizer (VBox)
Win. Show ()
App. Mainloop ()
This code runs the same result as the previous precedent, but uses relative coordinates instead of absolute coordinates.
Wx. The Boxsizer constructor has a parameter that determines whether it is horizontal or vertical (wx.horizontal or wx.vertical), and the default is horizontal. The Add method has several parameters, and the proportion parameter sets the proportions according to the space allocated when the window changes size. For example, in a horizontal boxsizer, the filename component changes size to get all the extra space. If these 3 parts set the proportion to 1, they will get equal space. The proportion can be set to any number.
The flag parameter is similar to the style parameter in the constructor, and can be constructed using a bitwise OR operator join to construct a symbolic constant. Wx. The expand flag ensures that the component expands into the allocated space. and WX. Left,wx. Right,wx. Top,wx. The bottom and Wx.all markers determine which edge the border parameter applies to, and the border parameter is used to set the edge width.
12.3.5 Event Handling
In GUI terminology, a user performs an action called an event. You need to let the program pay attention to these events and act as a reaction. You can bind a function to a component that is involved in a possible occurrence of an event that achieves this effect. When an event occurs, the function is called. You can link an event handler to a given event by using the part's Bind method.
Suppose you write a function that is responsible for opening the file and name it load. You can then use the function as an event handler for Loadbutton as follows:
Loadbutton.bind (WX. Evt_button,load)
I link the function to the button---the function is called when the button is clicked. Known as WX. The symbolic constant of Evt_button represents a button event. The WX framework has such event constants for various events----from mouse actions to keyboard keys.
12.3.6 completed the program
Now you need two event handler functions: Load and save. When the event handler is called, it receives an event object as its only parameter, which includes information about what happened, but this can be ignored here because the program only cares about what happens when clicked.
def 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 ()
The final GUI program
Import WX
def 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)
VBox. 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 (contenxs,proportion=1,flag=wx. EXPAND | Wx. Left | Wx. BOTTOM | Wx. RIGHT,BORDER=5)
bkg. Setsizer (VBox)
Win. Show ()
App. Mainloop ()
You can use this editor in the following steps.
1. Run the program. Should see a window just about the same
2. Make some words in the text area
3. Type the file name in the text box. Make sure the file does not exist or it will be overwritten.
4. Click the Save button
5. Close the editor window
6. Restart the program
7. Type the same file name within the text box
8. Click the Open button. The text content of the file should be reproduced within the large text area.
9. Edit the file and save it again.
12.4 But I would rather use ...
Python has too many GUI toolkits, so I can't show you all the toolkits. But I can give some examples of popular GUI packages.
Import WX
def 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 ()
12.4.1 using Tkinter
Tkinter is an established Python GUI program. It is packaged in the TK GUI toolkit.
From Tkinter Import *
def hello (): print ' Hello,world '
Win = Tk ()
Win.title (' hello,tkinter! ')
Win.geometry (' 200*100 ')
BTN = Button (win,text= ' Hello ', Command=hello)
Btn.pack (Expand=yes,fill=both)
Mainloop ()
12.4.2 using Jython and Swing
If you are using Jython, packages like Wxpython and Tkinter are not available. The only available GUI toolkit is the Java Standard Library package AWT and swing.
The following is an example of a GUI implemented using Jython and swing
From javax. Swing Import *
Import Sys
def hello (event): print ' hello,world! '
BTN = JButton (' Hello ')
btn.actionperformed = Hello
Win = JFrame (' hello,swing! ')
Win.contenPane.add (BTN)
def closehandler (event): Sys.exit ()
win.windowclosing = Closehandler
Btn.size = Win.size = 200,100
Win.show ()
An additional event handler is added here because the Close button does not have any useful default behavior in Java swing. In addition, it is not necessary to explicitly enter the main event loop because it is run with the program.
This article is from the "linux_oracle" blog, make sure to keep this source http://pankuo.blog.51cto.com/8651697/1661446
Python's graphical interface