编写wxPython程序代码风格的说明
本文是关于编写wxPython程序代码基本风格的说明,虽然并不能完全教会你如何编写wxPython的程序,但是尽量介绍一种清晰和Pythonic的方式来组织你的代码。
原文:Style Guide for wxPython code
http://wiki.wxpython.org/index.cgi/wxPython_Style_Guide
1. 使用 import wx;不使用 from wx import *或from wxPython.wx import *
同样,在使用其他库时,不要使用 import *
因为:命名空间会被污染
对于包结构复杂的模块,可以使用如下方式:

from wx.lib.SomeLib import SomeModule
没有希望敲如下的代码:

AnObject = wx.lib.SomeLib.SomeModule.SomeClass()
2. 构造函数传入参数时,使用关键字
如果在你的类的构造函数中使用了很多并未用到的缺省参数,
例如:wx.DefaultSize, wx.DefaultPosition, wx.ID_ANY, etc. 尽量在构造函数的参数列表中使用关键字:

MainFrame = wx.Frame(None, title = " A Title " , size = ( 500 , 400 ))
因为:看上去更加直观。
2b.从 wx.Windows 派生子类的时候,使用 *args 和 **kwargs

class MyPanel(wx.Panel):

""" This Panel does some custom thing """

def __init__ (self, * args, ** kwargs):

""" Create the DemoPanel. """

wx.Panel. __init__ (self, * args, ** kwargs)

这样,使你的派生类的构造函数与其父类具有相同的参数列表。
3. 不要使用ID,因为几乎找不到一个好的理由去使用它们
因为:简单总是胜过复杂,Python的精髓
大多数控件的构造函数将会提供缺省的ID值,没有必要去一一指定,其他参数可以使用关键字来标明(同上):

MyFrame = wx.Frame(None, title = " A Title " , size = ( 400 , 400 ))

AButton = wx.Button(self, label = " Push Me " )
如果ID是个必要参数,那么使用 wx.ID_ANY,因为wx.ID_ANY == -1,推荐使用 wx.ID_ANY而不是-1的原因是看上去更加清晰。没准某天 wx.ID_ANY 的值会改变。总之,在任何开发语言中都不要使用纯数字,因为纯数字对于其他开发者来说没有任何意义。
意外的情况:在标准的菜单中,请使用标准的ID,这样就能打开很多相关的功能,例如:menu item remapping for wxMac, automatic Dialog completion or cancellation, using stock button labels and images, etc.(还没接触过,不太懂)。所有标准的ID可以在wxWidgets手册中的“Constants -- Stock items”中找到。例如:

item = FileMenu.Append(wx.ID_EXIT, " &Quit " ),
这样退出菜单就会出现在该出现的地方。
4. 使用 Bind() 函数来绑定消息:
一个按钮的例子:

AButton = wx.Button(self, label = " Push Me " )

AButton.Bind(wx.EVT_BUTTON, self.OnButtonPush)

在菜单中同样可以使用 Bind(),尽管他们没有Bind()方法:

FileMenu = wx.Menu()

item = FileMenu.append(wx.ID_ANY, " &Quit " )

self.Bind(wx.EVT_MENU, self.OnQuit, item)
(其中 self 是指一个 wx.Frame)
5. 使用Sizer
如果你使用 Sizer 而不是坐标来定位的话,你的代码将具有以下特点:
在不同平台下将会运行得更好:不同平台有不同大小的控件
更容易适合不同的语言:不同语言具有不同长度的标签等
就算在同一平台上,当用户更改默认字体和风格时,也能工作得很好
更容易更改,每次增加/修改/删除控件时,其他空间不会受到影响
6. wx.App() 具有同 wx.PySimpleApp() 一样的内建功能
使用前一个就好了
注意:以上在Mac OS X上并不成立。当使用wx.App时,tracebacks会在一个对话框里显示,程序退出时立即消失(除非使用 pythonw -i)。使用 wx.PySimpleApp时,tracebacks重定向到stdout。
7. 使用独立的,定制的类,而不是在一个类中使用很多的 wx.Panels
如果你看到你的 __init__ 中出现如下代码:

self.MainPanel = wx.Panel(self, ...

self.SubPanel1 = wx.Panel(self.MainPanel, ..)d

self.SubPanel2 = wx.Panel(self.SubPanel1, ...)

MyButton = wx.Button(self.SubPanel2, ....)
那么你的代码绝对是烂极了。
除非,你使用如下的代码结构:

class MainPanel(wx.Panel):

...

class SubPanel1(wx.Panel):

...
通常,你会发现,你会有一组自定义空间,满足各种程序的需要,如果你将它们放到自动的 Panel中,你可以重用以前的代码。尽量保持这些控件的独立性,这样你可以在程序的其他部分使用到它们,甚至其他的程序。如果你的控件需要跟其他的控件打交道,那么就需要定义你特定的消息。单元测试的是十分必要,比如写一些demo的程序,仅测试当前的控件和功能。
8. 尽量使用 Python 中的东西,而不是 wx 中的东西
因为:简单总是好过复杂
例如:使用 size=(500, 400),而不是 size=wx.Size(500, 400)
9. 坚持使用 docstrings
实例代码:

# !/usr/bin/env python2.4


# I like to put the python version on the #! line,

# so that I can have multiple versions installed.


"""


This is a small wxPython app developed to demonstrate how to write

Pythonic wxPython code.


"""


import wx


class DemoPanel(wx.Panel):

""" This Panel hold two simple buttons, but doesn't really do anything. """

def __init__ (self, parent, * args, ** kwargs):

""" Create the DemoPanel. """

wx.Panel. __init__ (self, parent, * args, ** kwargs)


self.parent = parent # Sometimes one can use inline Comments


NothingBtn = wx.Button(self, label = " Do Nothing with a long label " )

NothingBtn.Bind(wx.EVT_BUTTON, self.DoNothing )


MsgBtn = wx.Button(self, label = " Send Message " )

MsgBtn.Bind(wx.EVT_BUTTON, self.OnMsgBtn )


Sizer = wx.BoxSizer(wx.VERTICAL)

Sizer.Add(NothingBtn, 0, wx.ALIGN_CENTER | wx.ALL, 5 )

Sizer.Add(MsgBtn, 0, wx.ALIGN_CENTER | wx.ALL, 5 )


self.SetSizerAndFit(Sizer)


def DoNothing(self, event = None):

""" Do nothing. """

pass


def OnMsgBtn(self, event = None):

""" Bring up a wx.MessageDialog with a useless message. """

dlg = wx.MessageDialog(self,

message = ' A completely useless message ' ,

caption = ' A Message Box ' ,

style = wx.OK | wx.ICON_INFORMATION

)

dlg.ShowModal()

dlg.Destroy()


class DemoFrame(wx.Frame):

""" Main Frame holding the Panel. """

def __init__ (self, * args, ** kwargs):

""" Create the DemoFrame. """

wx.Frame. __init__ (self, * args, ** kwargs)


# Build the menu bar

MenuBar = wx.MenuBar()


FileMenu = wx.Menu()


item = FileMenu.Append(wx.ID_EXIT, text = " &Quit " )

self.Bind(wx.EVT_MENU, self.OnQuit, item)


MenuBar.Append(FileMenu, " &File " )

self.SetMenuBar(MenuBar)


# Add the Widget Panel

self.Panel = DemoPanel(self)


self.Fit()


def OnQuit(self, event = None):

""" Exit application. """

self.Close()


if __name__ == ' __main__ ' :

app = wx.App()

frame = DemoFrame(None, title = " Micro App " )

frame.Show()

app.MainLoop()

