Advertisement
famansour

Tracker -- reset and close work fucking good

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