Tkinter triggers ButtonPress event on touchscreen only after touch is released

463 Views Asked by At

I'm building a GUI in Python (3.11) and tkinter on a Windows 10 PC with a touchscreen. The GUI controls a camera which should zoom in while a button is pressed. Binding a ButtonPress and ButtonRelease event to the widget works perfectly with a normal mouse, but doesn't work when using a touchscreen.

Windows normally uses press-and-hold on a touchscreen to simulate right-click. You can disable this functionality through: Control Panel->Pen and Touch->Press and hold->Settings... ; or through: Registry: \HKCU\SOFTWARE\Microsoft\Wisp\Touch->TouchMode_hold (0).

However, when using the code below with above settings on a touchscreen the ButtonPress event is not triggered when the button is pressed and held, but is triggered only when the button is released. The ButtonPress and ButtonRelease event are triggered right after each other. The ButtonPress event also triggers when the button is held and you move/swipe on the screen.

With this minimal code example the problem can be reproduced.

import tkinter as tk

def start_zoom(event):
    print(f'{event}')

def stop_zoom(event):
    print(f'{event}')

root = tk.Tk()
label = tk.Label(root, text='Zoom in', width=50, height=20)
label.bind('<ButtonPress-1>', start_zoom)
label.bind('<ButtonRelease-1>', stop_zoom)
label.pack()
root.mainloop()
1

There are 1 best solutions below

4
Reyot On

Instead of two functions - start and stop, you can use repeatdelay and repeatinterval with single function zoom()

import tkinter as tk


def zoom():
    print('zooming')


root = tk.Tk()
root.geometry("400x400+500+200")
zoom_btn = tk.Button(root, text='Zoom in', command=zoom, width=20, height=5, repeatdelay=150, repeatinterval=100)
zoom_btn.pack()
root.mainloop()