Advertisement
famansour

Tracker -- Best Geometry for 2 counters

Sep 15th, 2020
1,060
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.42 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.  
  8. class StopWatch(tk.Frame):  #Class StopWatch inheriting from the Tkinter class Frame
  9.     """ Implements a stop watch frame widget. """                                                                
  10.     def __init__(self, parent=None, **kw):        
  11.         tk.Frame.__init__(self, parent, kw)
  12.         # self.configure(bg='black')
  13.         self._start = 0.0        
  14.         self._elapsedtime = 0.0
  15.         self.timestr = tk.StringVar()              
  16.         self._running = 0
  17.        
  18.         self._Label1 = tk.Label(self, textvariable=self.timestr)
  19.         self._setTime(self._elapsedtime)
  20.         self._Label1.pack(fill=tk.X, expand=tk.NO, pady=2, padx=2)
  21.  
  22.  
  23.    
  24.     # def makeWidgets(self):                        
  25.         # """ Make the time label. """
  26.         # l = tk.Label(self, textvariable=self.timestr)
  27.         # self._setTime(self._elapsedtime)
  28.         # l.pack(fill=tk.X, expand=tk.NO, pady=2, padx=2)
  29.         # l.configure(bg='white')        
  30.         # return l  
  31.        
  32.     def _update(self):
  33.         """ Update the label with elapsed time. """
  34.         self._elapsedtime = time.time() - self._start
  35.         self._setTime(self._elapsedtime)
  36.         self._timer = self.after(50, self._update)
  37.         # print (self._timer, '= self.after')
  38.     def _setTime(self, elap):
  39.         """ Set the time string to Minutes:Seconds:Hundreths """
  40.         minutes = int(elap/60)
  41.         seconds = int(elap - minutes*60.0)
  42.         hseconds = int((elap - minutes*60.0 - seconds)*100)                
  43.         self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, hseconds)) # convert date and time and datetime objects to its equivalent string
  44.         # print(('%02d:%02d:%02d' % (minutes, seconds, hseconds)))
  45.        
  46.     def Start(self):          
  47.         if not self._running:  
  48.             self._start = time.time() - self._elapsedtime
  49.             self._update()
  50.             self._running = 1
  51.     def Stop(self):
  52.         if self._running:
  53.             self.after_cancel(self._timer)            
  54.             self._elapsedtime = time.time() - self._start    
  55.             self._setTime(self._elapsedtime)
  56.             self._running = 0
  57.             # print(self._elapsedtime)
  58.             # print ('after_cancel(', self._timer, ')')
  59.  
  60.  
  61.     # def Reset(self):                                  
  62.         # """ Reset the stopwatch. """
  63.         # self._start = time.time()        
  64.         # self._elapsedtime = 0.0    
  65.         # self._setTime(self._elapsedtime)
  66.    # if not StopWatch1Local._running:
  67. def mouse1Clicked():
  68.     global StopWatch1
  69.     global StopWatch2
  70.     if (StopWatch1._running and StopWatch2._running) :
  71.    
  72.         StopWatch1.Stop()
  73.         root.configure(bg='red')
  74.         StopWatch1._Label1.configure(bg='red')
  75.         StopWatch1.configure(bg='red')
  76.         StopWatch2._Label1.configure(bg='red')
  77.         StopWatch2.configure(bg='red')    
  78.     elif  (StopWatch1._running==0 and StopWatch2._running==0) :  
  79.    
  80.         StopWatch1.Start()
  81.         StopWatch2.Start()
  82.         root.configure(bg='green')
  83.         StopWatch1._Label1.configure(bg='green')
  84.         StopWatch1.configure(bg='green')
  85.         StopWatch2._Label1.configure(bg='green')
  86.         StopWatch2.configure(bg='green')
  87.     elif  (StopWatch1._running==0 and StopWatch2._running==1) :  
  88.         StopWatch1.Start()
  89.         StopWatch2.Start()
  90.         root.configure(bg='green')
  91.         StopWatch1._Label1.configure(bg='green')
  92.         StopWatch1.configure(bg='green')
  93.         StopWatch2._Label1.configure(bg='green')
  94.         StopWatch2.configure(bg='green')
  95. def mouse2Clicked():
  96.     global StopWatch1
  97.     global StopWatch2
  98.     if (StopWatch1._running==0 and StopWatch2._running==1) :  
  99.         StopWatch1.Stop()
  100.         StopWatch2.Stop()
  101.         root.configure(bg='white')
  102.         StopWatch1._Label1.configure(bg='white')
  103.         StopWatch1.configure(bg='white')
  104.         StopWatch2._Label1.configure(bg='white')
  105.         StopWatch2.configure(bg='white')
  106.     elif  (StopWatch1._running==1 and StopWatch2._running==1) :  
  107.         StopWatch1.Stop()
  108.         StopWatch2.Stop()  
  109.         root.configure(bg='white')
  110.         StopWatch1._Label1.configure(bg='white')
  111.         StopWatch1.configure(bg='white')
  112.         StopWatch2._Label1.configure(bg='white')
  113.         StopWatch2.configure(bg='white')
  114.        
  115.        
  116. root = tk.Tk() # initializing the tcl/tk interpreter and creating the root window
  117. root.geometry("46x38")
  118. StopWatch1 = StopWatch(root)
  119. StopWatch1.pack()# Tkinter Pack Geometry Manager   side=tk.TOP
  120. print (StopWatch1._running)
  121. # ----------------------
  122. # root = tk.Tk() # initializing the tcl/tk interpreter and creating the root window
  123. StopWatch2 = StopWatch(root)
  124. StopWatch2.pack()# Tkinter Pack Geometry Manager
  125. print (StopWatch2._running)
  126. # ----------------------
  127. frame = tk.Frame(root, width=100, height=100)
  128.  
  129. root.bind("<Button-1>", lambda x=None: mouse1Clicked() )
  130. #bind Python functions and methods to events
  131.  
  132. # frame.bind("<Button-1>", lambda x=None: [mouse1Clicked(iLoop,StopWatch1), mouse2Clicked(jLoop,StopWatch2)])
  133. # root.pack() # Tkinter Pack Geometry Manager
  134. # ----------------------
  135. root.bind("<Button-3>", lambda x=None: mouse2Clicked()) #bind Python functions and methods to events
  136. # root.pack() # Tkinter Pack Geometry Manager
  137.  
  138. # ----------------------
  139. # ----------------------
  140. # ----------------------
  141. # ----------------------
  142. # Program work only inside tkinter window ends here
  143. # ----------------------
  144. # ----------------------
  145. # ----------------------
  146. # ----------------------
  147.  
  148.  
  149. CurrentKeyState = win32api.GetKeyState(0x04)  # "0x04" is the Virtual-Key Code of the Middle mouse button (VK_MBUTTON)
  150. def firstFunction():
  151.     while True:
  152.         NewKeyState = win32api.GetKeyState(0x04)
  153.         global CurrentKeyState
  154.         if NewKeyState != CurrentKeyState:  # Button state changed
  155.  
  156.             if NewKeyState < 0:  # KeyState = -127 or -128
  157.                 # print('BUTTON PRESSED ---', 'NewKeyState = ', NewKeyState )
  158.                 pass
  159.             else:                # KeyState =1 or 0
  160.                 # print('BUTTON RELEASED ---', 'NewKeyState = ', NewKeyState)
  161.                 mouseClicked(iLoop,StopWatch1)
  162.             CurrentKeyState = NewKeyState
  163.            
  164.         time.sleep(0.001)
  165.    
  166. thread1  = Thread(target = firstFunction)
  167. # thread1.setDaemon(True)
  168. # thread1.start()  # This Ligne is to be uncommented to go back where I left
  169.  
  170. # root.after(1, firstFunction())
  171. class WindowDraggable():
  172.  
  173.     def __init__(self, root):
  174.         self.root = root
  175.         root.bind('<ButtonPress-2>', self.StartMove)
  176.         root.bind('<ButtonRelease-2>', self.StopMove)
  177.         root.bind('<B2-Motion>', self.OnMotion)
  178.  
  179.     def StartMove(self, event):
  180.         self.x = event.x
  181.         self.y = event.y
  182.  
  183.     def StopMove(self, event):
  184.         self.x = None
  185.         self.y = None
  186.  
  187.     def OnMotion(self,event):
  188.         x = (event.x_root - self.x - self.root.winfo_rootx() + self.root.winfo_rootx())
  189.         y = (event.y_root - self.y - self.root.winfo_rooty() + self.root.winfo_rooty())
  190.         root.geometry("+%s+%s" % (x, y))
  191.  
  192. WindowDraggable(root)
  193. root.overrideredirect(True) # turns off title bar, geometry
  194. root.attributes( '-topmost', 1 ) # ADDITINAL topmost
  195. root.mainloop() #This method will loop forever, waiting for events from the user, until it exits the program
  196.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement