Advertisement
famansour

Tracker -- % symbol added

Sep 16th, 2020
1,156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.13 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. import sys
  8.  
  9. class StopWatch(tk.Frame):  #Class StopWatch inheriting from the Tkinter class Frame
  10.     """ Implements a stop watch frame widget. """                                                                
  11.     def __init__(self, parent=None, **kw):        
  12.         tk.Frame.__init__(self, parent, kw)
  13.         # self.configure(bg='black')
  14.         self._start = 0.0        
  15.         self._elapsedtime = 0.0
  16.         self.timestr = tk.StringVar()              
  17.         self._running = 0
  18.        
  19.         self._Label1 = tk.Label(self, textvariable=self.timestr)
  20.         self._setTime(self._elapsedtime)
  21.         self._Label1.pack(fill=tk.X, expand=tk.NO, pady=2, padx=2)
  22.  
  23.  
  24.    
  25.     # def makeWidgets(self):                        
  26.         # """ Make the time label. """
  27.         # l = tk.Label(self, textvariable=self.timestr)
  28.         # self._setTime(self._elapsedtime)
  29.         # l.pack(fill=tk.X, expand=tk.NO, pady=2, padx=2)
  30.         # l.configure(bg='white')        
  31.         # return l  
  32.        
  33.     def _update(self):
  34.         """ Update the label with elapsed time. """
  35.         self._elapsedtime = time.time() - self._start
  36.         self._setTime(self._elapsedtime)
  37.         self._timer = self.after(50, self._update)
  38.         # print (self._timer, '= self.after')
  39.     def _setTime(self, elap):
  40.         """ Set the time string to Minutes:Seconds:Hundreths """
  41.         minutes = int(elap/60)
  42.         seconds = int(elap - minutes*60.0)
  43.         hseconds = int((elap - minutes*60.0 - seconds)*100)                
  44.         self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, hseconds)) # convert date and time and datetime objects to its equivalent string
  45.         # print(('%02d:%02d:%02d' % (minutes, seconds, hseconds)))
  46.        
  47.     def Start(self):          
  48.         if not self._running:  
  49.             self._start = time.time() - self._elapsedtime
  50.             self._update()
  51.             self._running = 1
  52.     def Stop(self):
  53.         if self._running:
  54.             self.after_cancel(self._timer)            
  55.             self._elapsedtime = time.time() - self._start    
  56.             self._setTime(self._elapsedtime)
  57.             self._running = 0
  58.             # print(self._elapsedtime)
  59.             # print ('after_cancel(', self._timer, ')')
  60.  
  61.  
  62.     def Reset(self):                                  
  63.         """ Reset the stopwatch. """
  64.         self._start = time.time()        
  65.         self._elapsedtime = 0.0    
  66.         self._setTime(self._elapsedtime)
  67.    # if not StopWatch1Local._running:
  68. def mouse1Clicked():
  69.     global StopWatch1
  70.     global StopWatch2
  71.     if (StopWatch1._running and StopWatch2._running) :
  72.    
  73.         StopWatch1.Stop()
  74.         root.configure(bg='red')
  75.         StopWatch1._Label1.configure(bg='red')
  76.         StopWatch1.configure(bg='red')
  77.         StopWatch2._Label1.configure(bg='red')
  78.         StopWatch2.configure(bg='red')    
  79.     elif  (StopWatch1._running==0 and StopWatch2._running==0) :  
  80.    
  81.         StopWatch1.Start()
  82.         StopWatch2.Start()
  83.         root.configure(bg='green')
  84.         StopWatch1._Label1.configure(bg='green')
  85.         StopWatch1.configure(bg='green')
  86.         StopWatch2._Label1.configure(bg='green')
  87.         StopWatch2.configure(bg='green')
  88.     elif  (StopWatch1._running==0 and StopWatch2._running==1) :  
  89.         StopWatch1.Start()
  90.         StopWatch2.Start()
  91.         root.configure(bg='green')
  92.         StopWatch1._Label1.configure(bg='green')
  93.         StopWatch1.configure(bg='green')
  94.         StopWatch2._Label1.configure(bg='green')
  95.         StopWatch2.configure(bg='green')
  96. def mouse2Clicked():
  97.     global StopWatch1
  98.     global StopWatch2
  99.     if (StopWatch1._running==0 and StopWatch2._running==1) :  
  100.         StopWatch1.Stop()
  101.         StopWatch2.Stop()
  102.         root.configure(bg='white')
  103.         StopWatch1._Label1.configure(bg='white')
  104.         StopWatch1.configure(bg='white')
  105.         StopWatch2._Label1.configure(bg='white')
  106.         StopWatch2.configure(bg='white')
  107.     elif  (StopWatch1._running==1 and StopWatch2._running==1) :  
  108.         StopWatch1.Stop()
  109.         StopWatch2.Stop()  
  110.         root.configure(bg='white')
  111.         StopWatch1._Label1.configure(bg='white')
  112.         StopWatch1.configure(bg='white')
  113.         StopWatch2._Label1.configure(bg='white')
  114.         StopWatch2.configure(bg='white')
  115.     print("StopWatch1.timestr= ", StopWatch1._elapsedtime)    
  116.     print("StopWatch2.timestr= ", StopWatch2._elapsedtime)
  117.     print("Effeciency= ", round(StopWatch1._elapsedtime/StopWatch2._elapsedtime,2))
  118.     Effeciency.set([int(100*round(StopWatch1._elapsedtime/StopWatch2._elapsedtime,2)), '%']) # convert date and time and
  119.  
  120. root = tk.Tk() # initializing the tcl/tk interpreter and creating the root window
  121. # root.geometry("47x38+-2+0")
  122. # root.minsize(150, 100)
  123. StopWatch1 = StopWatch(root)
  124. StopWatch1.pack()# Tkinter Pack Geometry Manager   side=tk.TOP
  125. # ----------------------
  126. # root = tk.Tk() # initializing the tcl/tk interpreter and creating the root window
  127. StopWatch2 = StopWatch(root)
  128. StopWatch2.pack()# Tkinter Pack Geometry Manager
  129. # ----------------------
  130. # frame = tk.Frame(root, width=100, height=100)
  131. # ----------------------
  132. # ----------------------
  133. # ----------------------
  134.  
  135. def Reset():                                  
  136.     """ Reset the stopwatch. """
  137.     mouse2Clicked()
  138.     StopWatch1.Reset()
  139.     StopWatch2.Reset()  
  140. def Hide():    
  141.    
  142.     # root.configure(bg='black')
  143.     # StopWatch1._Label1.configure(bg='black')
  144.     # StopWatch1.configure(bg='black')
  145.     # StopWatch2._Label1.configure(bg='black')
  146.     # StopWatch2.configure(bg='black')
  147.     # root.wm_attributes("-transparent", True)
  148.    
  149.     # root.attributes("-transparentcolor")
  150.     # root.config(bg='systemTransparent')
  151.    
  152.  
  153.     # root.lift()
  154.     # root.wm_attributes("-topmost", True)
  155.     # root.wm_attributes("-disabled", True)
  156.     pass
  157. def minimiser():
  158.     root.overrideredirect(0)
  159.     root.state('iconic')  
  160.             # root.iconify()    
  161.             # root.geometry("47x38+-2+0")
  162.             # root.overrideredirect(True)
  163. root.bind("<Button-1>", lambda x=None: mouse1Clicked() )
  164. root.bind("<Button-3>", lambda x=None: mouse2Clicked()) #bind Python functions and methods to events
  165. root.bind("<Control-Button-1>", lambda x=None: Hide())
  166. root.bind("<Control-Button-2>", lambda x=None: sys.exit())
  167. root.bind("<Control-Button-3>", lambda x=None: Reset())
  168.  
  169.  
  170.  
  171.  
  172. # ----------------------
  173. # ----------------------
  174. # ----------------------
  175. class WindowDraggable():
  176.  
  177.     def __init__(self, root):
  178.         self.root = root
  179.         root.bind('<ButtonPress-2>', self.StartMove)
  180.         root.bind('<ButtonRelease-2>', self.StopMove)
  181.         root.bind('<B2-Motion>', self.OnMotion)
  182.  
  183.     def StartMove(self, event):
  184.         self.x = event.x
  185.         self.y = event.y
  186.  
  187.     def StopMove(self, event):
  188.         self.x = None
  189.         self.y = None
  190.  
  191.     def OnMotion(self,event):
  192.         x = (event.x_root - self.x - self.root.winfo_rootx() + self.root.winfo_rootx())
  193.         y = (event.y_root - self.y - self.root.winfo_rooty() + self.root.winfo_rooty())
  194.         root.geometry("+%s+%s" % (x, y))
  195.        
  196. # if StopWatch2._elapsedtime!=0 :      
  197.     # root.Label1 = tk.Label(root, textvariable=round(StopWatch1._elapsedtime/StopWatch2._elapsedtime,2))
  198.     # root.Label1.pack(fill=tk.X, expand=tk.NO, pady=2, padx=2)
  199.  
  200. Effeciency = tk.StringVar()  
  201. LabelEff = tk.Label(root, textvariable=Effeciency)
  202. LabelEff.pack()
  203. # time.sleep(1)
  204. # Effeciency.set(StopWatch2._elapsedtime-StopWatch1._elapsedtime) # convert date and time and
  205.  
  206. # labelText = tk.StringVar()
  207. # depositLabel = tk.Label(root, textvariable=labelText)
  208.  
  209. # def updateDepositLabel(txt): # you may have to use *args in some cases
  210.     # global labelText
  211.     # labelText.set(txt)
  212.    
  213. # updateDepositLabel(5)
  214. # labelText.pack()
  215.  
  216. WindowDraggable(root)
  217. root.overrideredirect(True) # turns off title bar, geometry
  218. root.attributes( '-topmost', 1 ) # ADDITINAL topmost
  219. root.mainloop() #This method will loop forever, waiting for events from the user, until it exits the program
  220.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement