周海汉 /文
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
2.自动生成hello_xrc.py
class xrczframe(wx.Frame):
#!XRCED:begin-block:xrczframe.PreCreate
def PreCreate(self, pre):
""" This function is called during the class's initialization.
Override it for custom setup before the window is created usually to
set additional window styles using SetWindowStyle() and SetExtraStyle().
"""
pass
#!XRCED:end-block:xrczframe.PreCreate
def __init__(self, parent):
# Two stage creation (see http://wiki.wxpython.org/index.cgi/TwoStageCreation)
pre = wx.PreFrame()
self.PreCreate(pre)
get_resources().LoadOnFrame(pre, parent, "zframe")
self.PostCreate(pre)
# Define variables for the controls, bind event handlers
self.btn_next = xrc.XRCCTRL(self, "btn_next")
self.Bind(wx.EVT_BUTTON, self.OnButton_btn_next, self.btn_next)
self.Bind(wx.EVT_KEY_DOWN, self.OnKey_down_btn_next, self.btn_next)
#!XRCED:begin-block:xrczframe.OnButton_btn_next
def OnButton_btn_next(self, evt):
# Replace with event handler code
print "OnButton_btn_next()"
self.txt_main = xrc.XRCCTRL(self, "txt_main")
self.txt_main.SetValue("你好")
#!XRCED:end-block:xrczframe.OnButton_btn_next
#!XRCED:begin-block:xrczframe.OnKey_down_btn_next
def OnKey_down_btn_next(self, evt):
# Replace with event handler code
print "OnKey_down_btn_next()"
#!XRCED:end-block:xrczframe.OnKey_down_btn_next
# ------------------------ Resource data ----------------------
def __init_resources():
global __res
__res = xrc.EmptyXmlResource()
wx.FileSystem.AddHandler(wx.MemoryFSHandler())
hello_xrc = '''/
<?xml version="1.0" ?><resource class="wxButton">
<object class="wxFrame" name="zframe">
<object class="wxBoxSizer">
<orient>wxHORIZONTAL</orient>
<object class="sizeritem">
<object class="wxTextCtrl" name="txt_main">
<size>300,300</size>
<value>hello</value>
<mce:style><!--
wxTE_AUTO_SCROLL|wxTE_MULTILINE
--></mce:style><style mce_bogus="1">wxTE_AUTO_SCROLL|wxTE_MULTILINE</style>
</object>
<option>4</option>
<flag>wxEXPAND</flag>
<border>2</border>
</object>
<object class="sizeritem">
<object class="wxBoxSizer">
<orient>wxVERTICAL</orient>
<object class="sizeritem">
<object class="wxButton" name="btn_next">
<label>下一个</label>
<XRCED>
<events>EVT_BUTTON|EVT_KEY_DOWN</events>
<assign_var>1</assign_var>
</XRCED>
</object>
</object>
</object>
</object>
</object>
<title>你好</title>
</object>
</resource>'''
wx.MemoryFSHandler.AddFile('XRC/hello/hello_xrc', hello_xrc)
__res.Load('memory:XRC/hello/hello_xrc')
# ----------------------- Gettext strings ---------------------
def __gettext_strings():
# This is a dummy function that lists all the strings that are used in
# the XRC file in the _("a string") format to be recognized by GNU
# gettext utilities (specificaly the xgettext utility) and the
# mki18n.py script. For more information see:
# http://wiki.wxpython.org/index.cgi/Internationalization
def _(str): pass
_("hello")
_("下一个")
_("你好")
3.实现按钮点击,改变txt的内容。
找到def OnButton_btn_next(self, evt):
在下面添加:
self.txt_main = xrc.XRCCTRL(self, "txt_main")
self.txt_main.SetValue("你好")
4.完成可执行程序。
新建一个testhello.py
执行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