Wxpython study notes first day

  • 2020-04-02 09:23:29
  • OfStack

1, import the wxpython library
The import wx
2. Create a form constructor class
Class name (wx.frame) :
Def __init__ (self, parent, id) :
Wx. Frame. __init__ (self, parent, id, 'tools', size = (300, 200))
3, panel = wx.panel (self)
Set panel background
The panel. SetBackgroundColour (' White ')
Displays the status bar at the bottom of the panel
Wx. Frame. CreateStatusBar ()
This is the tool menu, which is displayed on the form. The tool menu is usually a menu with ICONS
Wx. Frame. CreateToolBar ()
The # for menu is also displayed on the form
Wx. MenuBar ()
# menu bar above the specific menu
Wx. Menu ()
About the event
Wx. Frame. The Bind
Parameter one, time type of binding
Parameter two, the triggered function
Parameter three, determine the id trigger on that form
About image processing
Get the image object
Image = wx. Image (' yuanxiao. JPG, wx BITMAP_TYPE_JPEG)
Get bitmap flow
Temp = image. ConvertToBitmap ()
About the specific menu bar
The Append method adds a menu item
Parameter one, the unique id of the menu item
Parameter two, menu item name
Parameter 3. The status bar displays text when the menu is selected
The AppendMenu method is to add the next menu level
Parameter one, the unique id of the menu item
Parameter two, menu item name
Parameter three, the specific menu item object
Constructor for the form
Wx. Frame (the parent, id = 1, the title = "", pos = wx. DefaultPosition,
Size = wx DefaultSize, style = wx. DEFAULT_FRAME_STYLE,
Name = "frame")
We will see similar arguments in the constructor of other widgets. The description of parameters is as follows:
Parent: the parent window of the framework. For the top-level window, this value is None. The frame is destroyed with the destruction of its parent window. Depending on the platform, the frame can be limited to appearing only at the top of the parent window. In the case of multi-document interfaces, child Windows are limited to being moved and scaled only in the parent window.
Id: the wxPython id number for the new window. You can definitely pass one. Or pass -1, which causes wxPython to automatically generate a new ID.
Title: the title of the window.
Pos: a wx.point object that specifies where the upper-left corner of the new window is on the screen. In graphical user interface programs, (0,0) is usually the top left corner of the display. This default (-1,-1) will let the system determine the location of the window.
Size: a wx.size object that specifies the initial size of the window. This default (-1,-1) will let the system determine the initial size of the window.
Style: a constant that specifies the type of the window. You can combine them using or operations.
Name: the internal name of the framework. You can use it later to find this window.
Remember, these parameters will be passed to the constructor method of the parent class: wx.frame.s ().
The way to create a subclass of wx.frame is as follows:
The class MyFrame (wx. Frame) :
Def __init__ (self) :
Wx.frame.s/s (self, None, -1, "My Friendly Window",
(100, 100), (100, 100))
Some properties about the framework
Frame.show (False) # makes the frame invisible.
Frame.show (True) # True is the default value to make the frame visible.
Frame.hide () # equals frame.show (False)
Closed form
Wx. The Exit ()
Examples of application
# sets the encoding that python USES
# coding = utf-8
# load the wx library
The import wx
# framework class
The class ToolbarFrame (wx. Frame) :
Def arbitration (self, parent, id):# constructor
# frame construction
Wx. Frame. __init__ (self, parent, id, 'tools', size = (300, 200))
# fill panel
The panel = wx. A panel (self)
Set panel background color
The panel. SetBackgroundColour (' White ')
Create a status bar
StatusBar = self. CreateStatusBar ()
Create a toolbar
The toolbar = self. CreateToolBar ()
Gets the toolbar icon object
Image = wx. Image (' yuanxiao. JPG, wx BITMAP_TYPE_JPEG)
Get icon bitmap flow
Temp = image. ConvertToBitmap ()
Create a toolbar
Toolbar.addsimpletool (wx.newid (),temp, "New", "Long help for 'New'")
Set the toolbar location
The toolbar. Realize ()
Create a menu bar
MenuBar = wx. MenuBar ()
Create menu items
Menu1 = wx. Menu ()
Add a subordinate menu item
Menu1.Append(wx.newid (), "C&ut", "Copy in status bar")
Set the dividing line in the menu item
Menu1. AppendSeparator ()
Sm = wx. Menu ()
Sm. Append (1, '1');
Sm. Append (1, '2');
Add a secondary menu item to a primary menu
Menu1. AppendMenu (1, 'lower', sm);
Add menu items to the menu bar
Menubar.append (menu1, "file (&F)")
Menu2 = wx. Menu ()
Nid = wx. NewId ()
Menu2. Append(nid, "&Copy", "Copy in status bar")
# event binding
The self. The 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..." , "the Display Options")
MenuBar. Append (menu2, "& Edit")
Set the menu bar display
Self. SetMenuBar (menuBar)
Event that # fires
Def onQuit (self, event) :
# pop-up
DLG = wx.messagedialog (None, 'Is this the coolest thing ever! ', 'MessageDialog', wx YES_NO | wx. ICON_QUESTION)
Result = DLG. ShowModal ()
DLG. Destroy ()
# to perform
If __name__ = = "__main__" :
App = wx. PySimpleApp ()
Frame = ToolbarFrame (parent = None, id = 1)
Frame. The Show ()
App. MainLoop ()
Introduction to open source projects
http://onlypython.group.javaeye.com/group/blog/309552
Interpretation of the development
http://rainytooo.javaeye.com/blog/150228
WxPython in Action Chinese document
http://www.pythontik.com/blog/article.asp? Id = 184
By sanshi0815

Related articles: