With every left mouse click, self.LEFT_MB_Counter
increments so the value is always changing. I want the value in self.LEFT_MB_Counter
to be displayed in the entry field self.left_MB_entry
but I'm unable to achieve this.
How can I get the entry field to always update and display the current value in self.LEFT_MB_Counter
?
from win32api import GetKeyState
import tkinter.ttk
import tkinter
class MainApplication:
"""Class that creates the widgets and window."""
def __init__(self, master):
"""Method that creates the class constructor."""
self.master = master
self.var = tkinter.IntVar(value=0)
self.left_MB_entry = self.Entry(self.var)
self.left_MB_entry.grid()
def Entry(self, text_var, justify="center"):
"""Method that defines a default entry field."""
entry = tkinter.ttk.Entry(self.master, textvariable=text_var, justify=justify)
return entry
class MouseCounter:
"""Class that counts mouse button clicks."""
def __init__(self):
"""Method that creates the class constructor."""
self.LEFT_MB = 0x01 # Virtual-key code from Microsoft for LEFT MButton
self.Old_State_LEFT_MB = GetKeyState(self.LEFT_MB) # LEFT MButton Down = -127 or -128, MButton Up = 0 or 1
self.LEFT_MB_Counter = 0 # Initialize to 0
def count(self):
# Following block of code monitors LEFT MButton
New_State_LEFT_MB = GetKeyState(self.LEFT_MB)
if New_State_LEFT_MB != self.Old_State_LEFT_MB: # Button state changed
self.Old_State_LEFT_MB = New_State_LEFT_MB
print(New_State_LEFT_MB)
if New_State_LEFT_MB < 0:
self.LEFT_MB_Counter += 1
print("Count:", self.LEFT_MB_Counter)
print('Left Button Pressed')
else:
print('Left Button Released')
root.after(1, self.count)
root = tkinter.Tk()
root.style = tkinter.ttk.Style()
# ('winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative')
root.style.theme_use("clam")
APP = MainApplication(root) # Create object instance of the user interface
root.after(0, MouseCounter().count())
root.mainloop() # Display the user interface