famansour

Tracker -- 2 counters- Work very well - Windows 2 is not draggable

Oct 20th, 2020
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 17.77 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.         hours = int(elap/(60.0*60.0))
  101.         minutes = int((elap- hours*60.0*60.0)/60.0)
  102.         seconds = int(elap- hours*60.0*60.0 - minutes*60.0)
  103.         # hseconds = int((elap - minutes*60.0 - seconds)*100)                        
  104.         self.timestr.set('%02d:%02d:%02d' % (hours, minutes, seconds)) # convert date and time and datetime objects to its equivalent string
  105.         # print(('%02d:%02d:%02d' % (minutes, seconds, hseconds)))
  106.        
  107.     def Start(self):          
  108.         if not self._running:  
  109.             self._start = time.time() - self._elapsedtime
  110.             self._update()
  111.             self._running = 1
  112.     def Stop(self):
  113.         if self._running:
  114.             self.after_cancel(self._timer)            
  115.             self._elapsedtime = time.time() - self._start    
  116.             self._setTime(self._elapsedtime)
  117.             self._running = 0
  118.             # print(self._elapsedtime)
  119.             # print ('after_cancel(', self._timer, ')')
  120.  
  121.  
  122.     def Reset(self):                                  
  123.         """ Reset the stopwatch. """
  124.         self._start = time.time()        
  125.         self._elapsedtime = 0.0    
  126.         self._setTime(self._elapsedtime)
  127.    # if not StopWatch1Local._running:
  128. def mouse1Clicked():
  129.     global StopWatch1
  130.     global StopWatch2
  131.     # StopWatch1._Label1.pack_forget()
  132.     # StopWatch2._Label1.pack_forget()
  133.     # LabelEff.pack_forget()
  134.     # if (StopWatch1._running and StopWatch2._running) :
  135.    
  136.         # StopWatch1.Stop()
  137.         # StopWatch2.Start()
  138.         # root.configure(bg='orange')
  139.         # StopWatch1._Label1.configure(bg='orange')
  140.         # StopWatch1.configure(bg='orange')
  141.         # StopWatch2._Label1.configure(bg='orange')
  142.         # StopWatch2.configure(bg='orange')
  143.     if  (StopWatch1._running==1 and StopWatch2._running==0) :  
  144.  
  145.         StopWatch1.Stop()
  146.         StopWatch2.Start()
  147.         StopWatch3.Stop()
  148.         StopWatch4.Start()
  149.         root.configure(bg='orange')
  150.         StopWatch1._Label1.configure(bg='orange')
  151.         StopWatch1.configure(bg='orange')
  152.         StopWatch2._Label1.configure(bg='orange')
  153.         StopWatch2.configure(bg='orange')
  154.         StopWatch3._Label1.configure(bg='orange')
  155.         StopWatch3.configure(bg='orange')
  156.         StopWatch4._Label1.configure(bg='orange')
  157.         StopWatch4.configure(bg='orange')
  158.        
  159.         ###
  160.  
  161.         StopWatch11.Stop()
  162.         StopWatch22.Start()
  163.         StopWatch33.Stop()
  164.         StopWatch44.Start()
  165.         window.configure(bg='orange')
  166.         StopWatch11._Label1.configure(bg='orange')
  167.         StopWatch11.configure(bg='orange')
  168.         StopWatch22._Label1.configure(bg='orange')
  169.         StopWatch22.configure(bg='orange')
  170.         StopWatch33._Label1.configure(bg='orange')
  171.         StopWatch33.configure(bg='orange')
  172.         StopWatch44._Label1.configure(bg='orange')
  173.         StopWatch44.configure(bg='orange')
  174.     elif  (StopWatch1._running==0 and StopWatch2._running==0) :  
  175.    
  176.         StopWatch1.Stop()
  177.         StopWatch2.Start()
  178.         StopWatch3.Stop()
  179.         StopWatch4.Start()
  180.         root.configure(bg='orange')
  181.         StopWatch1._Label1.configure(bg='orange')
  182.         StopWatch1.configure(bg='orange')
  183.         StopWatch2._Label1.configure(bg='orange')
  184.         StopWatch2.configure(bg='orange')
  185.         StopWatch3._Label1.configure(bg='orange')
  186.         StopWatch3.configure(bg='orange')
  187.         StopWatch4._Label1.configure(bg='orange')
  188.         StopWatch4.configure(bg='orange')
  189.         ######
  190.        
  191.         StopWatch11.Stop()
  192.         StopWatch22.Start()
  193.         StopWatch33.Stop()
  194.         StopWatch44.Start()
  195.         window.configure(bg='orange')
  196.         StopWatch11._Label1.configure(bg='orange')
  197.         StopWatch11.configure(bg='orange')
  198.         StopWatch22._Label1.configure(bg='orange')
  199.         StopWatch22.configure(bg='orange')
  200.         StopWatch33._Label1.configure(bg='orange')
  201.         StopWatch33.configure(bg='orange')
  202.         StopWatch44._Label1.configure(bg='orange')
  203.         StopWatch44.configure(bg='orange')
  204.     elif  (StopWatch1._running==0 and StopWatch2._running==1) :  
  205.         StopWatch1.Start()
  206.         StopWatch2.Stop()
  207.         StopWatch3.Stop()
  208.         StopWatch4.Start()
  209.         window.configure(bg='lime')
  210.         StopWatch11._Label1.configure(bg='lime')
  211.         StopWatch11.configure(bg='lime')
  212.         StopWatch22._Label1.configure(bg='lime')
  213.         StopWatch22.configure(bg='lime')
  214.         StopWatch33._Label1.configure(bg='lime')
  215.         StopWatch33.configure(bg='lime')
  216.         StopWatch44._Label1.configure(bg='lime')
  217.         StopWatch44.configure(bg='lime')
  218.        
  219.         ############
  220.         StopWatch11.Start()
  221.         StopWatch22.Stop()
  222.         StopWatch33.Stop()
  223.         StopWatch44.Start()
  224.         root.configure(bg='lime')
  225.         StopWatch1._Label1.configure(bg='lime')
  226.         StopWatch1.configure(bg='lime')
  227.         StopWatch2._Label1.configure(bg='lime')
  228.         StopWatch2.configure(bg='lime')
  229.         StopWatch3._Label1.configure(bg='lime')
  230.         StopWatch3.configure(bg='lime')
  231.         StopWatch4._Label1.configure(bg='lime')
  232.         StopWatch4.configure(bg='lime')
  233. def mouse2Clicked():
  234.     global StopWatch1
  235.     global StopWatch2
  236.  
  237.    
  238.     # StopWatch1._Label1.pack()
  239.     # StopWatch2._Label1.pack()
  240.     # LabelEff.pack()
  241.     if (StopWatch3._running==1 ) :  
  242.         StopWatch1.Stop()
  243.         StopWatch2.Stop()
  244.         StopWatch3.Stop()
  245.         StopWatch4.Stop()
  246.         root.configure(bg='white')
  247.         StopWatch1._Label1.configure(bg='white')
  248.         StopWatch1.configure(bg='white')
  249.         StopWatch2._Label1.configure(bg='white')
  250.         StopWatch2.configure(bg='white')
  251.         StopWatch3._Label1.configure(bg='white')
  252.         StopWatch3.configure(bg='white')
  253.         StopWatch4._Label1.configure(bg='white')
  254.         StopWatch4.configure(bg='white')
  255.         # StopWatch1._Label1.pack_forget()        
  256.         # StopWatch2._Label1.pack_forget()
  257.         # LabelEff.pack_forget()
  258.        
  259.         ###
  260.        
  261.         StopWatch11.Stop()
  262.         StopWatch22.Stop()
  263.         StopWatch33.Stop()
  264.         StopWatch44.Stop()
  265.         window.configure(bg='white')
  266.         StopWatch11._Label1.configure(bg='white')
  267.         StopWatch11.configure(bg='white')
  268.         StopWatch22._Label1.configure(bg='white')
  269.         StopWatch22.configure(bg='white')
  270.         StopWatch33._Label1.configure(bg='white')
  271.         StopWatch33.configure(bg='white')
  272.         StopWatch44._Label1.configure(bg='white')
  273.         StopWatch44.configure(bg='white')
  274.     elif  (StopWatch3._running==0) :  
  275.         StopWatch1.Stop()
  276.         StopWatch2.Stop()
  277.         StopWatch3.Start()
  278.         StopWatch4.Start()
  279.        
  280.         root.configure(bg='red')
  281.         StopWatch1._Label1.configure(bg='red')
  282.         StopWatch1.configure(bg='red')
  283.         StopWatch2._Label1.configure(bg='red')
  284.         StopWatch2.configure(bg='red')
  285.         StopWatch3._Label1.configure(bg='red')
  286.         StopWatch3.configure(bg='red')
  287.         StopWatch4._Label1.configure(bg='red')
  288.         StopWatch4.configure(bg='red')
  289.        
  290.         # StopWatch1._Label1.pack_forget()        
  291.         # StopWatch2._Label1.pack_forget()
  292.         # LabelEff.pack_forget()
  293.        
  294.         ###
  295.        
  296.         StopWatch11.Stop()
  297.         StopWatch22.Stop()
  298.         StopWatch33.Start()
  299.         StopWatch44.Start()
  300.        
  301.         window.configure(bg='red')
  302.         StopWatch11._Label1.configure(bg='red')
  303.         StopWatch11.configure(bg='red')
  304.         StopWatch22._Label1.configure(bg='red')
  305.         StopWatch22.configure(bg='red')
  306.         StopWatch33._Label1.configure(bg='red')
  307.         StopWatch33.configure(bg='red')
  308.         StopWatch44._Label1.configure(bg='red')
  309.         StopWatch44.configure(bg='red')
  310.  
  311.  
  312.     print("StopWatch1.timestr= ", StopWatch1._elapsedtime)    
  313.     print("StopWatch2.timestr= ", StopWatch2._elapsedtime)
  314.     print("Effeciency= ", round(StopWatch1._elapsedtime/StopWatch2._elapsedtime,2))
  315.     Effeciency.set([int(100*round(StopWatch1._elapsedtime/StopWatch2._elapsedtime,2)), '%']) # convert date and time and
  316.  
  317. root = tk.Tk() # initializing the tcl/tk interpreter and creating the root window
  318. # root.geometry("65x38+-2+0")
  319. root.geometry("65x78+-2+0")
  320.  
  321.  
  322. # root.geometry("47x38+-2+0")
  323. # root.geometry("100x100+-2+0")
  324. # root.minsize(150, 100)
  325. StopWatch1 = StopWatch(root)
  326. StopWatch1.pack()# Tkinter Pack Geometry Manager   side=tk.TOP
  327. # ----------------------
  328. # root = tk.Tk() # initializing the tcl/tk interpreter and creating the root window
  329. StopWatch2 = StopWatch(root)
  330. StopWatch2.pack()# Tkinter Pack Geometry Manager
  331. # ----------------------
  332. # frame = tk.Frame(root, width=100, height=100)
  333. # ----------------------
  334. # ----------------------
  335. # ----------------------
  336. StopWatch3 = StopWatch(root)
  337. StopWatch3.pack()# Tkinter Pack Geometry Manager   side=tk.TOP
  338.  
  339.  
  340. StopWatch4 = StopWatch(root)
  341. StopWatch4.pack()# Tkinter Pack Geometry Manager   side=tk.TOP
  342.  
  343. def ResetSession():                                  
  344.     """ Reset the stopwatch. """
  345.     StopWatch1.Reset()
  346.     StopWatch2.Reset()  
  347.     StopWatch3.Reset()
  348.     StopWatch4.Reset()  
  349.  
  350. def ResetDay():                                  
  351.     """ Reset the stopwatch. """
  352.     StopWatch1.Reset()
  353.     StopWatch2.Reset()  
  354.     StopWatch3.Reset()
  355.     StopWatch4.Reset()  
  356.     StopWatch11.Reset()
  357.     StopWatch22.Reset()  
  358.     StopWatch33.Reset()
  359.     StopWatch44.Reset()
  360. HideShowSessionCount=0
  361.  
  362. def HideShowSession():
  363.     global HideShowSessionCount
  364.     if HideShowSessionCount %2 ==0:
  365.         StopWatch1._Label1.pack_forget()        
  366.         StopWatch2._Label1.pack_forget()
  367.         StopWatch3._Label1.pack_forget()        
  368.         StopWatch4._Label1.pack_forget()
  369.         HideShowSessionCount+=1
  370.     else:
  371.         StopWatch1._Label1.pack()
  372.         StopWatch2._Label1.pack()    
  373.         StopWatch3._Label1.pack()
  374.         StopWatch4._Label1.pack()
  375.         HideShowSessionCount+=1
  376. HideShowDayCount=0    
  377. def HideShowDay():
  378.     global HideShowDayCount
  379.     if HideShowDayCount %2 ==0:
  380.         StopWatch11._Label1.pack_forget()        
  381.         StopWatch22._Label1.pack_forget()
  382.         StopWatch33._Label1.pack_forget()        
  383.         StopWatch44._Label1.pack_forget()
  384.         HideShowDayCount+=1
  385.     else:
  386.         StopWatch11._Label1.pack()
  387.         StopWatch22._Label1.pack()    
  388.         StopWatch33._Label1.pack()
  389.         StopWatch44._Label1.pack()
  390.         HideShowDayCount+=1
  391. def ShowAll():
  392.     StopWatch1._Label1.pack()
  393.     StopWatch2._Label1.pack()    
  394.     StopWatch3._Label1.pack()
  395.     StopWatch4._Label1.pack()
  396.     StopWatch11._Label1.pack()
  397.     StopWatch22._Label1.pack()    
  398.     StopWatch33._Label1.pack()
  399.     StopWatch44._Label1.pack()
  400. def HideAll():
  401.     StopWatch1._Label1.pack_forget()        
  402.     StopWatch2._Label1.pack_forget()
  403.     StopWatch3._Label1.pack_forget()        
  404.     StopWatch4._Label1.pack_forget()
  405.     StopWatch11._Label1.pack_forget()        
  406.     StopWatch22._Label1.pack_forget()
  407.     StopWatch33._Label1.pack_forget()        
  408.     StopWatch44._Label1.pack_forget()
  409.  
  410. def minimiser():
  411.     root.overrideredirect(0)
  412.     root.state('iconic')  
  413.             # root.iconify()    
  414.             # root.geometry("47x38+-2+0")
  415.             # root.overrideredirect(True)
  416. StopWatch1._Label1.pack_forget()
  417. StopWatch2._Label1.pack_forget()
  418. StopWatch3._Label1.pack_forget()
  419. StopWatch4._Label1.pack_forget()
  420.  
  421.  
  422. def new_window():
  423.     global window, StopWatch11, StopWatch22, StopWatch33, StopWatch44
  424.    
  425.     window = tk.Toplevel(root)
  426.     # WindowDraggable(window)
  427.     # window.overrideredirect(True) # turns off title bar, geometry
  428.     window.attributes( '-topmost', 1 ) # ADDITINAL topmost
  429.     # window.geometry("65x78+-2+0")
  430.    
  431.     StopWatch11 = StopWatch(window)
  432.     StopWatch11.pack()# Tkinter Pack Geometry Manager   side=tk.TOP
  433.     StopWatch22 = StopWatch(window)
  434.     StopWatch22.pack()# Tkinter Pack Geometry Manager
  435.     StopWatch33 = StopWatch(window)
  436.     StopWatch33.pack()# Tkinter Pack Geometry Manager   side=tk.TOP
  437.     StopWatch44 = StopWatch(window)
  438.     StopWatch44.pack()# Tkinter Pack Geometry Manager   side=tk.TOP
  439.    
  440.     window.bind("<Button-1>", lambda x=None: mouse1Clicked() )
  441.     window.bind("<Button-3>", lambda x=None: mouse2Clicked()) #bind Python functions and methods to events
  442.  
  443.     window.bind("<Alt-Button-2>", lambda x=None: sys.exit())
  444.     window.bind("<Control-Button-2>", lambda x=None: ResetDay())
  445.    
  446.    
  447.     # window.bind("<Shift-Button-2>", lambda x=None: Show())
  448.     window.bind("<Double-Button-2>", lambda x=None: HideShowDay())
  449.  
  450.    
  451.  
  452.  
  453. root.bind("<Button-1>", lambda x=None: mouse1Clicked() )
  454. root.bind("<Button-3>", lambda x=None: mouse2Clicked()) #bind Python functions and methods to events
  455. new_window()
  456.  
  457. root.bind("<Alt-Button-2>", lambda x=None: sys.exit())
  458. root.bind("<Control-Button-2>", lambda x=None: ResetSession())
  459.    
  460.  
  461. # root.bind("<Shift-Button-2>", lambda x=None: Show())
  462. root.bind("<Double-Button-2>", lambda x=None: HideShowSession())
  463.  
  464. # root.bind("<Alt-Button-2>", lambda x=None: Hide())
  465. # root.bind("<Shift-Button-2>", lambda x=None: Show())
  466.  
  467. # root.bind("<Control-Button-2>", lambda x=None: sys.exit())
  468. # root.bind("<Control-Button-3>", lambda x=None: Reset())
  469.  
  470. ###################
  471.  
  472.  
  473. # ----------------------
  474. # ----------------------
  475. # ----------------------
  476.  
  477. # if StopWatch2._elapsedtime!=0 :      
  478.     # root.Label1 = tk.Label(root, textvariable=round(StopWatch1._elapsedtime/StopWatch2._elapsedtime,2))
  479.     # root.Label1.pack(fill=tk.X, expand=tk.NO, pady=2, padx=2)
  480.  
  481. Effeciency = tk.StringVar()  
  482. LabelEff = tk.Label(root, textvariable=Effeciency)
  483. # LabelEff.pack()
  484.  
  485.  
  486. # time.sleep(1)
  487. # Effeciency.set(StopWatch2._elapsedtime-StopWatch1._elapsedtime) # convert date and time and
  488.  
  489. # labelText = tk.StringVar()
  490. # depositLabel = tk.Label(root, textvariable=labelText)
  491.  
  492. # def updateDepositLabel(txt): # you may have to use *args in some cases
  493.     # global labelText
  494.     # labelText.set(txt)
  495.    
  496. # updateDepositLabel(5)
  497. # labelText.pack()
  498.  
  499. WindowDraggable(root)
  500. root.overrideredirect(True) # turns off title bar, geometry
  501. root.attributes( '-topmost', 1 ) # ADDITINAL topmost
  502. HideAll()
  503. ShowAll()
  504. # Show()
  505.  
  506. root.mainloop() #This method will loop forever, waiting for events from the user, until it exits the program
  507.  
Add Comment
Please, Sign In to add comment