Wxpython study notes the first day _python

Source: Internet
Author: User
Tags unique id
1, Import Wxpython Library
Import WX
2, create the form construction class
Class name (WX. Frame):
def __init__ (self, Parent, id):
Wx. Frame.__init__ (self, parent, ID, ' Tools ', size= (300, 200))
3, set up panel = WX. Panel (self)
Set Panel background
Panel. Setbackgroundcolour (' white ')
For status display bar, display at bottom of panel
Wx. Frame.createstatusbar ()
#这个为工具菜单, displayed above the form, the tool menu is usually an icon menu
Wx. Frame.createtoolbar ()
#为菜单也是显示在窗体上面
Wx. MenuBar ()
#菜单栏上面的具体菜单
Wx. Menu ()
About events
Wx. Frame.bind
Parameter one, the time type of the binding
Parameter two, the trigger function
Parameter three, determine the ID trigger on that form
About picture Processing
#获取图片对象
Image = Wx. Image (' yuanxiao.jpg ', WX. BITMAP_TYPE_JPEG)
#取得位图流
temp = image. Converttobitmap ()
About the specific menu bar
Append method to add a menu item
Parameter one, menu item unique ID
Parameter two, menu item name
Parameter three, when you select the menu, the status bar displays the text
AppendMenu method to add the next level menu
Parameter one, menu item unique ID
Parameter two, menu item name
Parameter three, the specific menu item object
About the constructor for a form
Wx. Frame (parent, Id=-1, title= "", pos=wx. Defaultposition,
Size=wx. DefaultSize, Style=wx. Default_frame_style,
Name= "Frame")
We'll see a similar argument in the constructor of another widget. The parameters are described as follows:
Parent: The frame's parents window. For top-level windows, this value is none. The framework is destroyed with the destruction of its parent window. Depending on the platform, the framework can be restricted to appear only at the top of the parent window. In the case of a multiple-document interface, child windows are limited to moving and scaling only in the parent window.
ID: The Wxpython ID number for the new window. You can explicitly pass one. or pass-1, which will cause Wxpython to automatically generate a new ID.
Title: The caption of the window.
POS: a WX. The point object that specifies the position of the upper-left corner of the new window in the screen. In a graphical user interface program, usually (0,0) is the upper-left corner of the display. This default ( -1,-1) will let the system determine the position of the window.
Size: a WX. The size object that specifies the initial size of this window. This default ( -1,-1) will let the system determine the initial size of the window.
Style: Constant specifying the type of the window. You can use or compute them to combine them.
Name: The inner name of the frame. You can use it later to look for this window.
Remember, these parameters will be passed to the constructor method of the parent class: WX. Frame.__init__ ().
Create WX. The method for a frame subclass looks like this:
Class MyFrame (WX. Frame):
def __init__ (self):
Wx. Frame.__init__ (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, making the frame visible.
Frame. Hide () # is equivalent to frame. Show (False)
Close form
Wx. Exit ()
Instance Program
#设置python使用的编码
#coding =utf-8
#加载wx库
Import WX
#框架类
Class Toolbarframe (WX. Frame):
def __init__ (self, Parent, id): #构造函数
#构造框架
Wx. Frame.__init__ (self, parent, ID, ' Tools ', size= (300, 200))
#填充面板
panel = WX. Panel (self)
#设置面板背景颜色
Panel. Setbackgroundcolour (' white ')
#建立状态栏
StatusBar = self. Createstatusbar ()
#建立工具栏
toolbar = self. Createtoolbar ()
#取得工具栏图标对象
Image = Wx. Image (' yuanxiao.jpg ', WX. BITMAP_TYPE_JPEG)
#取得图标位图流
temp = image. Converttobitmap ()
#建立工具栏
Toolbar. Addsimpletool (WX. NewId (), temp, "new", "Long Help for ' new '")
#设置工具栏位置
Toolbar. Realize ()
#建立菜单栏
MenuBar = wx. MenuBar ()
#建立菜单项
menu1 = wx. Menu ()
#添加下级菜单项
Menu1. Append (WX. NewId (), "C&ut", "Copy in status bar")
#设置菜单项里的分割线
Menu1. Appendseparator ()
SM = WX. Menu ()
Sm. Append (-1, ' Test 1 ');
Sm. Append (-1, ' Test 2 ');
#把二级菜单项添加到一级菜单
Menu1. AppendMenu (-1, ' subordinate ', SM);
#把菜单项添加单菜单栏里
Menubar.append (menu1, "file (&f)")
MENU2 = wx. Menu ()
Nid = Wx. NewId ()
Menu2. Append (Nid, "&copy", "Copy in status bar")
#事件绑定
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")
#设置菜单栏展示
Self. Setmenubar (MenuBar)
#触发的事件
def onquit (self, event):
#弹出框
Dlg = wx. Messagedialog (None, ' Is this the coolest thing ever! ', ' Messagedialog ', WX. Yes_no | Wx. Icon_question)
result = Dlg. ShowModal ()
Dlg. Destroy ()
#执行
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
Explain development
http://rainytooo.javaeye.com/blog/150228
WxPython in Action Chinese document
http://www.pythontik.com/blog/article.asp?id=184
Author of the Three stone sanshi0815

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.