Kiri_23
Posts: 15
Joined: Sat Dec 26, 2015 3:19 am

Python Tkinter GUI button hold down

Wed Dec 30, 2015 1:38 am

Im 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 do when the button is press and hold run the car and when I release the button stop the car.

Code: Select all

from Tkinter import * 
hold_down = False 
root = Tk()
def button_hold(event):
      hold_down=true
      while hold_down== True: 
               print('test statement')
               hold_down = root.bind('<ButtonRelease-1>',stop_motor)

def stop_motor(event):
       hold_down= False
       print('button released')

button = Button(root, text ="forward")
button.pack(side=LEFT)
root.bind('<Button-1>',button_forward)
root.mainloop()

I try to do it in a while loop with a boolean when the user pressed the button the boolean change to true and enter the while loop still the user released the button that put the boolean to false and exit the loop but in this code the boolean stay always true not matter if I released the button.

I know this is a easy fix . Someone could help me. Thank

Edit: I want a function to be called repeatedly until a condition occurs. The function that I want to be called until the condition occurs is hold_down() that make the car run foward and the condition to check is the button is released.In other words while the button is pressed I want the function run_motor to be called repeatedly still I release the button, this is the condition to check. The problem with my code is always stays in the while loops printing 'test statememts' and doesn't check for the event button released.

If someone who watch this and have done what I want to do can kindly post a solution on how you do lt.

Update:I find a way to make it work . Thank. :)

note: English is not my native language
Last edited by Kiri_23 on Wed Dec 30, 2015 9:04 pm, edited 5 times in total.

User avatar
RogerW
Posts: 286
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: Python Tkinter GUI button hold down

Wed Dec 30, 2015 9:45 am

One way to do this is to create your own type of button.

Code: Select all

import Tkinter as tk

class MyBtn(tk.Button):
    # set function to call when pressed
    def set_down(self,fn):
        self.bind('<Button-1>',fn)
     
    # set function to be called when released
    def set_up(self,fn):
        self.bind('<ButtonRelease-1>',fn)
       
class Mainframe(tk.Frame):
    def __init__(self,master,*args,**kwargs):
        tk.Frame.__init__(self,master,*args,**kwargs)
        
        # create the button and set callback functions
        btn = MyBtn(self,text = 'Press ')
        btn.set_up(self.on_up)
        btn.set_down(self.on_down)
        btn.pack()
   
    # function called when pressed
    def on_down(self,x):
        print("Button down")

    # function called when released
    def on_up(self,x):
        print("Button up")
                
class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
               
        self.title('My Button')
        self.geometry('250x50')
              
        Mainframe(self).pack()
                    
# create and run an App object
App().mainloop()
This program prints a message when the button is pressed or released.

Kiri_23
Posts: 15
Joined: Sat Dec 26, 2015 3:19 am

Re: Python Tkinter GUI button hold down

Wed Dec 30, 2015 1:44 pm

RogerW wrote:One way to do this is to create your own type of button.

Code: Select all

import Tkinter as tk

class MyBtn(tk.Button):
    # set function to call when pressed
    def set_down(self,fn):
        self.bind('<Button-1>',fn)
     
    # set function to be called when released
    def set_up(self,fn):
        self.bind('<ButtonRelease-1>',fn)
       
class Mainframe(tk.Frame):
    def __init__(self,master,*args,**kwargs):
        tk.Frame.__init__(self,master,*args,**kwargs)
        
        # create the button and set callback functions
        btn = MyBtn(self,text = 'Press ')
        btn.set_up(self.on_up)
        btn.set_down(self.on_down)
        btn.pack()
   
    # function called when pressed
    def on_down(self,x):
        print("Button down")

    # function called when released
    def on_up(self,x):
        print("Button up")
                
class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
               
        self.title('My Button')
        self.geometry('250x50')
              
        Mainframe(self).pack()
                    
# create and run an App object
App().mainloop()
This program prints a message when the button is pressed or released.
Thank you for you answer RogerW please see my edited question to make clear what I want to do. :)

User avatar
RogerW
Posts: 286
Joined: Sat Dec 20, 2014 12:15 pm
Location: London UK

Re: Python Tkinter GUI button hold down

Wed Dec 30, 2015 2:06 pm

hold_down = root.bind('<ButtonRelease-1>',stop_motor)

is not doing what you expect. The function bind sets another function (stop_motor) when the button is released. It should not be called in a loop.

I suggest you either examine your code and work out where it is not working or use mine as a new starting point. You could start the motor in Mainframe.on_down and stop it in Mainframe.on_up for instance.

Kiri_23
Posts: 15
Joined: Sat Dec 26, 2015 3:19 am

Re: Python Tkinter GUI button hold down

Wed Dec 30, 2015 2:38 pm

RogerW wrote:hold_down = root.bind('<ButtonRelease-1>',stop_motor)

is not doing what you expect. The function bind sets another function (stop_motor) when the button is released. It should not be called in a loop.
http://stackoverflow.com/a/2440643 I try to simulate a do-while for what I found on this answer. I know it should not be called in a loop. The only doubts I have is how I check for the condition button release to exit the loop..I've been triying two days now,that is why I decide to put a question on this forum. Your code would work but it doesn't keep calling repeatedly the function run_motor. It will only call one time and this is not what I desired.

Thank for taking your time to respond my answer.

Return to “Python”