How do I update Tkinter label that has mouse click in it?

48 views Asked by At

While using mouse.click() function on tkinter label with multiprocessing/multithreading seem to freeze it up
is there any other way to mimic mouse clicks and movement?

I'm trying to make autoclicker that displays if it's turned on or off next to mouse.

I'm struggling to find a solution how to update tinker label:

Here's my code both versions:

MultiProcessing version:

import mouse
import keyboard
from time import sleep

import tkinter as tk
import multiprocessing


ON = "Clicker: ON"
OFF = "Clicker: OFF"

text = OFF

root = tk.Tk()
root.title("Sticky Window")

root.overrideredirect(True)
root.attributes("-alpha", 0.7)
root.wm_attributes("-topmost", 1)

label = tk.Label(root, text=text, font=("Arial", 10))
label.pack(padx=10, pady=5)

def haha():
    
     update_position()
     root.mainloop()


def update_position():
    x, y = root.winfo_pointerx(), root.winfo_pointery()
    root.geometry(f"90x25+{x+1}+{y+1}")
    root.after(50, update_position)

def main():
     Clicker = 0
     global text
     while True:
          
          if keyboard.is_pressed('z'):
               text = ON
               print(text)
               Clicker = 1
          while Clicker == 1:
               if keyboard.is_pressed('x'):
                    text = OFF
                    print(text)
                    Clicker = 0
               mouse.click()
               sleep(0.5) ## 0.03
          if keyboard.is_pressed('esc'):
               break

if __name__ == "__main__":
    process1 = multiprocessing.Process(target=haha)
    process2 = multiprocessing.Process(target=main)

    process1.start()
    process2.start()

    process1.join()
    process2.join()

MultiThreading version:

import threading
import mouse
import keyboard
from time import sleep

import tkinter as tk


ON = "Clicker: ON"
OFF = "Clicker: OFF"
sstate = False


root = tk.Tk()
root.title("Sticky Window")

root.overrideredirect(True)
root.attributes("-alpha", 0.7)
root.wm_attributes("-topmost", 1)


text = tk.StringVar()
text.set(OFF)

label = tk.Label(root, textvariable=text, font=("Arial", 10))
label.pack(padx=10, pady=5)


def update_position():
     
    x, y = root.winfo_pointerx(), root.winfo_pointery()
    root.geometry(f"90x25+{x+1}+{y+1}")
    root.after(50, update_position)
    

def updtext():
    global text
    global sstate
    if keyboard.is_pressed('z'):
        text.set(ON)
        sstate = True
        
    if keyboard.is_pressed('x'):
        text.set(OFF)
        sstate = False
        
    root.after(50,updtext)
        
        
def state():
    
    while sstate == True:
        mouse.click()
        print(sstate)
        sleep(3)

    else:
        print(sstate)
        
    root.after(50,state)

one = threading.Thread(target=update_position)
two = threading.Thread(target=updtext)
three = threading.Thread(target=state)

one.start()
two.start()
three.start()

root.mainloop()

Tried using label.configure with no success.

I've redone it with multihreading, however I think using mouse.click() is a problem, tinker label seems to freeze up whenever it is used.

0

There are 0 answers