在wxPython中,我试图从一个函数(在实例化器中)定义一个变量来调用另一个函数(不在实例化器中,但仍然在类中)
我见过有人在他们的处境中解决别人的问题,我也尝试过其他人,但这对我不起作用。你知道吗
class NamePrompts(wx.Frame):
def __init__(self, *args, **kw):
super(NamePrompts, self).__init__(*args, **kw)
panl = wx.Panel(self)
textboxtest = wx.TextCtrl(panl) # defining textboxtest as a text box
textboxtest.Bind(wx.EVT_TEXT, self.OnKeyTyped)
read = wx.Button(panl, label="Print", pos=(0, 25))
read.Bind(wx.EVT_BUTTON, self.PrintText)
def PrintText(self, event):
typedtext = event.textboxtest.GetString() # attempting to call the same textbox here
wx.StaticText(wx.Panel(self), label=typedtext, pos=(25, 25))
if __name__ == '__main__':
app = wx.App()
frm = NamePrompts(None, title='Basketball Game')
frm.SetSize(0,0,1920,1040)
frm.Show()
app.MainLoop()
我得到这个错误:
AttributeError: 'CommandEvent' object has no attribute 'textboxtest'
Traceback (most recent call last):
File "textboxtest.py", line 19, in PrintText
typedtest = event.textboxtest.GetString() # attempting to call the same text box here
欢迎使用StackOverflow。你知道吗
最简单的方法是在创建
wx.TextCtrl
时使用self
,这样它就可以从__init__
以外的方法获得,然后直接从其他方法访问wx.TextCtrl
的值。你知道吗不过,如果您想学习如何通过事件传递自定义变量,可以检查lambda functions和partial module的用法。你知道吗
相关问题 更多 >
编程相关推荐