GUI Button hold down - tkinter

后端 未结 5 831
天命终不由人
天命终不由人 2020-12-06 15:50

I\'m trying to do a GUI in python to control my robotic car. My question is how I do a function that determine a hold down button. I want to move the car when the button is

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-06 16:30

    @ Danny Try the following code:

    def stop_motor(event): print('button released') return False

    This answer run print 'test statement' one time. The while loop run one time when the button is pressed.

    @ Bryan Oakley Set a flag when the button is pressed, unset the flag when the button is released. There's no need for a loop since you're already running a loop (mainloop)

    from Tkinter import * 
    running = False
    
    root = Tk()
    def start_motor(event):
        global running
        running = True
        print("starting motor...")
    
    def stop_motor(event):
        global running
        print("stopping motor...")
        running = False
    
    button = Button(root, text ="forward")
    button.pack(side=LEFT)
    root.bind('',start_motor)
    root.bind('',stop_motor)
    root.mainloop()
    

    This answer above stays in a infinite loop when the button is pressed.

    @ Joseph FarahYou might want to try the repeatinterval option. The way it works is a button will continually fire as long as the user holds it down. The repeatinterval parameter essentially lets the program know how often it should fire the button if so. Here is a link to the explanation:

    http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/button.html

    Search in-page for "repeatinterval".

    Another name for this parameter is repeatdelay.

    I set the repeat interval in the parameter option for the button widget but it doesn't repeat the command.

    Thanks for all the answer . Still looking to solve this problem.

提交回复
热议问题