Python Forum

Full Version: Tkinter Button Settings
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a problem about buttons. Example:

root=Tk()

def callback():
     print("My name is Kaan")

buton=ttk.Button(root, text="Callback", command=callback)
buton.pack()
That's okay. It works but it works when I pulled click from button. I need my function works when I clicked (at the moment of pushed). Also, I need my function work like during I am pushing to button (holding) then output might be

Output:
My name is Kaan My name is Kaan My name is Kaan My name is Kaan My name is Kaan . . .
So is there any setting about push-pull of buttons?

Thank you all.
This will allow printing on each click of button:
import tkinter as tk

def callback():
     print("My name is Kaan")

def try_button(root):
    buton=tk.Button(root, text="Callback", command=callback)
    buton.pack()
# root.mainloop()

def main():
    root = tk.Tk()
    try_button(root)
    root.mainloop()

if __name__ == '__main__':
    main()
In order to repeat on hold, you would need to re-sample the button after waiting for about 250 miliseconds, if still pressed, execute the callback again. I did something very similar to this quite a while ago, it was for single and double click, and for wxpython, not tkinter but logic would be almost identical.

see: https://python-forum.io/Thread-wxpython-...ght=double
I run your code but still same. Function works when I pulled mouse from button. I need my function run at the moment of pushing the button. I could not find any setting about that.

Thank you.
Misunderstood, but I understand now.
for button down, bind using <Button-n> where n = 1, 2 , 3 for left, middle, or right mouse click.
for button up, use <ButtonRelease-n>, where n = 1, 2 , 3 for left, middle, or right mouse click.
So for immediately when button is pressed:
import tkinter as tk

def callback(event):
     print("My name is Kaan")

def try_button(root):
    buton=tk.Button(root, text="Callback")
    buton.bind('<Button-1>', callback)
    buton.pack()
# root.mainloop()

def main():
    root = tk.Tk()
    try_button(root)
    root.mainloop()

if __name__ == '__main__':
    main()
and So for immediately after button is released:
import tkinter as tk

def callback(event):
     print("My name is Kaan")

def try_button(root):
    buton=tk.Button(root, text="Callback")
#     buton.bind('<Button-1>', callback)
    buton.bind('<ButtonRelease-1>', callback)
    buton.pack()
# root.mainloop()

def main():
    root = tk.Tk()
    try_button(root)
    root.mainloop()

if __name__ == '__main__':
    main()