famansour

Tracker -- 2 counters- Verouillage works well

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