Question

I have a programm in Python with a simple GUI that simulates a queue management system. When i press the Button "Next Customer" it displays the next queue number. Now i want to count the time intervals between the 2 clicks (on the button "Next Customer") so to track the service time needed. How is this possible? The code is the following.

import time
import random
from Tkinter import *

def PrintNumber():
   global j, label
   j+=1
   label.config(text=str(j))
   print j
   t = (time.strftime("%H:%M:%S"))
   d = time.strftime("%d/%m/%Y")
   return

j=0
mgui=Tk()
mgui.geometry('200x200')
mgui.title('Queue System')
st = Button(mgui, text="Next Customer", command = PrintNumber)
st.pack()
label = Label(mgui, text=str(j))
label.pack()  
mgui.mainloop()      
Was it helpful?

Solution

This is my lazy solution:

import time
import random
from Tkinter import *

class GetClicktime():
    def __init__(self):
        self.j=0
        self.t=[]
        self.mgui=Tk()
        self.mgui.geometry('200x200')
        self.mgui.title('Queue System')
        self.st = Button(self.mgui, text="Next Customer", command = self.PrintNumber)
        self.st.pack()
        #self.st.bind('<Button-1>',callback)
        self.label = Label(self.mgui, text=str(self.j))
        self.label.pack()  
        self.mgui.mainloop()

    def PrintNumber(self):
       self.j+=1
       self.label.config(text=str(self.j))
       print self.j
       t = (time.strftime("%H:%M:%S"))
       d = time.strftime("%d/%m/%Y")
       self.t.append(int(t.replace(':','')))
       print self.t
       if self.j >2:
           print 'the time between clicks is:',self.t[self.j-1]-self.t[self.j-2],'seconds'
       print t,d
       return

if __name__ == "__main__":
    GetClicktime()

you can avoid writing a class, but this does what you need to.

If you need some docs on classes i recommmend: https://www.youtube.com/watch?v=trOZBgZ8F_c#start=0:00;end=13:27;cycles=-1;autoreplay=false;showoptions=false

OTHER TIPS

you could begin the timer after the first button pressed and end it after the second pressed. You could add a condition to determining weather it is the first press.

if start:
   elapsed = (time.clock() - start)
   print (elapsed)
start = time.clock()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top