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
- 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._start = 0.0
- self._elapsedtime = 0.0
- self.timestr = tk.StringVar()
- self.makeWidgets()
- self._running = 0
- 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)
- 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')
- elif (StopWatch1._running==0 and StopWatch2._running==0) :
- StopWatch1.Start()
- StopWatch2.Start()
- root.configure(bg='green')
- elif (StopWatch1._running==0 and StopWatch2._running==1) :
- StopWatch1.Start()
- StopWatch2.Start()
- root.configure(bg='green')
- def mouse2Clicked():
- global StopWatch1
- global StopWatch2
- if (StopWatch1._running==0 and StopWatch2._running==1) :
- StopWatch1.Stop()
- StopWatch2.Stop()
- root.configure(bg='grey')
- elif (StopWatch1._running==1 and StopWatch2._running==1) :
- StopWatch1.Stop()
- StopWatch2.Stop()
- root.configure(bg='grey')
- root = tk.Tk() # initializing the tcl/tk interpreter and creating the root window
- root.geometry("250x100")
- 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)
- frame.bind("<Button-1>", lambda x=None: mouse1Clicked() )
- #bind Python functions and methods to events
- # frame.bind("<Button-1>", lambda x=None: [mouse1Clicked(iLoop,StopWatch1), mouse2Clicked(jLoop,StopWatch2)])
- frame.pack() # Tkinter Pack Geometry Manager
- # ----------------------
- frame.bind("<Button-3>", lambda x=None: mouse2Clicked()) #bind Python functions and methods to events
- frame.pack() # Tkinter Pack Geometry Manager
- # ----------------------
- # ----------------------
- # ----------------------
- # ----------------------
- # Program work only inside tkinter window ends here
- # ----------------------
- # ----------------------
- # ----------------------
- # ----------------------
- CurrentKeyState = win32api.GetKeyState(0x04) # "0x04" is the Virtual-Key Code of the Middle mouse button (VK_MBUTTON)
- def firstFunction():
- while True:
- NewKeyState = win32api.GetKeyState(0x04)
- global CurrentKeyState
- if NewKeyState != CurrentKeyState: # Button state changed
- if NewKeyState < 0: # KeyState = -127 or -128
- # print('BUTTON PRESSED ---', 'NewKeyState = ', NewKeyState )
- pass
- else: # KeyState =1 or 0
- # print('BUTTON RELEASED ---', 'NewKeyState = ', NewKeyState)
- mouseClicked(iLoop,StopWatch1)
- CurrentKeyState = NewKeyState
- time.sleep(0.001)
- thread1 = Thread(target = firstFunction)
- # thread1.setDaemon(True)
- # thread1.start() # This Ligne is to be uncommented to go back where I left
- # root.after(1, firstFunction())
- 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.attributes( '-topmost', 1 ) # ADDITINAL topmost
- root.mainloop() #This method will loop forever, waiting for events from the user, until it exits the program
Add Comment
Please, Sign In to add comment