Advertisement
famansour

Tracker -- added pack() and pack_forget()

Sep 16th, 2020
1,464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.32 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.     StopWatch1._Label1.pack_forget()
  72.     StopWatch2._Label1.pack_forget()
  73.     LabelEff.pack_forget()
  74.     if (StopWatch1._running and StopWatch2._running) :
  75.    
  76.         StopWatch1.Stop()
  77.         root.configure(bg='red')
  78.         StopWatch1._Label1.configure(bg='red')
  79.         StopWatch1.configure(bg='red')
  80.         StopWatch2._Label1.configure(bg='red')
  81.         StopWatch2.configure(bg='red')    
  82.     elif  (StopWatch1._running==0 and StopWatch2._running==0) :  
  83.    
  84.         StopWatch1.Start()
  85.         StopWatch2.Start()
  86.         root.configure(bg='green')
  87.         StopWatch1._Label1.configure(bg='green')
  88.         StopWatch1.configure(bg='green')
  89.         StopWatch2._Label1.configure(bg='green')
  90.         StopWatch2.configure(bg='green')
  91.     elif  (StopWatch1._running==0 and StopWatch2._running==1) :  
  92.         StopWatch1.Start()
  93.         StopWatch2.Start()
  94.         root.configure(bg='green')
  95.         StopWatch1._Label1.configure(bg='green')
  96.         StopWatch1.configure(bg='green')
  97.         StopWatch2._Label1.configure(bg='green')
  98.         StopWatch2.configure(bg='green')
  99. def mouse2Clicked():
  100.     global StopWatch1
  101.     global StopWatch2
  102.  
  103.    
  104.     StopWatch1._Label1.pack()
  105.     StopWatch2._Label1.pack()
  106.     LabelEff.pack()
  107.     if (StopWatch1._running==0 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.     elif  (StopWatch1._running==1 and StopWatch2._running==1) :  
  116.         StopWatch1.Stop()
  117.         StopWatch2.Stop()  
  118.         root.configure(bg='white')
  119.         StopWatch1._Label1.configure(bg='white')
  120.         StopWatch1.configure(bg='white')
  121.         StopWatch2._Label1.configure(bg='white')
  122.         StopWatch2.configure(bg='white')
  123.     print("StopWatch1.timestr= ", StopWatch1._elapsedtime)    
  124.     print("StopWatch2.timestr= ", StopWatch2._elapsedtime)
  125.     print("Effeciency= ", round(StopWatch1._elapsedtime/StopWatch2._elapsedtime,2))
  126.     Effeciency.set([int(100*round(StopWatch1._elapsedtime/StopWatch2._elapsedtime,2)), '%']) # convert date and time and
  127.  
  128. root = tk.Tk() # initializing the tcl/tk interpreter and creating the root window
  129. # root.geometry("47x38+-2+0")
  130. # root.minsize(150, 100)
  131. StopWatch1 = StopWatch(root)
  132. StopWatch1.pack()# Tkinter Pack Geometry Manager   side=tk.TOP
  133. # ----------------------
  134. # root = tk.Tk() # initializing the tcl/tk interpreter and creating the root window
  135. StopWatch2 = StopWatch(root)
  136. StopWatch2.pack()# Tkinter Pack Geometry Manager
  137. # ----------------------
  138. # frame = tk.Frame(root, width=100, height=100)
  139. # ----------------------
  140. # ----------------------
  141. # ----------------------
  142.  
  143. def Reset():                                  
  144.     """ Reset the stopwatch. """
  145.     mouse2Clicked()
  146.     StopWatch1.Reset()
  147.     StopWatch2.Reset()  
  148. def Hide():    
  149.    
  150.     # root.configure(bg='black')
  151.     # StopWatch1._Label1.configure(bg='black')
  152.     # StopWatch1.configure(bg='black')
  153.     # StopWatch2._Label1.configure(bg='black')
  154.     # StopWatch2.configure(bg='black')
  155.     # root.wm_attributes("-transparent", True)
  156.    
  157.     # root.attributes("-transparentcolor")
  158.     # root.config(bg='systemTransparent')
  159.    
  160.  
  161.     # root.lift()
  162.     # root.wm_attributes("-topmost", True)
  163.     # root.wm_attributes("-disabled", True)
  164.     pass
  165. def minimiser():
  166.     root.overrideredirect(0)
  167.     root.state('iconic')  
  168.             # root.iconify()    
  169.             # root.geometry("47x38+-2+0")
  170.             # root.overrideredirect(True)
  171. root.bind("<Button-1>", lambda x=None: mouse1Clicked() )
  172. root.bind("<Button-3>", lambda x=None: mouse2Clicked()) #bind Python functions and methods to events
  173. root.bind("<Control-Button-1>", lambda x=None: Hide())
  174. root.bind("<Control-Button-2>", lambda x=None: sys.exit())
  175. root.bind("<Control-Button-3>", lambda x=None: Reset())
  176.  
  177.  
  178.  
  179.  
  180. # ----------------------
  181. # ----------------------
  182. # ----------------------
  183. class WindowDraggable():
  184.  
  185.     def __init__(self, root):
  186.         self.root = root
  187.         root.bind('<ButtonPress-2>', self.StartMove)
  188.         root.bind('<ButtonRelease-2>', self.StopMove)
  189.         root.bind('<B2-Motion>', self.OnMotion)
  190.  
  191.     def StartMove(self, event):
  192.         self.x = event.x
  193.         self.y = event.y
  194.  
  195.     def StopMove(self, event):
  196.         self.x = None
  197.         self.y = None
  198.  
  199.     def OnMotion(self,event):
  200.         x = (event.x_root - self.x - self.root.winfo_rootx() + self.root.winfo_rootx())
  201.         y = (event.y_root - self.y - self.root.winfo_rooty() + self.root.winfo_rooty())
  202.         root.geometry("+%s+%s" % (x, y))
  203.        
  204. # if StopWatch2._elapsedtime!=0 :      
  205.     # root.Label1 = tk.Label(root, textvariable=round(StopWatch1._elapsedtime/StopWatch2._elapsedtime,2))
  206.     # root.Label1.pack(fill=tk.X, expand=tk.NO, pady=2, padx=2)
  207.  
  208. Effeciency = tk.StringVar()  
  209. LabelEff = tk.Label(root, textvariable=Effeciency)
  210. LabelEff.pack()
  211. # time.sleep(1)
  212. # Effeciency.set(StopWatch2._elapsedtime-StopWatch1._elapsedtime) # convert date and time and
  213.  
  214. # labelText = tk.StringVar()
  215. # depositLabel = tk.Label(root, textvariable=labelText)
  216.  
  217. # def updateDepositLabel(txt): # you may have to use *args in some cases
  218.     # global labelText
  219.     # labelText.set(txt)
  220.    
  221. # updateDepositLabel(5)
  222. # labelText.pack()
  223.  
  224. WindowDraggable(root)
  225. root.overrideredirect(True) # turns off title bar, geometry
  226. root.attributes( '-topmost', 1 ) # ADDITINAL topmost
  227. root.mainloop() #This method will loop forever, waiting for events from the user, until it exits the program
  228.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement