wxPython介绍+一个实用的例子
1. wxPython简介
wxPython是wxWidget的库的一个python的封装。提供了一些库和一些工具。
这样wxPython即有python语言的优点: 语法强悍,少写了不少代码:)
也有wxWidget图形库的优点: 直接拉控件到大概位置就行了,不需要去调整控件的对齐,也不需要关心gui界面是否支持各种分辨率的桌面。而且界面都是可以运行时切换,只要写很少的切换代码。跨平台的图形库...
后悔,我怎么以前会用vb开发gui的 -_-!。
缺点,网上资料还比较少。而且在线文档都是英文的。不过如果英文好的话,到真的无所谓,因为wxPython的安装包本身提供的工具和资料也足够多,足够好了。
2. 开发入门 我使用ultraedit作为编辑器,wxPython自带的XRCed作为编辑xrc文件(一种xml格式的资源文件,类似于VC6中的RC文件)。
文档主要参考wxPython自带的在线文档和demo代码。不过关于如何使用xrc文件设计gui,wxPython自带的资料似乎还不够详细,这也是我写本文的原因。
通常情况下google"cvs def wxpython相关的关键字"可以快速找到开源的源代码参考。
如果这些例子还不够的话,就参考我的程序吧,这是一个商用程序的原型,我相信该程序的内容已经足够丰富了,应该比网上的一些wxPython教程中的hello world程序更有借鉴意义。
3. 源代码 两个文件,main.py是主程序,main.xrc是资源文件。只要安装了了python和wxpython,然后将这两个文件放在同一个目录中,运行main.py就可以了。
------------------------main.py-----------------------begin
################################################################################## #General: # read http://wiki.wxpython.org/index.cgi/UsingXmlResources for more information about how to use xrc # #Problems: # google "(class ''''wxMenuBar'''') not found ", visit http://aspn.activestate.com/ASPN/Mail/Message/wxPython-users/2242126 ( wxGlade''''s problem?) # google "TypeError: OnExit() takes exactly 2 arguments (1 given)" and visit http://www.gnuenterprise.org/irc-logs/gnue-public.log.2003-02-26 (wxPython''''s problem?) # we *donot* want sash in splitter window to disappear. google "wxSplitterEvent" , get "The sash was double clicked. The default behaviour is to unsplit the window when this happens (unless *the minimum pane size has been set to a value greater than zero*, yes it is what we do in xrc)", # #Naming conventions: # class._var1 = class shared variable (like static class data member in C++, BTW: see python online help for python''''s private variables''''s naming conventions ) # class.CapitalizeEveryWord = class method # class.instance_data_member = instance''''s data member (like public data member in C++) # m_file_shared_variables = variables prefixed with "m_" are shared in a file # g_global_shared_variables = variables prefixed with "g_" are global variables ##################################################################################
from wxPython.wx import * from wxPython.xrc import * import sys
#the New Job dialog class dlgNew: def __init__(self,ctrl): assert(ctrl<>None) self.ctrl=ctrl self.idc_textmode_txt=self.ctrl.FindWindowById(XRCID("idc_textmode_txt")) assert(self.idc_textmode_txt<>None) self.idc_new_runnow=self.ctrl.FindWindowById(XRCID("idc_new_runnow")) assert(self.idc_new_runnow) self.idc_new_jobname=self.ctrl.FindWindowById(XRCID("idc_new_jobname")) assert(self.idc_new_jobname<>None) self.MessageMap(self.ctrl) def __del__(self): #self.ctrl.Destroy() pass def MessageMap(self,ctrl): EVT_BUTTON(ctrl, XRCID("idc_new_ok"), self.OnOK) EVT_BUTTON(ctrl, XRCID("idc_new_cancel"), self.OnCancel) EVT_BUTTON(ctrl, XRCID("idc_new_apply"), self.OnApply) def OnOK(self,event): if self.Validate(): self.OnApply(event) self.ctrl.Close(True) else: # report error pass def OnCancel(self,event): self.ctrl.Close(True) def OnApply(self,event): #save job #if needed, submit it now if self.idc_new_runnow.IsChecked(): pass def Validate(self): try: if self.idc_textmode_txt.GetValue()==None: assert(0) raise if self.idc_new_jobname.GetValue()==None: assert(0) raise if not self.IsCmdValid(self.idc_textmode_txt.GetValue()): assert(0) raise return True except: return False def IsCmdValid(self,txt): if txt==None: assert(0); return False return True
class hostList: def __init__(self, parent, id): assert(parent<>None) assert(id<>None) self.id=id self.ctrl=parent.FindWindowById(self.id) assert(self.ctrl<>None) #init list header self.config_column=[ #id; #name; #size; #after filling data we resize [0,"ID", wxLIST_AUTOSIZE_USEHEADER, wxLIST_AUTOSIZE], &n [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] 下一页 [平面设计]如何使用FireWorks制作沿路径排列的文字效果 [网络技术]如何使用DOS命令有计划地重新启动IIS [电脑技术]图文解说如何使用QQ截图 [系统软件]如何使用WinMe的系统还原功能 [系统软件]如何使用Ghost备份和恢复系统 [VB.NET程序]如何使用 VB 编写自动反安装的程序? [VB.NET程序]DX: Full Screen GUI Development 2 [VB.NET程序]DX: Full Screen GUI Development 1 [网页制作]如何使用Microsoft FrontPage 2000制作动态按钮 [Web开发]如何使用ajax开发web应用程序(三)
|