我想在 for 循环中创建和启动多个计时器。我的方法如下:
import wx
trials = range(1, 3)
timers = range(7)
name = 'timer'
class TimersClass(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
panel = wx.Panel(self)
self.button1 = wx.Button(panel, label = 'Go')
self.Bind(wx.EVT_BUTTON, self.Timers, self.button1)
def Timers(self, event):
for trial in trials:
for timer in timers:
setattr(self, name + str(timer) + '_' + 'iteration' + str(trial), wx.Timer(self))
print name + str(timer) + '_' + 'iteration' + str(trial)
eval(name + str(timer) + '_' + 'iteration' + str(trial) + '.Start(' + str(timer * 1000, ) + ', OneShoot = True)')
self.Bind(wx.EVT_TIMER, self.Hi)
def Hi(self, event):
print 'Hi, bastard!'
app = wx.App()
frame = TimersClass(None)
frame.Center()
frame.Show()
app.MainLoop()
但是 self.timers 对象似乎没有被创建:
回溯(最后一次调用):文件“Escritorio/iteration_timers.py”,第 24 行,在 Timers eval(name + str(timer) + '_' + 'iteration' + str(trial) + '.Start(' + str(timer * 1000, ) + ', OneShoot = True)') 文件“”,第 1 行,在 NameError 中:名称 'timer0_iteration1' 未定义
有人知道为什么这段代码不能运行,或者有其他方法吗?
非常感谢!!