famansour

Tracker -- Last version full of bugs -- Efficiency doesn't work in real time

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