Attachment 'metronome2.py'
Download 1 '''metronome2
2 Now try to get a visual component flashing with each beat
3 '''
4 import wx
5
6 mspm = 60*1000 # milliseconds per minute
7
8 class MFrame(wx.Frame):
9 def __init__(self,):
10 wx.Frame.__init__(self,None)
11 #define the components
12 self.go = wx.Button(self,wx.ID_ANY,"Start")
13 self.stop = wx.Button(self,wx.ID_ANY,"Stop")
14 self.bpm = wx.SpinCtrl(self,wx.ID_ANY,value="120",min=1,max=240)
15 self.dingcheck = wx.CheckBox(self,wx.ID_ANY)
16 self.downbeat = wx.SpinCtrl(self,wx.ID_ANY,value="4",min = 2, max=12)
17 self.flashcheck = wx.CheckBox(self,wx.ID_ANY,"Flash")
18 self.flash = wx.Panel(self,wx.ID_ANY,size=(150,150))
19
20 self.dingcheck.SetValue(False)
21 self.downbeat.Enable(False)
22 self.flashing = True
23 self.flashcheck.SetValue(True)
24 self.SetFlash()
25
26 #do the layout
27 sizer = wx.BoxSizer(wx.VERTICAL)
28 sizer.Add(wx.StaticText(self,wx.ID_ANY,"Set the beats per minute"))
29 sizer.Add(self.bpm)
30 sizer.Add(self.go)
31 sizer.Add(self.stop)
32 s2 = wx.BoxSizer(wx.HORIZONTAL)
33 s2.Add(self.dingcheck,0,wx.ALIGN_CENTRE_VERTICAL)
34 s2.Add(wx.StaticText(self,wx.ID_ANY,"Beats per measure"),0,wx.ALIGN_CENTRE_VERTICAL)
35 s2.Add(self.downbeat)
36 sizer.Add(s2)
37 sizer.Add(self.flashcheck,0,wx.ALIGN_CENTRE)
38 sizer.Add(self.flash,1,wx.ALL|wx.EXPAND|wx.ALIGN_CENTRE,10)
39 self.SetSizerAndFit(sizer)
40 self.Layout()
41
42 #Bind events
43 self.Bind(wx.EVT_BUTTON,self.Start,self.go)
44 self.Bind(wx.EVT_BUTTON,self.Stop,self.stop)
45 self.Bind(wx.EVT_TIMER,self.Tick)
46 self.Bind(wx.EVT_CHECKBOX,self.SetDownBeat,self.dingcheck)
47
48 def SetFlash(self):
49 if self.flashcheck.IsChecked():
50 if self.flashing:
51 self.flash.SetBackgroundColour('White')
52 else:
53 self.flash.SetBackgroundColour('Black')
54 self.flash.Refresh()
55 self.flashing = not self.flashing
56
57 def SetDownBeat(self,evt):
58 if self.dingcheck.IsChecked():
59 self.downbeat.Enable(True)
60 else:
61 self.downbeat.Enable(False)
62
63 def Start(self,evt):
64 self.timer=wx.Timer(self)
65 self.count = 0
66 self.timer.Start(mspm/float(self.bpm.GetValue()))
67 self.go.Enable(False)
68 self.stop.Enable(True)
69
70 def Stop(self,evt):
71 self.timer.Stop()
72 del self.timer
73 self.go.Enable(True)
74 self.stop.Enable(False)
75
76 def Tick(self,evt):
77 if bool(self.count):
78 sound = wx.Sound('tick2.wav')
79 else:
80 if self.dingcheck.IsChecked():
81 sound = wx.Sound('ding.wav')
82 else:
83 sound = wx.Sound('tick2.wav')
84 sound.Play(wx.SOUND_ASYNC)
85 self.SetFlash()
86 self.count = (self.count + 1) % int(self.downbeat.GetValue())
87 wx.YieldIfNeeded()
88
89 class TestApp(wx.App):
90 def OnInit(self):
91 mainframe = MFrame()
92 self.SetTopWindow(mainframe)
93 mainframe.Show()
94 return 1
95
96
97 app = TestApp(0)
98 app.MainLoop()
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.