wxpython 使用 XRCed创建界面

abloz 2010-01-09
2010-01-09

周海汉 /文

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. wxHORIZONTAL
  3. 300,300
  4. hello
  5. <!--
  6. wxTE_AUTO_SCROLL wxTE_MULTILINE
  7. –></mce:style>
  8. </object>
  9. wxEXPAND
  10. 2
  11. </object>
  12. wxVERTICAL
  13. EVT_BUTTON|EVT_KEY_DOWN
  14. 1
  15. </XRCED>
  16. </object>
  17. </object>
  18. </object>
  19. </object>
  20. </object>
  21. 你好
  22. </object>
  23. </resource>

<?xml version=”1.0” encoding=”UTF-8”?> wxHORIZONTAL 300,300 hello wxEXPAND 2 wxVERTICAL EVT_BUTTON|EVT_KEY_DOWN 1 你好

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
      1. class xrczframe(wx.Frame):
  12. #!XRCED:begin-block:xrczframe.PreCreate
  13. def PreCreate(self, pre):
  14. ””” This function is called during the class’s initialization.
    1. Override it for custom setup before the window is created usually to
  15. set additional window styles using SetWindowStyle() and SetExtraStyle().
  16. ”””
  17. pass
    1. #!XRCED:end-block:xrczframe.PreCreate
  18. def init(self, parent):
  19. Two stage creation (see http://wiki.wxpython.org/index.cgi/TwoStageCreation)

  20. pre = wx.PreFrame()
  21. self.PreCreate(pre)
  22. get_resources().LoadOnFrame(pre, parent, “zframe”)
  23. self.PostCreate(pre)
  24. Define variables for the controls, bind event handlers

  25. self.btn_next = xrc.XRCCTRL(self, “btn_next”)
  26. self.Bind(wx.EVT_BUTTON, self.OnButton_btn_next, self.btn_next)
  27. self.Bind(wx.EVT_KEY_DOWN, self.OnKey_down_btn_next, self.btn_next)
  28. #!XRCED:begin-block:xrczframe.OnButton_btn_next
  29. def OnButton_btn_next(self, evt):
  30. Replace with event handler code

  31. print “OnButton_btn_next()”
  32. self.txt_main = xrc.XRCCTRL(self, “txt_main”)
  33. self.txt_main.SetValue(“你好”)
  34. #!XRCED:end-block:xrczframe.OnButton_btn_next
  35. #!XRCED:begin-block:xrczframe.OnKey_down_btn_next
  36. def OnKey_down_btn_next(self, evt):
  37. Replace with event handler code

  38. print “OnKey_down_btn_next()”
  39. #!XRCED:end-block:xrczframe.OnKey_down_btn_next
      1. ———————— Resource data ———————-

  40. def __init_resources():
  41. global __res
  42. __res = xrc.EmptyXmlResource()
  43. wx.FileSystem.AddHandler(wx.MemoryFSHandler())
  44. hello_xrc = ‘’’’’
  45. <?xml version=”1.0” ?>
  46. wxHORIZONTAL
  47. 300,300
  48. hello
  49. <!--
  50. wxTE_AUTO_SCROLL wxTE_MULTILINE
  51. –></mce:style>
  52. </object>
  53. wxEXPAND
  54. 2
  55. </object>
  56. &nbs; p;
  57. wxVERTICAL
  58. EVT_BUTTON|EVT_KEY_DOWN
  59. 1
  60. </XRCED>
  61. </object>
  62. </object>
  63. </object>
  64. </object>
  65. </object>
  66. 你好
  67. </object>
  68. </resource>’’’
  69. wx.MemoryFSHandler.AddFile(‘XRC/hello/hello_xrc’, hello_xrc)
  70. __res.Load(‘memory:XRC/hello/hello_xrc’)
    1. ———————– Gettext strings ———————

  71. def __gettext_strings():
  72. This is a dummy function that lists all the strings that are used in

  73. the XRC file in the _(“a string”) format to be recognized by GNU

  74. gettext utilities (specificaly the xgettext utility) and the

  75. mki18n.py script. For more information see:

  76. http://wiki.wxpython.org/index.cgi/Internationalization

    1. def _(str): pass
    1. _(“hello”)
  77. _(“下一个”)
  78. _(“你好”)

This file was automatically generated by pywxrc. # -- coding: UTF-8 -- import wx import wx.xrc as xrc res = None def get_resources(): “”” This function provides access to the XML resources in this module.””” global __res if __res == None: __init_resources() return __res 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” ?> wxHORIZONTAL 300,300 hello wxEXPAND 2 wxVERTICAL EVT_BUTTON|EVT_KEY_DOWN 1 </obj

ect> </object> </object> 你好 </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


  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()

#!/bin/env python #-- coding=utf8 -- import wx import hello_xrc app = wx.PySimpleApp() frame = hello_xrc.xrczframe(parent = None) frame.Show() 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


如非注明转载, 均为原创. 本站遵循知识共享CC协议,转载请注明来源