Advertisement
famansour

Tracker -- 2 counters- Verouillage Bug corrected

Sep 15th, 2020
1,624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.29 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.    # if not StopWatch1Local._running:
  59. def mouse1Clicked():
  60.     global StopWatch1
  61.     global StopWatch2
  62.     if (StopWatch1._running and StopWatch2._running) :
  63.    
  64.         StopWatch1.Stop()
  65.     elif  (StopWatch1._running==0 and StopWatch2._running==0) :  
  66.    
  67.         StopWatch1.Start()
  68.         StopWatch2.Start()
  69.     elif  (StopWatch1._running==0 and StopWatch2._running==1) :  
  70.         StopWatch1.Start()
  71.         StopWatch2.Start()
  72.  
  73. def mouse2Clicked():
  74.     global StopWatch1
  75.     global StopWatch2
  76.     if (StopWatch1._running==0 and StopWatch2._running==1) :  
  77.         StopWatch1.Stop()
  78.         StopWatch2.Stop()
  79.     elif  (StopWatch1._running==1 and StopWatch2._running==1) :  
  80.         StopWatch1.Stop()
  81.         StopWatch2.Stop()      
  82.        
  83. root = tk.Tk() # initializing the tcl/tk interpreter and creating the root window
  84. StopWatch1 = StopWatch(root)
  85. StopWatch1.pack()# Tkinter Pack Geometry Manager   side=tk.TOP
  86. print (StopWatch1._running)
  87. # ----------------------
  88. # root = tk.Tk() # initializing the tcl/tk interpreter and creating the root window
  89. StopWatch2 = StopWatch(root)
  90. StopWatch2.pack()# Tkinter Pack Geometry Manager
  91. print (StopWatch2._running)
  92. # ----------------------
  93. frame = tk.Frame(root, width=100, height=100)
  94.  
  95. frame.bind("<Button-1>", lambda x=None: mouse1Clicked() )
  96. #bind Python functions and methods to events
  97.  
  98. # frame.bind("<Button-1>", lambda x=None: [mouse1Clicked(iLoop,StopWatch1), mouse2Clicked(jLoop,StopWatch2)])
  99. frame.pack() # Tkinter Pack Geometry Manager
  100. # ----------------------
  101. frame.bind("<Button-3>", lambda x=None: mouse2Clicked()) #bind Python functions and methods to events
  102. frame.pack() # Tkinter Pack Geometry Manager
  103.  
  104. # ----------------------
  105. # ----------------------
  106. # ----------------------
  107. # ----------------------
  108. # Program work only inside tkinter window ends here
  109. # ----------------------
  110. # ----------------------
  111. # ----------------------
  112. # ----------------------
  113.  
  114.  
  115. CurrentKeyState = win32api.GetKeyState(0x04)  # "0x04" is the Virtual-Key Code of the Middle mouse button (VK_MBUTTON)
  116. def firstFunction():
  117.     while True:
  118.         NewKeyState = win32api.GetKeyState(0x04)
  119.         global CurrentKeyState
  120.         if NewKeyState != CurrentKeyState:  # Button state changed
  121.  
  122.             if NewKeyState < 0:  # KeyState = -127 or -128
  123.                 # print('BUTTON PRESSED ---', 'NewKeyState = ', NewKeyState )
  124.                 pass
  125.             else:                # KeyState =1 or 0
  126.                 # print('BUTTON RELEASED ---', 'NewKeyState = ', NewKeyState)
  127.                 mouseClicked(iLoop,StopWatch1)
  128.             CurrentKeyState = NewKeyState
  129.            
  130.         time.sleep(0.001)
  131.    
  132. thread1  = Thread(target = firstFunction)
  133. # thread1.setDaemon(True)
  134. # thread1.start()  # This Ligne is to be uncommented to go back where I left
  135.  
  136. # root.after(1, firstFunction())
  137.  
  138. root.mainloop() #This method will loop forever, waiting for events from the user, until it exits the program
  139.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement