1.源代码:
import wx
class Frame1(wx.Frame):
def __init__(self, superior):
wx.Frame.__init__(self, parent=superior, title='Example', pos=(100, 200), size=(350, 200))
panel = wx.Panel(self)
text1 = wx.TextCtrl(panel, value='Hello, World!', size=(350, 200))
self.panel.Bind(wx.EVT_LEFT_UP, self.OnClick)
def OnClick(self, event):
posm = event.GetPosition()
wx.StaticText(parent = self.panel, label = 'Hello, World', pos = (posm.x, posm.y))
if __name__ == '__main__':
app = wx.App()
frame = Frame1(None)
frame.Show(True)
app.MainLoop()
2.报错:
3.报错原因分析:
- 类Frame1中没有pannel成员,此处的pannel成员指的是类成员self.panel,即在使用self.panel时没有事先定义。故在使用self.panel.Bind(...)时报错。
4.报错解决:
- 将前面的panel临时变量改为Frame1的类成员变量 self.panel.后续出现panel的地方参照此规则改即可。
import wx
class Frame1(wx.Frame):
def __init__(self, superior):
wx.Frame.__init__(self, parent=superior, title='Example', pos=(100, 200), size=(350, 200))
self.panel = wx.Panel(self)
self.panel.Bind(wx.EVT_LEFT_UP, self.OnClick)
def OnClick(self, event):
posm = event.GetPosition()
wx.StaticText(parent = self.panel, label = 'Hello, World', pos = (posm.x, posm.y))
if __name__ == '__main__':
app = wx.App()
frame = Frame1(None)
frame.Show(True)
app.MainLoop()