Python Forum
Tkinter Button Settings
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tkinter Button Settings
#1
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.
Reply
#2
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
Reply
#3
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.
Reply
#4
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] TKinter Remove Button Frame Nu2Python 8 977 Jan-16-2024, 06:44 PM
Last Post: rob101
  tkinter - touchscreen, push the button like click the mouse John64 5 840 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  Centering and adding a push button to a grid window, TKinter Edward_ 15 4,717 May-25-2023, 07:37 PM
Last Post: deanhystad
  Can't get tkinter button to change color based on changes in data dford 4 3,416 Feb-13-2022, 01:57 PM
Last Post: dford
  Creating a function interrupt button tkinter AnotherSam 2 5,522 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 5,006 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  tkinter showing image in button rwahdan 3 5,617 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  tkinter button image Nick_tkinter 4 4,030 Mar-04-2021, 11:33 PM
Last Post: deanhystad
  tkinter python button position problem Nick_tkinter 3 3,557 Jan-31-2021, 05:15 AM
Last Post: deanhystad
  TKinter restarting the mainloop when button pressed zazas321 7 16,319 Jan-26-2021, 06:38 AM
Last Post: zazas321

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020