Advertisement
famansour

Tracker -- Version 2 is complete

Sep 16th, 2020
1,017
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.38 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.         minutes = int(elap/60)
  101.         seconds = int(elap - minutes*60.0)
  102.         hseconds = int((elap - minutes*60.0 - seconds)*100)                
  103.         self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, hseconds)) # convert date and time and datetime objects to its equivalent string
  104.         # print(('%02d:%02d:%02d' % (minutes, seconds, hseconds)))
  105.        
  106.     def Start(self):          
  107.         if not self._running:  
  108.             self._start = time.time() - self._elapsedtime
  109.             self._update()
  110.             self._running = 1
  111.     def Stop(self):
  112.         if self._running:
  113.             self.after_cancel(self._timer)            
  114.             self._elapsedtime = time.time() - self._start    
  115.             self._setTime(self._elapsedtime)
  116.             self._running = 0
  117.             # print(self._elapsedtime)
  118.             # print ('after_cancel(', self._timer, ')')
  119.  
  120.  
  121.     def Reset(self):                                  
  122.         """ Reset the stopwatch. """
  123.         self._start = time.time()        
  124.         self._elapsedtime = 0.0    
  125.         self._setTime(self._elapsedtime)
  126.    # if not StopWatch1Local._running:
  127. def mouse1Clicked():
  128.     global StopWatch1
  129.     global StopWatch2
  130.     StopWatch1._Label1.pack_forget()
  131.     StopWatch2._Label1.pack_forget()
  132.     LabelEff.pack_forget()
  133.     if (StopWatch1._running and StopWatch2._running) :
  134.    
  135.         StopWatch1.Stop()
  136.         root.configure(bg='red')
  137.         StopWatch1._Label1.configure(bg='red')
  138.         StopWatch1.configure(bg='red')
  139.         StopWatch2._Label1.configure(bg='red')
  140.         StopWatch2.configure(bg='red')    
  141.     elif  (StopWatch1._running==0 and StopWatch2._running==0) :  
  142.    
  143.         StopWatch1.Start()
  144.         StopWatch2.Start()
  145.         root.configure(bg='green')
  146.         StopWatch1._Label1.configure(bg='green')
  147.         StopWatch1.configure(bg='green')
  148.         StopWatch2._Label1.configure(bg='green')
  149.         StopWatch2.configure(bg='green')
  150.     elif  (StopWatch1._running==0 and StopWatch2._running==1) :  
  151.         StopWatch1.Start()
  152.         StopWatch2.Start()
  153.         root.configure(bg='green')
  154.         StopWatch1._Label1.configure(bg='green')
  155.         StopWatch1.configure(bg='green')
  156.         StopWatch2._Label1.configure(bg='green')
  157.         StopWatch2.configure(bg='green')
  158. def mouse2Clicked():
  159.     global StopWatch1
  160.     global StopWatch2
  161.  
  162.    
  163.     StopWatch1._Label1.pack()
  164.     # StopWatch2._Label1.pack()
  165.     LabelEff.pack()
  166.     if (StopWatch1._running==0 and StopWatch2._running==1) :  
  167.         StopWatch1.Stop()
  168.         StopWatch2.Stop()
  169.         root.configure(bg='white')
  170.         StopWatch1._Label1.configure(bg='white')
  171.         StopWatch1.configure(bg='white')
  172.         StopWatch2._Label1.configure(bg='white')
  173.         StopWatch2.configure(bg='white')
  174.     elif  (StopWatch1._running==1 and StopWatch2._running==1) :  
  175.         StopWatch1.Stop()
  176.         StopWatch2.Stop()  
  177.         root.configure(bg='white')
  178.         StopWatch1._Label1.configure(bg='white')
  179.         StopWatch1.configure(bg='white')
  180.         StopWatch2._Label1.configure(bg='white')
  181.         StopWatch2.configure(bg='white')
  182.     elif  (StopWatch1._running==0 and StopWatch2._running==0) :
  183.         # stopWatch1._Label1.pack()
  184.         # StopWatch2._Label1.pack()
  185.         # LabelEff.pack()
  186.         # app = Fullscreen_Example()
  187.         # StopWatch1._Label1.pack()
  188.         # StopWatch2._Label1.pack()
  189.         # root.attributes("-fullscreen", self.fullScreenState)
  190.         # StopWatch1._Label1.pack_forget()
  191.         # # StopWatch2._Label1.pack_forget()
  192.  
  193.         StopWatch1._Label1.pack_forget()
  194.         # StopWatch2._Label1.pack_forget()
  195.         LabelEff.pack_forget()
  196.     print("StopWatch1.timestr= ", StopWatch1._elapsedtime)    
  197.     print("StopWatch2.timestr= ", StopWatch2._elapsedtime)
  198.     print("Effeciency= ", round(StopWatch1._elapsedtime/StopWatch2._elapsedtime,2))
  199.     Effeciency.set([int(100*round(StopWatch1._elapsedtime/StopWatch2._elapsedtime,2)), '%']) # convert date and time and
  200.  
  201. root = tk.Tk() # initializing the tcl/tk interpreter and creating the root window
  202. root.geometry("52x38+-2+0")
  203. # root.geometry("47x38+-2+0")
  204. # root.geometry("100x100+-2+0")
  205. # root.minsize(150, 100)
  206. StopWatch1 = StopWatch(root)
  207. StopWatch1.pack()# Tkinter Pack Geometry Manager   side=tk.TOP
  208. # ----------------------
  209. # root = tk.Tk() # initializing the tcl/tk interpreter and creating the root window
  210. StopWatch2 = StopWatch(root)
  211. # StopWatch2.pack()# Tkinter Pack Geometry Manager
  212. # ----------------------
  213. # frame = tk.Frame(root, width=100, height=100)
  214. # ----------------------
  215. # ----------------------
  216. # ----------------------
  217.  
  218. def Reset():                                  
  219.     """ Reset the stopwatch. """
  220.     mouse2Clicked()
  221.     StopWatch1.Reset()
  222.     StopWatch2.Reset()  
  223. def Hide():    
  224.    
  225.     # root.configure(bg='black')
  226.     # StopWatch1._Label1.configure(bg='black')
  227.     # StopWatch1.configure(bg='black')
  228.     # StopWatch2._Label1.configure(bg='black')
  229.     # StopWatch2.configure(bg='black')
  230.     # root.wm_attributes("-transparent", True)
  231.    
  232.     # root.attributes("-transparentcolor")
  233.     # root.config(bg='systemTransparent')
  234.    
  235.  
  236.     # root.lift()
  237.     # root.wm_attributes("-topmost", True)
  238.     # root.wm_attributes("-disabled", True)
  239.     pass
  240. def minimiser():
  241.     root.overrideredirect(0)
  242.     root.state('iconic')  
  243.             # root.iconify()    
  244.             # root.geometry("47x38+-2+0")
  245.             # root.overrideredirect(True)
  246. StopWatch1._Label1.pack_forget()
  247. root.bind("<Button-1>", lambda x=None: mouse1Clicked() )
  248. root.bind("<Button-3>", lambda x=None: mouse2Clicked()) #bind Python functions and methods to events
  249. root.bind("<Control-Button-1>", lambda x=None: Hide())
  250. root.bind("<Control-Button-2>", lambda x=None: sys.exit())
  251. root.bind("<Control-Button-3>", lambda x=None: Reset())
  252.  
  253.  
  254.  
  255.  
  256. # ----------------------
  257. # ----------------------
  258. # ----------------------
  259.  
  260. # if StopWatch2._elapsedtime!=0 :      
  261.     # root.Label1 = tk.Label(root, textvariable=round(StopWatch1._elapsedtime/StopWatch2._elapsedtime,2))
  262.     # root.Label1.pack(fill=tk.X, expand=tk.NO, pady=2, padx=2)
  263.  
  264. Effeciency = tk.StringVar()  
  265. LabelEff = tk.Label(root, textvariable=Effeciency)
  266. LabelEff.pack()
  267. # time.sleep(1)
  268. # Effeciency.set(StopWatch2._elapsedtime-StopWatch1._elapsedtime) # convert date and time and
  269.  
  270. # labelText = tk.StringVar()
  271. # depositLabel = tk.Label(root, textvariable=labelText)
  272.  
  273. # def updateDepositLabel(txt): # you may have to use *args in some cases
  274.     # global labelText
  275.     # labelText.set(txt)
  276.    
  277. # updateDepositLabel(5)
  278. # labelText.pack()
  279.  
  280. WindowDraggable(root)
  281. root.overrideredirect(True) # turns off title bar, geometry
  282. root.attributes( '-topmost', 1 ) # ADDITINAL topmost
  283.  
  284.  
  285.  
  286.  
  287. root.mainloop() #This method will loop forever, waiting for events from the user, until it exits the program
  288.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement