#!/usr/bin/python
import pygtk
pygtk.require('2.0')
import gtk
import time
class MarkupButton(gtk.Button):
def __init__(self, markup):
gtk.Button.__init__(self)
l = gtk.Label()
l.set_markup_with_mnemonic(markup)
self.add(l)
class BPMCounter:
def __init__(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_border_width(8)
self.window.set_resizable(False)
self.window.connect("destroy", self.destroy)
vbox = gtk.VBox(False, 8)
self.window.add(vbox)
self.label = gtk.Label()
self.label.set_alignment(0, 0)
vbox.pack_start(self.label, True, True, 0)
self.btncount = MarkupButton("_Count")
vbox.pack_start(self.btncount, True, True, 0)
self.btncount.connect("clicked", self.callback_count)
hbox = gtk.HBox(False, 8)
vbox.pack_start(hbox, False, False, 0)
self.btnreset = gtk.Button("_Reset")
self.btnreset.set_property("can-focus", False)
hbox.pack_start(self.btnreset, True, True, 0)
self.btnreset.connect("clicked", self.callback_reset)
self.btnquit = gtk.Button("_Quit")
self.btnquit.set_property("can-focus", False)
hbox.pack_start(self.btnquit, False, False, 0)
self.btnquit.connect("clicked", self.destroy)
self.start = 0.0
self.beats = 0
self.update()
self.window.show_all()
def callback_count(self, widget):
self.beats += 1
if self.start == 0.0:
self.start = time.time()
self.update()
def callback_reset(self, widget):
self.start = 0.0
self.beats = 0
self.update()
def update(self):
bpm = 0
current = time.time()
if self.beats > 1:
bpm = ((self.beats-1) * 60) / (current - self.start)
self.label.set_markup("BPM: %d\nBeats: %d" %
(bpm, self.beats))
def destroy(self, widget, data=None):
gtk.main_quit()
def main(self):
gtk.main()
if __name__ == "__main__":
BPMCounter().main()