如何从一个函数调用变量到另一个函数?
How to call a variable from one function to another?
在 wxPython 中,我试图从一个函数(在实例化器中)定义一个变量来调用另一个函数(不在实例化器中,但仍在 class 中)
我见过有人在他们的情况下解决其他人的问题,我也尝试过其他人,但它对我不起作用。
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
欢迎来到 Whosebug。
实现你想要的最简单的方法是在创建 wx.TextCtrl
时使用 self
,这样它就可以从 __init__
以外的方法获得,然后直接访问wx.TextCtrl
来自其他方法。
import wx
class NamePrompts(wx.Frame):
def __init__(self, *args, **kw):
super(NamePrompts, self).__init__(*args, **kw)
panl = wx.Panel(self)
self.textboxtest = wx.TextCtrl(panl) # defining textboxtest as a text box
#self.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 = self.textboxtest.GetValue() # attempting to call the same textbox here
print(typedtext)
wx.StaticText(wx.Panel(self), label=typedtext, pos=(25, 25))
if __name__ == '__main__':
app = wx.App()
frm = NamePrompts(None, title='Basketball Game')
frm.SetSize(50,50,300,300)
frm.Show()
app.MainLoop()
不过,如果您想了解如何通过事件传递自定义变量,您可以查看 lambda functions and the partial module.
的用法
在 wxPython 中,我试图从一个函数(在实例化器中)定义一个变量来调用另一个函数(不在实例化器中,但仍在 class 中)
我见过有人在他们的情况下解决其他人的问题,我也尝试过其他人,但它对我不起作用。
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
欢迎来到 Whosebug。
实现你想要的最简单的方法是在创建 wx.TextCtrl
时使用 self
,这样它就可以从 __init__
以外的方法获得,然后直接访问wx.TextCtrl
来自其他方法。
import wx
class NamePrompts(wx.Frame):
def __init__(self, *args, **kw):
super(NamePrompts, self).__init__(*args, **kw)
panl = wx.Panel(self)
self.textboxtest = wx.TextCtrl(panl) # defining textboxtest as a text box
#self.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 = self.textboxtest.GetValue() # attempting to call the same textbox here
print(typedtext)
wx.StaticText(wx.Panel(self), label=typedtext, pos=(25, 25))
if __name__ == '__main__':
app = wx.App()
frm = NamePrompts(None, title='Basketball Game')
frm.SetSize(50,50,300,300)
frm.Show()
app.MainLoop()
不过,如果您想了解如何通过事件传递自定义变量,您可以查看 lambda functions and the partial module.
的用法