转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 电脑应用 >> 电脑技术 >> 正文
如何使用wxPython设计gui         ★★★

如何使用wxPython设计gui

作者:闵涛 文章来源:闵涛的学习笔记 点击数:5400 更新时间:2006/7/27

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应用程序(三)
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · 电脑技术  · 操作系统
    · 磁盘工具  · 视音频技术
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台