0

I am sorry for my English. It only detects once when I hold down the left click on the mouse.I want it to detect as long as I hold it down.

def Dataprint(event=None):
    UDP_IP_ADDRESS_SERVER = "xxx.xxx.xx.xxx"
    UDP_PORT_SERVER = 7005
    message = "#upto"
    client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
    client.sendto(message.encode('utf-8'), (UDP_IP_ADDRESS_SERVER, UDP_PORT_SERVER))
    print("sent")
    time.sleep(0.01)
ttk.Label(TAB2, text="bos",font=("Arial",20),foreground="#fff",borderwidth=2,background="#7b7bc0",width=9).grid(column=0, row=4, padx=10, pady=10)
emptyup=ttk.Button(TAB2,text="up")
emptyup.grid(column=0, row=5, padx=10, pady=10)
emptyup.bind("<Button-1>",Dataprint)
Seçkin
  • 11
  • 4
  • 1
    Does this answer your question? [How do I bind an event to the left mouse button being held down?](https://stackoverflow.com/questions/3288001/how-do-i-bind-an-event-to-the-left-mouse-button-being-held-down) Another useful answer is found [here](https://stackoverflow.com/questions/46227617/tkinter-event-for-downpress-of-mouse-button-holding-down). – Gareth Ma Dec 14 '22 at 12:14
  • Nope, i tried them before i don't want Motion – Seçkin Dec 14 '22 at 12:19
  • The selected answer to that linked question doesn't deal with motion. – Bryan Oakley Dec 14 '22 at 15:59
  • Sorry, i can't understand how the linked question will solve my problem, can you help me? – Seçkin Dec 15 '22 at 05:48

1 Answers1

0

I don't know anything about tkinter, so I can't provide any code for this. However, I imagine it is because when you bind, the function is triggered once only when button is pressed, and won't retrigger while it's held.

A solution to this is to call Dataprint again at the end of the function and maintaining a global variable (or a field in the class, as shown here) that indicates whether the user is still holding down the mouse. Something like this:

import time
mouse_is_pressed = False

def Dataprint(event):
    global emptyup
    # ...
    emptyup.after(20, function=None)
    if mouse_is_pressed:
        Dataprint(event)

def start_printing(event=None):
    global mouse_is_pressed
    mouse_is_pressed = True
    Dataprint(event)

def stop_printing(event=None):
    global mouse_is_pressed
    mouse_is_pressed = False

# I am not sure about the exact details, but I am sure you can find out
emptyup.bind("<Button-1>", start_printing)
emptyup.bind("<B1-Release>", stop_printing)

See if this works. I replaced time.sleep with .after from tkinter since time.sleep blocks the thread - see here.

Gareth Ma
  • 199
  • 10