Wxpython 简明教程

wxPython - GUI Builder Tools

通过手动编码创建出色的 GUI 可能很繁琐。一个可视化 GUI 设计器工具总是很方便的。针对 wxPython 的许多 GUI 开发 IDE 都可用。下面是其中一些 −

Creating a good looking GUI by manual coding can be tedious. A visual GUI designer tool is always handy. Many GUI development IDEs targeted at wxPython are available. Following are some of them −

  1. wxFormBuilder

  2. wxDesigner

  3. wxGlade

  4. BoaConstructor

  5. gui2py

wxFormBuilder 是一个开源、跨平台的所见即所得 (WYSIWYG) GUI 构建器,可以将 wxWidget GUI 设计转换为 C++、Python、PHP 或 XML 格式。此处给出了有关 wxFormBuilder 用法的简要介绍。

wxFormBuilder is an open source, cross-platform WYSIWYG GUI builder that can translate the wxWidget GUI design into C++, Python, PHP or XML format. A brief introduction to usage of wxFormBuilder is given here.

首先,需要从 http://sourceforge.net/projects/wxformbuilder/. 下载并安装 wxFormBuilder 的最新版本。打开应用程序后,会出现一个新项目,中间有一个空白灰色区域。

First of all the latest version of wxFormBuilder needs to be downloaded and installed from http://sourceforge.net/projects/wxformbuilder/. On opening the application, a new project with blank grey area at the center appears.

为项目提供一个合适的名称,并将 Python 选择为代码生成语言。这是在对象属性窗口中完成的,如下图所示 −

Give a suitable name to the project and choose Python as code generation language. This is done in the Object properties window as shown in the following image −

object properties

然后,从组件选板的“Forms”选项卡,选择 Frame。

Then from ‘Forms’ tab of components palette, choose Frame.

frame

从“Layouts”选项卡添加垂直的 wxBoxSizer。

Add a vertical wxBoxSizer from ‘Layouts’ tab.

wxboxsizer

在 Box 中添加带有合适标题的必要控件。在此,添加了一个 StaticText(标签)、两个 TextCtrl 对象(文本框)和一个 wxButton 对象。框架类似于以下图片:

Add necessary controls in the Box with suitable captions. Here, a StaticText (label), two TextCtrl objects (text boxes) and a wxButton object are added. The frame looks like the following image −

add controls

对这三个控件启用“Expand and Stretch”。在 wxButton 对象的对象属性中,为 OnButtonClick 事件指定一个函数 findsquare()。

Enable Expand and Stretch on these three controls. In the object properties for wxButton object, assign a function findsquare() to OnButtonClick event.

three controls

保存项目并按 F8 以生成已开发 GUI 的 Python 代码。生成的代码命名为 Demo.py

Save the project and press F8 to generate Python code for developed GUI. Let the generated file be named as Demo.py

在可执行 Python 脚本中,导入 demo.py 并定义 FindSquare() 函数。声明 Application 对象并启动一个主事件循环。以下为可执行代码:

In the executable Python script, import demo.py and define FindSquare() function. Declare Application object and start a main event loop. Following is the executable code −

import wx

#import the newly created GUI file
import demo
class CalcFrame(demo.MyFrame1):
   def __init__(self,parent):
      demo.MyFrame1.__init__(self,parent)

   def FindSquare(self,event):
      num = int(self.m_textCtrl1.GetValue())
      self.m_textCtrl2.SetValue (str(num*num))

app = wx.App(False)
frame = CalcFrame(None)
frame.Show(True)
#start the applications
app.MainLoop()

上述代码生成以下输出 -

The above code produces the following output −

tools output