Skip to content

Instantly share code, notes, and snippets.

@MechCoder
Created June 29, 2013 13:56
Show Gist options
  • Save MechCoder/5891187 to your computer and use it in GitHub Desktop.
Save MechCoder/5891187 to your computer and use it in GitHub Desktop.
Stopwatch using PyGtk
import time
import gtk
import threading
import gobject
gtk.gdk.threads_init()
class main():
def __init__(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title('Stopwatch')
self.window.set_size_request(200,200)
self.table = gtk.Table(10,20,True)
self.entry = gtk.Entry()
self.entry.set_editable(False)
self.table.attach(self.entry , 0 , 20 , 2 , 4)
self.button = gtk.ToggleButton(label = 'Start')
self.button.connect('toggled' , self.count)
self.table.attach(self.button , 6 , 14 , 6 ,10)
self.image = gtk.Image()
self.image.set_from_file('stop.jpg')
self.table.attach(self.image , 0 ,20, 0 ,10)
self.window.add(self.table)
self.window.connect('delete-event' , gtk.main_quit)
self.window.show_all()
def count(self,widget):
if widget.get_active():
self.s = time.time()
threading.Thread(target = self.ent_change).start()
widget.set_label('Stop')
else:
widget.set_label('Start')
def ent_change(self):
i = 0
while 1:
time.sleep(0.01)
gobject.idle_add(self.change)
i = i + 1
if self.button.get_active() == 0:
break
def change(self):
show = time.time() - self.s
hour = show / 3600
minutes = (show % 3600) / 60
seconds = show - (int(hour) * 3600) - (int(minutes) * 60)
string = ' %s : %s : %0.2f ' % (str(int(hour)),str(int(minutes)),seconds)
self.entry.set_text(string)
if __name__ == '__main__':
main()
gtk.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment