famansour

Tracker -- +Background colors + Windows geometry

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