周海汉 /文

2010.1.9

wxPython写UI界面是比较爽的事情。python和wxWidgets都是跨平台的。因此,用wxPython写的应用程序的界面,可以在 不同的操作系统有本地一样的界面。即windows用户看到的是windows界面,linux,mac用户看到的是linux和mac的界面。这比使用 java,qt,tcl等写的界面会更符合用户的操作习惯一些。

除了直接调用wxPython API,还可以利用一些工具,wxGlade,BOA, XRCed等。前两者都支持所见即所得的拖放生成UI,XRCed是wxPython自带的UI生成工具,支持UI控件布局的逻辑,参数,事件设置,但非所见即所得的拖放。

本文是利用XRCed生成界面的简单示例。

1.进入XRCed, 创建wxFrame。(因为CSDN现在不能传图,只描述一下)

再创建一个横排的wxBoxSizer,里面放一个textCtrl, 一个竖排的wxBoxSizer,里面放一个按钮。在不选中任何组件时,将编码设为UTF-8. 否则对中文字符无法保存xrc文件。

配置gettext,以生成多种语言界面。(支持I18N)

设置完毕,保存为hello.xrc


  1. <?xml version=“1.0″ encoding=“UTF-8″?>  
  2. <resource class=“wxButton”>  
  3.   <object class=“wxFrame” name=“zframe”>  
  4.     <object class=“wxBoxSizer”>  
  5.       <orient>wxHORIZONTAL</orient>  
  6.       <object class=“sizeritem”>  
  7.         <object class=“wxTextCtrl” name=“txt_main”>  
  8.           <size>300,300</size>  
  9.           <value>hello</value>  
  10.           <mce:style><!– 
  11. wxTE_AUTO_SCROLL|wxTE_MULTILINE 
  12. –></mce:style><style mce_bogus=“1″>wxTE_AUTO_SCROLL|wxTE_MULTILINE</style>  
  13.         </object>  
  14.         <option>4</option>  
  15.         <flag>wxEXPAND</flag>  
  16.         <border>2</border>  
  17.       </object>  
  18.       <object class=“sizeritem”>  
  19.         <object class=“wxBoxSizer”>  
  20.           <orient>wxVERTICAL</orient>  
  21.           <object class=“sizeritem”>  
  22.             <object class=“wxButton” name=“btn_next”>  
  23.               <label>下一个</label>  
  24.               <XRCED>  
  25.                 <events>EVT_BUTTON|EVT_KEY_DOWN</events>  
  26.                 <assign_var>1</assign_var>  
  27.               </XRCED>  
  28.             </object>  
  29.           </object>  
  30.         </object>  
  31.       </object>  
  32.     </object>  
  33.     <title>你好</title>  
  34.   </object>  
  35. </resource>  

2.自动生成hello_xrc.py


  1. # This file was automatically generated by pywxrc.  
  2. # -*- coding: UTF-8 -*-  
  3. import wx  
  4. import wx.xrc as xrc  
  5. __res = None  
  6. def get_resources():  
  7.     “”" This function provides access to the XML resources in this module.”"”  
  8.     global __res  
  9.     if __res == None:  
  10.         __init_resources()  
  11.     return __res  
  12.   
  13.   
  14. class xrczframe(wx.Frame):  
  15. #!XRCED:begin-block:xrczframe.PreCreate  
  16.     def PreCreate(self, pre):  
  17.         “”" This function is called during the class’s initialization. 
  18.          
  19.         Override it for custom setup before the window is created usually to 
  20.         set additional window styles using SetWindowStyle() and SetExtraStyle(). 
  21.         ”"”  
  22.         pass  
  23.           
  24. #!XRCED:end-block:xrczframe.PreCreate  
  25.     def __init__(self, parent):  
  26.         # Two stage creation (see http://wiki.wxpython.org/index.cgi/TwoStageCreation)  
  27.         pre = wx.PreFrame()  
  28.         self.PreCreate(pre)  
  29.         get_resources().LoadOnFrame(pre, parent, “zframe”)  
  30.         self.PostCreate(pre)  
  31.         # Define variables for the controls, bind event handlers  
  32.         self.btn_next = xrc.XRCCTRL(self“btn_next”)  
  33.         self.Bind(wx.EVT_BUTTON, self.OnButton_btn_next, self.btn_next)  
  34.         self.Bind(wx.EVT_KEY_DOWN, self.OnKey_down_btn_next, self.btn_next)  
  35. #!XRCED:begin-block:xrczframe.OnButton_btn_next  
  36.     def OnButton_btn_next(self, evt):  
  37.         # Replace with event handler code  
  38.         print “OnButton_btn_next()”  
  39.         self.txt_main = xrc.XRCCTRL(self“txt_main”)  
  40.         self.txt_main.SetValue(“你好”)  
  41. #!XRCED:end-block:xrczframe.OnButton_btn_next          
  42. #!XRCED:begin-block:xrczframe.OnKey_down_btn_next  
  43.     def OnKey_down_btn_next(self, evt):  
  44.         # Replace with event handler code  
  45.         print “OnKey_down_btn_next()”  
  46. #!XRCED:end-block:xrczframe.OnKey_down_btn_next          
  47.   
  48.   
  49. # ———————— Resource data ———————-  
  50. def __init_resources():  
  51.     global __res  
  52.     __res = xrc.EmptyXmlResource()  
  53.     wx.FileSystem.AddHandler(wx.MemoryFSHandler())  
  54.     hello_xrc = ”’\ 
  55. <?xml version=”1.0″ ?><resource class=”wxButton”> 
  56.   <object class=”wxFrame” name=”zframe”> 
  57.     <object class=”wxBoxSizer”> 
  58.       <orient>wxHORIZONTAL</orient> 
  59.       <object class=”sizeritem”> 
  60.         <object class=”wxTextCtrl” name=”txt_main”> 
  61.           <size>300,300</size> 
  62.           <value>hello</value> 
  63.           <mce:style><!– 
  64. wxTE_AUTO_SCROLL|wxTE_MULTILINE 
  65. –></mce:style><style mce_bogus=”1″>wxTE_AUTO_SCROLL|wxTE_MULTILINE</style> 
  66.         </object> 
  67.         <option>4</option> 
  68.         <flag>wxEXPAND</flag> 
  69.         <border>2</border> 
  70.       </object> 
  71.       <object class=”sizeritem”> 
  72.         <object class=”wxBoxSizer”> 
  73.           <orient>wxVERTICAL</orient> 
  74.           <object class=”sizeritem”> 
  75.             <object class=”wxButton” name=”btn_next”> 
  76.               <label>下一个</label> 
  77.               <XRCED> 
  78.                 <events>EVT_BUTTON|EVT_KEY_DOWN</events> 
  79.                 <assign_var>1</assign_var> 
  80.               </XRCED> 
  81.             </object> 
  82.           </object> 
  83.         </object> 
  84.       </object> 
  85.     </object> 
  86.     <title>你好</title> 
  87.   </object> 
  88. </resource>”’  
  89.     wx.MemoryFSHandler.AddFile(‘XRC/hello/hello_xrc’, hello_xrc)  
  90.     __res.Load(‘memory:XRC/hello/hello_xrc’)  
  91.   
  92. # ———————– Gettext strings ———————  
  93. def __gettext_strings():  
  94.     # This is a dummy function that lists all the strings that are used in  
  95.     # the XRC file in the _(“a string”) format to be recognized by GNU  
  96.     # gettext utilities (specificaly the xgettext utility) and the  
  97.     # mki18n.py script.  For more information see:  
  98.     # http://wiki.wxpython.org/index.cgi/Internationalization   
  99.       
  100.     def _(str): pass  
  101.       
  102.     _(“hello”)  
  103.     _(“下一个”)  
  104.     _(“你好”)  

3.实现按钮点击,改变txt的内容。

找到def OnButton_btn_next(self, evt):

在下面添加:

        self.txt_main = xrc.XRCCTRL(self, “txt_main”)
        self.txt_main.SetValue(“你好”)

4.完成可执行程序。

新建一个testhello.py


  1. #!/bin/env python  
  2. #-*- coding=utf8 -*-  
  3. import wx  
  4. import hello_xrc  
  5. app = wx.PySimpleApp()  
  6. frame = hello_xrc.xrczframe(parent = None)  
  7. frame.Show()  
  8. app.MainLoop()  

执行testhello.py,即可看到设计的界面。

参考:

xrced官网(只有源码下载):http://xrced.sourceforge.net/

wxpython的doc里包括xrced的可执行文件

http://www.wxpython.org/download.php

win32二进制版:

http://downloads.sourceforge.net/wxpython/wxPython2.8-win32-docs-demos-2.8.10.1.exe

ubuntu:可以在库里安装wxpython时自带

金庆的专栏

http://blog.csdn.net/jq0123/archive/2008/03/24/2213855.aspx

http://blog.csdn.net/jq0123/archive/2008/03/26/2219836.aspx


原创文章,转载请注明出自瀚海星空.
本文链接地址:http://abloz.com/2010/01/09/wxpython-interface-created-using-xrced.html