How to Learn wxPython: http://wiki.wxpython.org/How%20to%20Learn%20wxPython
Getting started with wxPython 1:http://wiki.wxpython.org/Getting%20Started
1. 两个ID符号:wxID_ANY, wxID_NONE (http://docs.wxwidgets.org/2.8.12/wx_stdevtid.html)
wxWidgets defines a special identifier value wxID_ANY which is used in the following two situations:
1) when creating a new window you may specify wxID_ANY to let wxWidgets assign an unused identifier to it automatically
2) when installing an event handler using either the event table macros or wxEvtHandler::Connect, you may use it to indicate that you want to handle the events coming from any control, regardless of its identifier
Another standard special identifier value is wxID_NONE: this is a value which is not matched by any other id.
wxWidgets also defines a few standard command identifiers which may be used by the user code and also are sometimes used by wxWidgets itself. These reserved identifiers are all in the range between wxID_LOWEST and wxID_HIGHEST and, accordingly, the user code should avoid defining its own constants in this range.
2.wx.Window 是所有其他可视元素(button,menu等)的基类,wx.Frame是应用程序的顶层窗口。
A wx.Window is the base class from which all visual elements are derived (buttons, menus, etc) and what we normally think of as a program window is a wx.Frame. This is an unfortunate inconsistency that has led to much confusion for new users.
3. 获取wx支持的完整事件ID列表:
import wx
for x in dir(wx):
if x.startswith('EVT_'):
print x
class MainWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self,parent, title=title, size=(200,100))
...
menuItem = filemenu.Append(wx.ID_ABOUT, "&About"," Information about this program")
self.Bind(wx.EVT_MENU, self.OnAbout, menuItem)
5.事件处理函数有统一的参数形式;通过event.Skip()可跳过事件的处理。
def OnAbout(self, e):
if (some_condition):
do_something()
else:
e.Skip()
def OnOpen(self,e):
""" Open a file"""
self.dirname = ''
dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", wx.OPEN)
if dlg.ShowModal() == wx.ID_OK:
self.filename = dlg.GetFilename()
self.dirname = dlg.GetDirectory()
f = open(os.path.join(self.dirname, self.filename), 'r')
self.control.SetValue(f.read())
f.close()
dlg.Destroy()