Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- # from mttkinter import mtTkinter as tk
- import time
- # import classes
- import win32api
- from threading import Thread
- class StopWatch(tk.Frame): #Class StopWatch inheriting from the Tkinter class Frame
- """ Implements a stop watch frame widget. """
- def __init__(self, parent=None, **kw):
- tk.Frame.__init__(self, parent, kw)
- self._start = 0.0
- self._elapsedtime = 0.0
- self.timestr = tk.StringVar()
- self.makeWidgets()
- def makeWidgets(self):
- """ Make the time label. """
- l = tk.Label(self, textvariable=self.timestr)
- self._setTime(self._elapsedtime)
- l.pack(fill=tk.X, expand=tk.NO, pady=2, padx=2)
- def _update(self):
- """ Update the label with elapsed time. """
- self._elapsedtime = time.time() - self._start
- self._setTime(self._elapsedtime)
- self._timer = self.after(50, self._update)
- # print (self._timer, '= self.after')
- def _setTime(self, elap):
- """ Set the time string to Minutes:Seconds:Hundreths """
- minutes = int(elap/60)
- seconds = int(elap - minutes*60.0)
- hseconds = int((elap - minutes*60.0 - seconds)*100)
- self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, hseconds)) # convert date and time and datetime objects to its equivalent string
- # print(('%02d:%02d:%02d' % (minutes, seconds, hseconds)))
- def Start(self):
- self._start = time.time() - self._elapsedtime
- self._update()
- def Stop(self):
- self.after_cancel(self._timer)
- self._elapsedtime = time.time() - self._start
- self._setTime(self._elapsedtime)
- # print(self._elapsedtime)
- # print ('after_cancel(', self._timer, ')')
- # def Reset(self):
- # """ Reset the stopwatch. """
- # self._start = time.time()
- # self._elapsedtime = 0.0
- # self._setTime(self._elapsedtime)
- def mouse1Clicked(iLoopLocal,StopWatch1Local):
- if (iLoopLocal % 2)== 0:
- StopWatch1Local.Start()
- print(iLoopLocal)
- else:
- StopWatch1Local.Stop()
- print(iLoopLocal)
- iLoopIncrement()
- def iLoopIncrement():
- # nonlocal iLoop
- global iLoop
- iLoop+=1
- return iLoop
- def mouse2Clicked(jLoopLocal,StopWatch2Local):
- if (jLoopLocal % 2)== 0:
- StopWatch2Local.Start()
- print(jLoopLocal)
- else:
- StopWatch2Local.Stop()
- print(jLoopLocal)
- jLoopIncrement()
- def jLoopIncrement():
- # nonlocal jLoop
- global jLoop
- jLoop+=1
- return jLoop
- iLoop=0
- jLoop=0
- root = tk.Tk() # initializing the tcl/tk interpreter and creating the root window
- StopWatch1 = StopWatch(root)
- StopWatch1.pack()# Tkinter Pack Geometry Manager side=tk.TOP
- # ----------------------
- # root = tk.Tk() # initializing the tcl/tk interpreter and creating the root window
- StopWatch2 = StopWatch(root)
- StopWatch2.pack()# Tkinter Pack Geometry Manager
- # ----------------------
- frame = tk.Frame(root, width=100, height=100)
- frame.bind("<Button-1>", lambda x=None: mouse1Clicked(iLoop,StopWatch1) ) #bind Python functions and methods to events
- frame.pack() # Tkinter Pack Geometry Manager
- # ----------------------
- frame.bind("<Button-2>", lambda x=None: mouse2Clicked(jLoop,StopWatch2)) #bind Python functions and methods to events
- frame.pack() # Tkinter Pack Geometry Manager
- # ----------------------
- # ----------------------
- # ----------------------
- # ----------------------
- # Program work only inside tkinter window ends here
- # ----------------------
- # ----------------------
- # ----------------------
- # ----------------------
- CurrentKeyState = win32api.GetKeyState(0x04) # "0x04" is the Virtual-Key Code of the Middle mouse button (VK_MBUTTON)
- def firstFunction():
- while True:
- NewKeyState = win32api.GetKeyState(0x04)
- global CurrentKeyState
- if NewKeyState != CurrentKeyState: # Button state changed
- if NewKeyState < 0: # KeyState = -127 or -128
- # print('BUTTON PRESSED ---', 'NewKeyState = ', NewKeyState )
- pass
- else: # KeyState =1 or 0
- # print('BUTTON RELEASED ---', 'NewKeyState = ', NewKeyState)
- mouseClicked(iLoop,StopWatch1)
- CurrentKeyState = NewKeyState
- time.sleep(0.001)
- thread1 = Thread(target = firstFunction)
- # thread1.setDaemon(True)
- # thread1.start() # This Ligne is to be uncommented to go back where I left
- # root.after(1, firstFunction())
- root.mainloop() #This method will loop forever, waiting for events from the user, until it exits the program
Add Comment
Please, Sign In to add comment