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

