AttributeError: 'Window' object has no attribute 'pressed'
AttributeError: 'Window' object has no attribute 'pressed'
我试过删除 pressed() 中的事件参数并移动 pressed() 的位置,但没有任何效果...我完全卡住了。
我试过的代码:
import wx
class Window ( wx.Frame ):
def __init__ ( self ):
super().__init__ ( parent = None, title = "Hello World!" )
panel = wx.Panel ( self )
sizer = wx.BoxSizer ( wx.VERTICAL )
self.TextBox = wx.TextCtrl ( panel, pos = ( 5, 5 ), style = wx.TE_MULTILINE | wx.TE_AUTO_URL )
sizer.Add ( self.TextBox, 0, wx.ALL | wx.EXPAND, 5 )
self.button = wx.Button ( panel, label = "Button", pos = ( 5, 40 ) )
self.button.Bind ( wx.EVT_BUTTON, self.pressed )
sizer.Add ( self.button, 0, wx.ALL | wx.CENTER, 5 )
def pressed ( self, event ):
text = self.TextBox.GetValue()
print ( text )
panel.SetSizer ( sizer )
self.Show()
if __name__ == '__main__':
app = wx.App()
window = Window()
app.MainLoop()
错误:
Traceback (most recent call last):
File "E:\Dell\Python\WxPython.py", line 27, in <module>
window = Window()
File "E:\Dell\Python\WxPython.py", line 15, in __init__
self.button.Bind ( wx.EVT_BUTTON, self.pressed )
AttributeError: 'Window' object has no attribute 'pressed'
您按下的方法缩进过多。它必须与 init 处于同一级别。否则它将是 init 的局部函数,仅在 init 中可见,并且在 self.button.Bind 调用后实际有效。
import wx
class Window ( wx.Frame ):
def __init__ ( self ):
super().__init__ ( parent = None, title = "Hello World!" )
panel = wx.Panel ( self )
sizer = wx.BoxSizer ( wx.VERTICAL )
self.TextBox = wx.TextCtrl ( panel, pos = ( 5, 5 ), style = wx.TE_MULTILINE | wx.TE_AUTO_URL )
sizer.Add ( self.TextBox, 0, wx.ALL | wx.EXPAND, 5 )
self.button = wx.Button ( panel, label = "Button", pos = ( 5, 40 ) )
self.button.Bind ( wx.EVT_BUTTON, self.pressed )
sizer.Add ( self.button, 0, wx.ALL | wx.CENTER, 5 )
panel.SetSizer ( sizer )
self.Show()
def pressed ( self, event ):
text = self.TextBox.GetValue()
print ( text )
if __name__ == '__main__':
app = wx.App()
window = Window()
app.MainLoop()
我试过删除 pressed() 中的事件参数并移动 pressed() 的位置,但没有任何效果...我完全卡住了。
我试过的代码:
import wx
class Window ( wx.Frame ):
def __init__ ( self ):
super().__init__ ( parent = None, title = "Hello World!" )
panel = wx.Panel ( self )
sizer = wx.BoxSizer ( wx.VERTICAL )
self.TextBox = wx.TextCtrl ( panel, pos = ( 5, 5 ), style = wx.TE_MULTILINE | wx.TE_AUTO_URL )
sizer.Add ( self.TextBox, 0, wx.ALL | wx.EXPAND, 5 )
self.button = wx.Button ( panel, label = "Button", pos = ( 5, 40 ) )
self.button.Bind ( wx.EVT_BUTTON, self.pressed )
sizer.Add ( self.button, 0, wx.ALL | wx.CENTER, 5 )
def pressed ( self, event ):
text = self.TextBox.GetValue()
print ( text )
panel.SetSizer ( sizer )
self.Show()
if __name__ == '__main__':
app = wx.App()
window = Window()
app.MainLoop()
错误:
Traceback (most recent call last):
File "E:\Dell\Python\WxPython.py", line 27, in <module>
window = Window()
File "E:\Dell\Python\WxPython.py", line 15, in __init__
self.button.Bind ( wx.EVT_BUTTON, self.pressed )
AttributeError: 'Window' object has no attribute 'pressed'
您按下的方法缩进过多。它必须与 init 处于同一级别。否则它将是 init 的局部函数,仅在 init 中可见,并且在 self.button.Bind 调用后实际有效。
import wx
class Window ( wx.Frame ):
def __init__ ( self ):
super().__init__ ( parent = None, title = "Hello World!" )
panel = wx.Panel ( self )
sizer = wx.BoxSizer ( wx.VERTICAL )
self.TextBox = wx.TextCtrl ( panel, pos = ( 5, 5 ), style = wx.TE_MULTILINE | wx.TE_AUTO_URL )
sizer.Add ( self.TextBox, 0, wx.ALL | wx.EXPAND, 5 )
self.button = wx.Button ( panel, label = "Button", pos = ( 5, 40 ) )
self.button.Bind ( wx.EVT_BUTTON, self.pressed )
sizer.Add ( self.button, 0, wx.ALL | wx.CENTER, 5 )
panel.SetSizer ( sizer )
self.Show()
def pressed ( self, event ):
text = self.TextBox.GetValue()
print ( text )
if __name__ == '__main__':
app = wx.App()
window = Window()
app.MainLoop()