Advertisement
famansour

Tracker -- Background colors are way enhanced

Sep 15th, 2020
1,195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.40 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.configure(bg='black')
  13.         self._start = 0.0        
  14.         self._elapsedtime = 0.0
  15.         self.timestr = tk.StringVar()              
  16.        
  17.         # self.makeWidgets()
  18.         self._running = 0
  19.        
  20.         self._Label1 = tk.Label(self, textvariable=self.timestr)
  21.         self._setTime(self._elapsedtime)
  22.         self._Label1.pack(fill=tk.X, expand=tk.NO, pady=2, padx=2)
  23.  
  24.  
  25.    
  26.     # def makeWidgets(self):                        
  27.         # """ Make the time label. """
  28.         # l = tk.Label(self, textvariable=self.timestr)
  29.         # self._setTime(self._elapsedtime)
  30.         # l.pack(fill=tk.X, expand=tk.NO, pady=2, padx=2)
  31.         # l.configure(bg='grey')        
  32.         # return l  
  33.        
  34.     def _update(self):
  35.         """ Update the label with elapsed time. """
  36.         self._elapsedtime = time.time() - self._start
  37.         self._setTime(self._elapsedtime)
  38.         self._timer = self.after(50, self._update)
  39.         # print (self._timer, '= self.after')
  40.     def _setTime(self, elap):
  41.         """ Set the time string to Minutes:Seconds:Hundreths """
  42.         minutes = int(elap/60)
  43.         seconds = int(elap - minutes*60.0)
  44.         hseconds = int((elap - minutes*60.0 - seconds)*100)                
  45.         self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, hseconds)) # convert date and time and datetime objects to its equivalent string
  46.         # print(('%02d:%02d:%02d' % (minutes, seconds, hseconds)))
  47.        
  48.     def Start(self):          
  49.         if not self._running:  
  50.             self._start = time.time() - self._elapsedtime
  51.             self._update()
  52.             self._running = 1
  53.     def Stop(self):
  54.         if self._running:
  55.             self.after_cancel(self._timer)            
  56.             self._elapsedtime = time.time() - self._start    
  57.             self._setTime(self._elapsedtime)
  58.             self._running = 0
  59.             # print(self._elapsedtime)
  60.             # print ('after_cancel(', self._timer, ')')
  61.  
  62.  
  63.     # def Reset(self):                                  
  64.         # """ Reset the stopwatch. """
  65.         # self._start = time.time()        
  66.         # self._elapsedtime = 0.0    
  67.         # self._setTime(self._elapsedtime)
  68.    # if not StopWatch1Local._running:
  69. def mouse1Clicked():
  70.     global StopWatch1
  71.     global StopWatch2
  72.     if (StopWatch1._running and StopWatch2._running) :
  73.    
  74.         StopWatch1.Stop()
  75.         root.configure(bg='red')
  76.         StopWatch1._Label1.configure(bg='red')
  77.         StopWatch1.configure(bg='red')
  78.         StopWatch2._Label1.configure(bg='red')
  79.         StopWatch2.configure(bg='red')    
  80.     elif  (StopWatch1._running==0 and StopWatch2._running==0) :  
  81.    
  82.         StopWatch1.Start()
  83.         StopWatch2.Start()
  84.         root.configure(bg='green')
  85.         StopWatch1._Label1.configure(bg='green')
  86.         StopWatch1.configure(bg='green')
  87.         StopWatch2._Label1.configure(bg='green')
  88.         StopWatch2.configure(bg='green')
  89.     elif  (StopWatch1._running==0 and StopWatch2._running==1) :  
  90.         StopWatch1.Start()
  91.         StopWatch2.Start()
  92.         root.configure(bg='green')
  93.         StopWatch1._Label1.configure(bg='green')
  94.         StopWatch1.configure(bg='green')
  95.         StopWatch2._Label1.configure(bg='green')
  96.         StopWatch2.configure(bg='green')
  97. def mouse2Clicked():
  98.     global StopWatch1
  99.     global StopWatch2
  100.     if (StopWatch1._running==0 and StopWatch2._running==1) :  
  101.         StopWatch1.Stop()
  102.         StopWatch2.Stop()
  103.         root.configure(bg='grey')
  104.         StopWatch1._Label1.configure(bg='grey')
  105.         StopWatch1.configure(bg='grey')
  106.         StopWatch2._Label1.configure(bg='grey')
  107.         StopWatch2.configure(bg='grey')
  108.     elif  (StopWatch1._running==1 and StopWatch2._running==1) :  
  109.         StopWatch1.Stop()
  110.         StopWatch2.Stop()  
  111.         root.configure(bg='grey')
  112.         StopWatch1._Label1.configure(bg='grey')
  113.         StopWatch1.configure(bg='grey')
  114.         StopWatch2._Label1.configure(bg='grey')
  115.         StopWatch2.configure(bg='grey')
  116.        
  117.        
  118. root = tk.Tk() # initializing the tcl/tk interpreter and creating the root window
  119. root.geometry("250x100")
  120. StopWatch1 = StopWatch(root)
  121. StopWatch1.pack()# Tkinter Pack Geometry Manager   side=tk.TOP
  122. print (StopWatch1._running)
  123. # ----------------------
  124. # root = tk.Tk() # initializing the tcl/tk interpreter and creating the root window
  125. StopWatch2 = StopWatch(root)
  126. StopWatch2.pack()# Tkinter Pack Geometry Manager
  127. print (StopWatch2._running)
  128. # ----------------------
  129. frame = tk.Frame(root, width=100, height=100)
  130.  
  131. frame.bind("<Button-1>", lambda x=None: mouse1Clicked() )
  132. #bind Python functions and methods to events
  133.  
  134. # frame.bind("<Button-1>", lambda x=None: [mouse1Clicked(iLoop,StopWatch1), mouse2Clicked(jLoop,StopWatch2)])
  135. frame.pack() # Tkinter Pack Geometry Manager
  136. # ----------------------
  137. frame.bind("<Button-3>", lambda x=None: mouse2Clicked()) #bind Python functions and methods to events
  138. frame.pack() # Tkinter Pack Geometry Manager
  139.  
  140. # ----------------------
  141. # ----------------------
  142. # ----------------------
  143. # ----------------------
  144. # Program work only inside tkinter window ends here
  145. # ----------------------
  146. # ----------------------
  147. # ----------------------
  148. # ----------------------
  149.  
  150.  
  151. CurrentKeyState = win32api.GetKeyState(0x04)  # "0x04" is the Virtual-Key Code of the Middle mouse button (VK_MBUTTON)
  152. def firstFunction():
  153.     while True:
  154.         NewKeyState = win32api.GetKeyState(0x04)
  155.         global CurrentKeyState
  156.         if NewKeyState != CurrentKeyState:  # Button state changed
  157.  
  158.             if NewKeyState < 0:  # KeyState = -127 or -128
  159.                 # print('BUTTON PRESSED ---', 'NewKeyState = ', NewKeyState )
  160.                 pass
  161.             else:                # KeyState =1 or 0
  162.                 # print('BUTTON RELEASED ---', 'NewKeyState = ', NewKeyState)
  163.                 mouseClicked(iLoop,StopWatch1)
  164.             CurrentKeyState = NewKeyState
  165.            
  166.         time.sleep(0.001)
  167.    
  168. thread1  = Thread(target = firstFunction)
  169. # thread1.setDaemon(True)
  170. # thread1.start()  # This Ligne is to be uncommented to go back where I left
  171.  
  172. # root.after(1, firstFunction())
  173. class WindowDraggable():
  174.  
  175.     def __init__(self, root):
  176.         self.root = root
  177.         root.bind('<ButtonPress-2>', self.StartMove)
  178.         root.bind('<ButtonRelease-2>', self.StopMove)
  179.         root.bind('<B2-Motion>', self.OnMotion)
  180.  
  181.     def StartMove(self, event):
  182.         self.x = event.x
  183.         self.y = event.y
  184.  
  185.     def StopMove(self, event):
  186.         self.x = None
  187.         self.y = None
  188.  
  189.     def OnMotion(self,event):
  190.         x = (event.x_root - self.x - self.root.winfo_rootx() + self.root.winfo_rootx())
  191.         y = (event.y_root - self.y - self.root.winfo_rooty() + self.root.winfo_rooty())
  192.         root.geometry("+%s+%s" % (x, y))
  193.  
  194. WindowDraggable(root)
  195. root.attributes( '-topmost', 1 ) # ADDITINAL topmost
  196. root.mainloop() #This method will loop forever, waiting for events from the user, until it exits the program
  197.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement