<p>+1用于包含到完整源代码的链接-使测试更加容易。在</p>
<p>我无法重现您在Win32上wxpython2.8.12上描述的错误,但在运行代码时,我发现在<code>pyed.py</code>第18行创建了一个看似无关的<code>wx.Panel</code>对象。。。在</p>
<pre><code>self.panel = wx.Panel(self, -1)
</code></pre>
<p>…这似乎妨碍了程序的正确运行。在把那句话注释掉之后,它似乎工作得很好。在</p>
<hr/>
<p>我还注意到其他一些事情:56号线。。。在</p>
^{pr2}$
<p>…可能应该放在前面的if块中,否则如果用户在<code>wx.FileDialog</code>上单击“Cancel”并在第16行上单击“Cancel”,您将得到一个错误。。。在</p>
^{3}$
<p>…如果使用关键字参数而不是位置参数。。。在</p>
<pre><code>wx.Frame.__init__(self, parent=parent, id=id, title='PyEd', size=wx.Size(640, 480))
</code></pre>
<p>…您不必为窗口位置重新指定默认值,这也稍微安全一些,以防wxPython开发人员决定在将来的版本中更改默认值。在</p>
<p>您还可以计算出常量值,以及可选地创建<code>wx.Size</code>对象来将该行缩减为。。。在</p>
<pre><code>wx.Frame.__init__(self, parent=None, title='PyEd', size=(640, 480))
</code></pre>
<hr/>
<p>最后,关于id:在大多数情况下,您可能会发现它们没有什么用处。它们派上用场的地方就是需要许多类似控件的地方,而让它们由单个事件处理程序函数处理更有意义。在</p>
<p>考虑这个例子。。。在</p>
<pre><code>def create_buttons(parent):
parent.button1 = wx.Button(label='Button 1')
parent.button2 = wx.Button(label='Button 2')
parent.button3 = wx.Button(label='Button 3')
parent.button1.Bind(wx.EVT_BUTTON, on_button_1)
parent.button2.Bind(wx.EVT_BUTTON, on_button_2)
parent.button3.Bind(wx.EVT_BUTTON, on_button_3)
def on_button_1(event):
print 'You clicked button 1'
def on_button_2(event):
print 'You clicked button 2'
def on_button_3(event):
print 'You clicked button 3'
</code></pre>
<p>…这很好,但是如果你需要,比如说,100个按钮,你可能更喜欢这样实现它。。。在</p>
<pre><code>def create_buttons(parent):
parent.buttons = [wx.Button(id=i, label='Button %d' % i) for i in range(100)]
parent.Bind(wx.EVT_BUTTON, on_button)
def on_button(event):
button_id = event.GetId()
print 'You clicked button %d' % button_id
</code></pre>
<p>哦,小心使用<code>id</code>作为变量名,因为它也是一个Python内置函数名。在</p>