Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- # from mttkinter import mtTkinter as tk
- import time
- # import classes
- import win32api
- from threading import Thread
- import sys
- class StopWatch(tk.Frame): #Class StopWatch inheriting from the Tkinter class Frame
- """ Implements a stop watch frame widget. """
- def __init__(self, parent=None, **kw):
- tk.Frame.__init__(self, parent, kw)
- # self.configure(bg='black')
- self._start = 0.0
- self._elapsedtime = 0.0
- self.timestr = tk.StringVar()
- self._running = 0
- self._Label1 = tk.Label(self, textvariable=self.timestr)
- self._setTime(self._elapsedtime)
- self._Label1.pack(fill=tk.X, expand=tk.NO, pady=2, padx=2)
- # def makeWidgets(self):
- # """ Make the time label. """
- # l = tk.Label(self, textvariable=self.timestr)
- # self._setTime(self._elapsedtime)
- # l.pack(fill=tk.X, expand=tk.NO, pady=2, padx=2)
- # l.configure(bg='white')
- # return l
- def _update(self):
- """ Update the label with elapsed time. """
- self._elapsedtime = time.time() - self._start
- self._setTime(self._elapsedtime)
- self._timer = self.after(50, self._update)
- # print (self._timer, '= self.after')
- def _setTime(self, elap):
- """ Set the time string to Minutes:Seconds:Hundreths """
- minutes = int(elap/60)
- seconds = int(elap - minutes*60.0)
- hseconds = int((elap - minutes*60.0 - seconds)*100)
- self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, hseconds)) # convert date and time and datetime objects to its equivalent string
- # print(('%02d:%02d:%02d' % (minutes, seconds, hseconds)))
- def Start(self):
- if not self._running:
- self._start = time.time() - self._elapsedtime
- self._update()
- self._running = 1
- def Stop(self):
- if self._running:
- self.after_cancel(self._timer)
- self._elapsedtime = time.time() - self._start
- self._setTime(self._elapsedtime)
- self._running = 0
- # print(self._elapsedtime)
- # print ('after_cancel(', self._timer, ')')
- def Reset(self):
- """ Reset the stopwatch. """
- self._start = time.time()
- self._elapsedtime = 0.0
- self._setTime(self._elapsedtime)
- # if not StopWatch1Local._running:
- def mouse1Clicked():
- global StopWatch1
- global StopWatch2
- if (StopWatch1._running and StopWatch2._running) :
- StopWatch1.Stop()
- root.configure(bg='red')
- StopWatch1._Label1.configure(bg='red')
- StopWatch1.configure(bg='red')
- StopWatch2._Label1.configure(bg='red')
- StopWatch2.configure(bg='red')
- elif (StopWatch1._running==0 and StopWatch2._running==0) :
- StopWatch1.Start()
- StopWatch2.Start()
- root.configure(bg='green')
- StopWatch1._Label1.configure(bg='green')
- StopWatch1.configure(bg='green')
- StopWatch2._Label1.configure(bg='green')
- StopWatch2.configure(bg='green')
- elif (StopWatch1._running==0 and StopWatch2._running==1) :
- StopWatch1.Start()
- StopWatch2.Start()
- root.configure(bg='green')
- StopWatch1._Label1.configure(bg='green')
- StopWatch1.configure(bg='green')
- StopWatch2._Label1.configure(bg='green')
- StopWatch2.configure(bg='green')
- def mouse2Clicked():
- global StopWatch1
- global StopWatch2
- if (StopWatch1._running==0 and StopWatch2._running==1) :
- StopWatch1.Stop()
- StopWatch2.Stop()
- root.configure(bg='white')
- StopWatch1._Label1.configure(bg='white')
- StopWatch1.configure(bg='white')
- StopWatch2._Label1.configure(bg='white')
- StopWatch2.configure(bg='white')
- elif (StopWatch1._running==1 and StopWatch2._running==1) :
- StopWatch1.Stop()
- StopWatch2.Stop()
- root.configure(bg='white')
- StopWatch1._Label1.configure(bg='white')
- StopWatch1.configure(bg='white')
- StopWatch2._Label1.configure(bg='white')
- StopWatch2.configure(bg='white')
- root = tk.Tk() # initializing the tcl/tk interpreter and creating the root window
- root.geometry("47x38+-2+0")
- StopWatch1 = StopWatch(root)
- StopWatch1.pack()# Tkinter Pack Geometry Manager side=tk.TOP
- print (StopWatch1._running)
- # ----------------------
- # root = tk.Tk() # initializing the tcl/tk interpreter and creating the root window
- StopWatch2 = StopWatch(root)
- StopWatch2.pack()# Tkinter Pack Geometry Manager
- print (StopWatch2._running)
- # ----------------------
- frame = tk.Frame(root, width=100, height=100)
- # ----------------------
- # ----------------------
- # ----------------------
- def Reset():
- """ Reset the stopwatch. """
- mouse2Clicked()
- StopWatch1.Reset()
- StopWatch2.Reset()
- root.bind("<Button-1>", lambda x=None: mouse1Clicked() )
- root.bind("<Button-3>", lambda x=None: mouse2Clicked()) #bind Python functions and methods to events
- root.bind("<Control-Button-2>", lambda x=None: sys.exit())
- root.bind("<Control-Button-3>", lambda x=None: Reset())
- # ----------------------
- # ----------------------
- # ----------------------
- class WindowDraggable():
- def __init__(self, root):
- self.root = root
- root.bind('<ButtonPress-2>', self.StartMove)
- root.bind('<ButtonRelease-2>', self.StopMove)
- root.bind('<B2-Motion>', self.OnMotion)
- def StartMove(self, event):
- self.x = event.x
- self.y = event.y
- def StopMove(self, event):
- self.x = None
- self.y = None
- def OnMotion(self,event):
- x = (event.x_root - self.x - self.root.winfo_rootx() + self.root.winfo_rootx())
- y = (event.y_root - self.y - self.root.winfo_rooty() + self.root.winfo_rooty())
- root.geometry("+%s+%s" % (x, y))
- WindowDraggable(root)
- root.overrideredirect(True) # turns off title bar, geometry
- root.attributes( '-topmost', 1 ) # ADDITINAL topmost
- root.mainloop() #This method will loop forever, waiting for events from the user, until it exits the program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement