forループでn個のタイマーを作成して初期化し、それらをいくつかのイベントにバインドするPythonコードを作成しようとしています。以下のコードは、私が行っている方法の例です。
import wx
#dm1 = {a dewell times values dictionary}
#screens = [a list of dm1 keys]
trials = range(1, 3)
timers = range(0, 4)
class DM1(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
panel = wx.Panel(self)
self.Go = wx.Button(panel, label = 'Go!', pos = (600, 450))
self.Bind(wx.EVT_BUTTON, self.BuildTimers, self.Go)
def BuildTimers(self, event):
self.timers = {}
cum_timer = 0
for trial in trials:
for timer in timers:
key = (timer, trial)
new_timer = wx.Timer(self)
cum_timer += dm1[screens[timer]]
new_timer.Start(cum_timer * 1000, False)
new_timer.mykey = key
self.timers[key] = new_timer
self.Bind(wx.EVT_TIMER, self.Screens)
def Screens(self, event):
if event.GetEventObject() == self.timers[event.GetEventObject().mykey]:
print event.GetEventObject().mykey
if event.GetEventObject().mykey[0] == 0:
print 'You bastard!'
if event.GetEventObject().mykey[0] == 1:
print 'you vicious...'
if event.GetEventObject().mykey[0] == 2:
print 'heartless bastard!'
if event.GetEventObject().mykey[0] == 3:
print 'Oh It makes me mad!'
app = wx.App()
frame = DM1(None, title = 'IG', size = (wx.DisplaySize()))
frame.Show()
app.MainLoop()
指定した時間にタイマーが開始されません。2 番目のループがtrial最初のループを上書きするように見えます。たとえばprint event.GetEventObject().mykey、完全なコードのステートメントは、次のように出力されます。
(0, 1) (1, 1) (1, 2) (2, 1) (3, 1) (0, 2) (3, 1) (2, 1)
それ以外の
(0, 1) (1, 1) (2, 1) (3, 1) (0, 2) (1, 2) (2, 2) (3, 2)
問題は にあると思いGetEventObjectますが、タイマーをイベントにバインドするより良い方法がわかりません。誰かがアイデアを持っていますか?
どうもありがとう!