0

How do I make a button use it's command whenever it is active? For example, if you hold a repeating button for 1 second, it should have executed it's command about 250 to 1000 times already right? But with a regular button, it just executes it one time, until you release the mouse and click it again. How can I make a button like that? I currently have this with moving,

def up(player):
        x = 0
        y = -10
        c.move(player.rect,x,y)
    def down(player):
        x = 0
        y = 10
        c.move(player.rect,x,y)
    def left(player):
        x = -10
        y = 0
        c.move(player.rect,x,y)
    def right(player):
        x = 10
        y = 0
        c.move(player.rect,x,y)
    #functions are in a class called player
p = player(sx1=950,sy1=540,sx2=975,sy2=565)
lup = lambda : tk.after(50,p.up)
upbtn = Button(tk,width=3,height=2,text="↑",command=lup)
upbtn.pack()
upbtn.place(x=960,y=700)
mainloop()

I searched on the web, but it didn't show any results consistent with my problem. Thanks in advance!

0

1 Answer 1

1

Use option repeatdelay and repeatinterval of tkinter.Button.

  • repeatdelay, the number of milliseconds to wait before starting to repeat.
  • repeatinterval, the number of milliseconds to be used between repeats

Simple demo code,

from tkinter import *

def on_click():
    global count
    count += 1
    print(f'click {count}')

root = Tk()

count = 0
button = Button(root, text='Fire', repeatdelay=100, repeatinterval=100, command=on_click)
button.pack()

root.mainloop()
Sign up to request clarification or add additional context in comments.

2 Comments

unknown option "-repeatinterval" happened if you use ttk.Button, not tk.Button
@user14354511: Please accept this answer if it worked for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.