famansour

Tracker -- 2 counters- mouse clicks on tkinter Windows

Sep 15th, 2020
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.99 KB | None | 0 0
  1. import tkinter as tk
  2. # from mttkinter import mtTkinter as tk
  3. import time
  4. # import classes
  5. import win32api
  6. from threading import Thread
  7.  
  8. class StopWatch(tk.Frame):  #Class StopWatch inheriting from the Tkinter class Frame
  9.     """ Implements a stop watch frame widget. """                                                                
  10.     def __init__(self, parent=None, **kw):        
  11.         tk.Frame.__init__(self, parent, kw)
  12.         self._start = 0.0        
  13.         self._elapsedtime = 0.0
  14.         self.timestr = tk.StringVar()              
  15.         self.makeWidgets()      
  16.  
  17.     def makeWidgets(self):                        
  18.         """ Make the time label. """
  19.         l = tk.Label(self, textvariable=self.timestr)
  20.         self._setTime(self._elapsedtime)
  21.         l.pack(fill=tk.X, expand=tk.NO, pady=2, padx=2)                      
  22.    
  23.     def _update(self):
  24.         """ Update the label with elapsed time. """
  25.         self._elapsedtime = time.time() - self._start
  26.         self._setTime(self._elapsedtime)
  27.         self._timer = self.after(50, self._update)
  28.         # print (self._timer, '= self.after')
  29.     def _setTime(self, elap):
  30.         """ Set the time string to Minutes:Seconds:Hundreths """
  31.         minutes = int(elap/60)
  32.         seconds = int(elap - minutes*60.0)
  33.         hseconds = int((elap - minutes*60.0 - seconds)*100)                
  34.         self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, hseconds)) # convert date and time and datetime objects to its equivalent string
  35.         # print(('%02d:%02d:%02d' % (minutes, seconds, hseconds)))
  36.        
  37.     def Start(self):                                                              
  38.         self._start = time.time() - self._elapsedtime
  39.         self._update()
  40.     def Stop(self):                                    
  41.         self.after_cancel(self._timer)            
  42.         self._elapsedtime = time.time() - self._start    
  43.         self._setTime(self._elapsedtime)
  44.         # print(self._elapsedtime)
  45.         # print ('after_cancel(', self._timer, ')')
  46.  
  47.  
  48.     # def Reset(self):                                  
  49.         # """ Reset the stopwatch. """
  50.         # self._start = time.time()        
  51.         # self._elapsedtime = 0.0    
  52.         # self._setTime(self._elapsedtime)
  53.  
  54. def mouse1Clicked(iLoopLocal,StopWatch1Local):
  55.     if (iLoopLocal % 2)== 0:
  56.         StopWatch1Local.Start()
  57.         print(iLoopLocal)
  58.  
  59.     else:  
  60.         StopWatch1Local.Stop()
  61.         print(iLoopLocal)
  62.  
  63.     iLoopIncrement()
  64.  
  65. def iLoopIncrement():
  66.     # nonlocal iLoop
  67.     global iLoop
  68.     iLoop+=1
  69.     return iLoop
  70. def mouse2Clicked(jLoopLocal,StopWatch2Local):
  71.     if (jLoopLocal % 2)== 0:
  72.         StopWatch2Local.Start()
  73.         print(jLoopLocal)
  74.  
  75.     else:  
  76.         StopWatch2Local.Stop()
  77.         print(jLoopLocal)
  78.  
  79.     jLoopIncrement()
  80.  
  81. def jLoopIncrement():
  82.     # nonlocal jLoop
  83.     global jLoop
  84.     jLoop+=1
  85.     return jLoop    
  86. iLoop=0
  87. jLoop=0
  88. root = tk.Tk() # initializing the tcl/tk interpreter and creating the root window
  89. StopWatch1 = StopWatch(root)
  90. StopWatch1.pack()# Tkinter Pack Geometry Manager   side=tk.TOP
  91. # ----------------------
  92. # root = tk.Tk() # initializing the tcl/tk interpreter and creating the root window
  93. StopWatch2 = StopWatch(root)
  94. StopWatch2.pack()# Tkinter Pack Geometry Manager
  95. # ----------------------
  96. frame = tk.Frame(root, width=100, height=100)
  97. frame.bind("<Button-1>", lambda x=None: mouse1Clicked(iLoop,StopWatch1) ) #bind Python functions and methods to events
  98. frame.pack() # Tkinter Pack Geometry Manager
  99. # ----------------------
  100. frame.bind("<Button-2>", lambda x=None: mouse2Clicked(jLoop,StopWatch2)) #bind Python functions and methods to events
  101. frame.pack() # Tkinter Pack Geometry Manager
  102.  
  103. # ----------------------
  104. # ----------------------
  105. # ----------------------
  106. # ----------------------
  107. # Program work only inside tkinter window ends here
  108. # ----------------------
  109. # ----------------------
  110. # ----------------------
  111. # ----------------------
  112.  
  113.  
  114. CurrentKeyState = win32api.GetKeyState(0x04)  # "0x04" is the Virtual-Key Code of the Middle mouse button (VK_MBUTTON)
  115. def firstFunction():
  116.     while True:
  117.         NewKeyState = win32api.GetKeyState(0x04)
  118.         global CurrentKeyState
  119.         if NewKeyState != CurrentKeyState:  # Button state changed
  120.  
  121.             if NewKeyState < 0:  # KeyState = -127 or -128
  122.                 # print('BUTTON PRESSED ---', 'NewKeyState = ', NewKeyState )
  123.                 pass
  124.             else:                # KeyState =1 or 0
  125.                 # print('BUTTON RELEASED ---', 'NewKeyState = ', NewKeyState)
  126.                 mouseClicked(iLoop,StopWatch1)
  127.             CurrentKeyState = NewKeyState
  128.            
  129.         time.sleep(0.001)
  130.    
  131. thread1  = Thread(target = firstFunction)
  132. # thread1.setDaemon(True)
  133. # thread1.start()  # This Ligne is to be uncommented to go back where I left
  134.  
  135. # root.after(1, firstFunction())
  136.  
  137. root.mainloop() #This method will loop forever, waiting for events from the user, until it exits the program
  138.  
Add Comment
Please, Sign In to add comment