2

I have a problem to detect/check button press in python tkinter ! I have a variable click i want that if my button is clicked then it becomes True for ex: this is my code:

buttonClicked=False
myButton=Button()

I want something like this:

if myButton is pressed:
 buttonClicked=True

Thanks for your help!

2
  • 2
    That's not the way GUIs work. In the case of Tkinter, you respond to a click on a button by specifying a command= option when creating the button, that gives a function that will be called when the click happens. Commented May 6, 2020 at 15:43
  • Please show a Minimal, Reproducible Example. The code you showed here gives an error: NameError: name 'Button' is not defined Commented May 6, 2020 at 15:43

4 Answers 4

6

I am not aware of any internal tkinter method to check if a button is pressed.

However you could connect the Button with a function that changes the value of a global variable, like in the following:

from Tkinter import *

master = Tk()

def callback():
    global buttonClicked
    buttonClicked = not buttonClicked 


buttonClicked  = False # Bfore first click

b = Button(master, text="Smth", command=callback)
b.pack()

mainloop()

The code, changes the variable value from False to True (or reverse) every time you press the button.

Sign up to request clarification or add additional context in comments.

1 Comment

but i want everytime i press the button, the value will changed
3

I think that you could make a function to change the value of buttonClicked, and, when the button is clicked, it executes that function (whose only purpose is to change the value of buttonClicked).

The complete code could go as follows:

from tkinter import *
buttonClicked = False
def changeValue():
    if buttonClicked:
        buttonClicked=False
    if not buttonClicked:
        buttonClicked=True
tk = Tk()
btn = Button(tk, text="Put whatever text you want here, to tell the person what pressing the button will do", command=changeValue())
btn.pack()

If this answer help, I would appreciate you tell me! :).

This is a changed/edited version, with a loop for logic that changes the value of buttonClicked. In the part of code that says "if not buttonClicked:" you could change to an "else:" statement. @

1 Comment

yes ! it would work but the problem i that it will change the value only for the first time not always!
1

Try this:

from tkinter import *
value = 1
def change_value():
       global value
       value -= 1
       if value == 0:
                 print("button pressed")
                 value = 1
       else:
                pass    
tk = Tk()
btn = Button(tk, text="your_text", command=change_value)
btn.pack()

Comments

0

Just put the code you want to run inside a function like this:

def when_clicked():
    #code you want here

button = Button(window,command=when_clicked)

(may be a bit late but oh well)

Comments

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.