famansour

Tracker -- Minimize works but maximize don't

Sep 15th, 2020
809
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.        
  116.        
  117. root = tk.Tk() # initializing the tcl/tk interpreter and creating the root window
  118. root.geometry("47x38+-2+0")
  119. # root.minsize(150, 100)
  120. StopWatch1 = StopWatch(root)
  121. StopWatch1.pack()# Tkinter Pack Geometry Manager   side=tk.TOP
  122. # ----------------------
  123. # root = tk.Tk() # initializing the tcl/tk interpreter and creating the root window
  124. StopWatch2 = StopWatch(root)
  125. StopWatch2.pack()# Tkinter Pack Geometry Manager
  126. # ----------------------
  127. frame = tk.Frame(root, width=100, height=100)
  128. # ----------------------
  129. # ----------------------
  130. # ----------------------
  131.  
  132. def Reset():                                  
  133.     """ Reset the stopwatch. """
  134.     mouse2Clicked()
  135.     StopWatch1.Reset()
  136.     StopWatch2.Reset()  
  137. def minimiser():
  138.     root.overrideredirect(0)
  139.     root.state('iconic')  
  140.     root.iconify()    
  141. root.bind("<Button-1>", lambda x=None: mouse1Clicked() )
  142. root.bind("<Button-3>", lambda x=None: mouse2Clicked()) #bind Python functions and methods to events
  143. root.bind("<Control-Button-1>", lambda x=None: minimiser())
  144. root.bind("<Control-Button-2>", lambda x=None: sys.exit())
  145. root.bind("<Control-Button-3>", lambda x=None: Reset())
  146.  
  147.  
  148.  
  149.  
  150. # ----------------------
  151. # ----------------------
  152. # ----------------------
  153. class WindowDraggable():
  154.  
  155.     def __init__(self, root):
  156.         self.root = root
  157.         root.bind('<ButtonPress-2>', self.StartMove)
  158.         root.bind('<ButtonRelease-2>', self.StopMove)
  159.         root.bind('<B2-Motion>', self.OnMotion)
  160.  
  161.     def StartMove(self, event):
  162.         self.x = event.x
  163.         self.y = event.y
  164.  
  165.     def StopMove(self, event):
  166.         self.x = None
  167.         self.y = None
  168.  
  169.     def OnMotion(self,event):
  170.         x = (event.x_root - self.x - self.root.winfo_rootx() + self.root.winfo_rootx())
  171.         y = (event.y_root - self.y - self.root.winfo_rooty() + self.root.winfo_rooty())
  172.         root.geometry("+%s+%s" % (x, y))
  173.  
  174. WindowDraggable(root)
  175. root.overrideredirect(True) # turns off title bar, geometry
  176. root.attributes( '-topmost', 1 ) # ADDITINAL topmost
  177. root.mainloop() #This method will loop forever, waiting for events from the user, until it exits the program
  178.  
RAW Paste Data