Wxpython learning notes Day 1

Source: Internet
Author: User

1. Import the wxpython Library
Import wx
2. Create a form constructor class
Class Name (wx. Frame ):
Def _ init _ (self, parent, id ):
Wx. Frame. _ init _ (self, parent, id, 'tool', size = (300,200 ))
3. Create a panel Panel = wx. panel (self)
Set panel background
Panel. SetBackgroundColour ('white ')
Is the Status display bar, displayed at the bottom of the Panel
Wx. Frame. CreateStatusBar ()
# This is a tool menu displayed on the form. The tool menu generally has a graphic menu.
Wx. Frame. createmedilbar ()
# The menu is also displayed on the form
Wx. MenuBar ()
# Specific menu on the menu bar
Wx. Menu ()
About events
Wx. Frame. Bind
Parameter 1: binding time type
Parameter 2: triggered Function
Parameter 3: Determine the id trigger on the form
About Image Processing
# Retrieving image objects
Image = wx.Image('yuanxiao.jpg ', wx. BITMAP_TYPE_JPEG)
# Retrieving bitmap streams
Temp = image. ConvertToBitmap ()
Menu Bar details
Append method to add menu items
Parameter 1: the unique id of the menu item
Parameter 2: menu item name
Parameter 3: The text displayed in the status bar when the menu is selected
The AppendMenu method adds the next menu
Parameter 1: the unique id of the menu item
Parameter 2: menu item name
Parameter 3: menu item object
Constructors of forms
Wx. Frame (parent, id =-1, title = "", pos = wx. DefaultPosition,
Size = wx. DefaultSize, style = wx. DEFAULT_FRAME_STYLE,
Name = "frame ")
Similar parameters are displayed in the constructor of other widgets. Parameters are described as follows:
Parent: the parent window of the framework. For top-level windows, this value is None. The framework is destroyed with the destruction of its parent window. Depending on the platform, the frame can only appear at the top of the parent window. In the case of multi-document interface, the subwindow is restricted to moving and scaling only in the parent window.
Id: The wxPython id of the new window. You can clearly pass one. Or pass-1, which will cause wxPython to automatically generate a new ID.
Title: The title of the window.
Pos: A wx. Point object that specifies the position of the upper left corner of the new window in the screen. In graphic user interface programs, (0, 0) is usually the upper left corner of the display. This default (-1,-1) will allow the system to determine the position of the window.
Size: A wx. Size object that specifies the initial size of the window. This default (-1,-1) will allow the system to determine the initial size of the window.
Style: a constant of the window type. You can use or to combine them.
Name: the internal name of the framework. You can use it to find the window later.
Remember, these parameters will be passed to the constructor method of the parent class: wx. Frame. _ init __().
The method for creating the wx. Frame subclass is as follows:
Class MyFrame (wx. Frame ):
Def _ init _ (self ):
Wx. Frame. _ init _ (self, None,-1, "My Friendly Window ",
(100,100), (100,100 ))
Some attributes of the framework
Frame. Show (False) # make the frame invisible.
Frame. Show (True) # True is the default value to make the frame visible.
Frame. Hide () # equivalent to frame. Show (False)
Close form
Wx. Exit ()
Instance Program
# Set the encoding used by python
# Coding = UTF-8
# Load the wx Library
Import wx
# Framework
Class ToolbarFrame (wx. Frame ):
Def _ init _ (self, parent, id): # Constructor
# Construct a framework
Wx. Frame. _ init _ (self, parent, id, 'tool', size = (300,200 ))
# Filling panel
Panel = wx. Panel (self)
# Set panel background color
Panel. SetBackgroundColour ('white ')
# Create a status bar
StatusBar = self. CreateStatusBar ()
# Create a toolbar
Toolbar = self. createmedilbar ()
# Retrieving toolbar icon objects
Image = wx.Image('yuanxiao.jpg ', wx. BITMAP_TYPE_JPEG)
# Retrieving an icon bitmap stream
Temp = image. ConvertToBitmap ()
# Create a toolbar
Toolbar. AddSimpleTool (wx. NewId (), temp, "New", "Long help for 'new '")
# Set the toolbar location
Toolbar. Realize ()
# Create a menu bar
MenuBar = wx. MenuBar ()
# Create a menu item
Menu1 = wx. Menu ()
# Add a sub-menu item
Menu1.Append (wx. NewId (), "C & ut", "Copy in status bar ")
# Set the split line in the menu item
Menu1.AppendSeparator ()
Sm = wx. Menu ()
Sm. Append (-1, 'test 1 ');
Sm. Append (-1, 'test 2 ');
# Add level-2 menu items to level-1 menus
Menu1.AppendMenu (-1, 'sub', sm );
# Add a menu item to a single menu bar
MenuBar. Append (menu1, "file (& F )")
Menu2 = wx. Menu ()
Nid = wx. NewId ()
Menu2.Append (nid, "& Copy", "Copy in status bar ")
# Event binding
Self. Bind (wx. EVT_MENU, self. onQuit, id = nid)
Menu2.Append (wx. NewId (), "C & ut ","")
Menu2.Append (wx. NewId (), "& Paste ","")
Menu2.AppendSeparator ()
Menu2.Append (wx. NewId (), "& Options...", "Display Options ")
MenuBar. Append (menu2, "& Edit ")
# Set Menu Bar display
Self. SetMenuBar (menuBar)
# Events triggered
Def onQuit (self, event ):
# Dialog box
Dlg = wx. MessageDialog (None, 'Is this the coolest thing ever! ', 'Messagedialog', wx. YES_NO | wx. ICON_QUESTION)
Result = dlg. ShowModal ()
Dlg. Destroy ()
# Execution
If _ name _ = '_ main __':
App = wx. PySimpleApp ()
Frame = ToolbarFrame (parent = None, id =-1)
Frame. Show ()
App. MainLoop ()
Open-Source Project Introduction
Http://onlypython.group.javaeye.com/group/blog/309552
Development
Http://rainytooo.javaeye.com/blog/150228
WxPython in Action Chinese Document
Http://www.pythontik.com/blog/article.asp? Id = 184
Author sanshi0815

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.