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.
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.
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. @
tkinter - Checking if a button has been pressed in python? - Stack Overflow
On python, how do I determine which button was clicked - Stack Overflow
python - how to check if button is clicked on tkinter - Stack Overflow
How do you say "If button is pressed, then do this" in python tkinter? - Stack Overflow
Videos
It's simple, define a function which will be called after button press. Like so:
def addCredit():
global credit
credit+=10
And then assign this simple function to your button:
tenbutton = Button(insert, text="10p", command=addCredit).grid(row=2, column=1)
By the way, your code is badly asking for a class somewhere. Using so many globals is generally a bad practice. Another nitpick is from tkinter import *, it destroys readability. I'd suggest import tkinter as tk.
You can add a command to your Tkinter Button widget that will callback a function:
def tenbuttonCallback():
global credit
credit += 10
tenbutton = Button(insert, text="10p", command=tenbuttonCallback)
tenbutton.grid(row=2, column=1)
See: http://effbot.org/tkinterbook/button.htm
You could set each button's command option to a lambda like this:
self.button1 = Tkinter.Button(self, ..., command=lambda: self.OnButtonClick(1))
...
self.button2 = Tkinter.Button(self, ..., command=lambda: self.OnButtonClick(2))
Then, make self.OnButtonClick accept an argument that will be the button's "id". It would be something like this:
def OnButtonClick(self, button_id):
if button_id == 1:
# self.button1 was clicked; do something
elif button_id == 2:
# self.button2 was clicked; do something
An object-oriented way to do this is to just pass the button clicked to theOnButtonClick()method:
def OnButtonClick(self, button):
# do stuff with button passed...
...
In order to do this requires creating and configuring each button in two steps. That's necessary because you need to pass the button as an argument to the command which can't be done in the same statement that creates the button itself:
button1 = Tkinter.Button(self, text=u"Convert Decimal to Binary")
button1.config(command=lambda button=button1: self.OnButtonClick(button))
button2 = Tkinter.Button(self, text=u"Convert Binary to Decimal")
button2.config(command=lambda button=button2: self.OnButtonClick(button))
Use a Boolean flag.
Define isClicked as False near the beginning of your code, and then set isClicked as True in your create_window() function.
This way, other functions and variables in your code can see whether the button's been clicked (if isClicked).
Not sure what you asked, do you want to disable it or check its status in another routine ? Or just to count the times it has been clicked,
In order to do that Simple solution would be to add a general variable that will be updated inside the create_window method (general because you want to allow access from other places).
Depending on how your circuit is wired you should use
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
inputPin = "YOUR_PIN_GOES_HERE"
GPIO.setup(inputPin,GPIO.IN,pull_up_down=GPIO.PUD_UP)
def buttonPress(channel):
#stuff
def buttonRelease(channel):
#stuff
GPIO.add_event_detect(channel,GPIO.RISING,callback=buttonPress)
#OR
GPIO.add_event_detect(channel,GPIO.FALLING,callback=buttonRelease)
while(1):
keypressed = raw_input('Press q to quit: ')
if keypressed == 'q':
break
elif keypressed == 'SOME OTHER KEY':
#code for some other thing
else
print("Unknown input")
These are events that will be trigged where there is a change on your input pin. Your raw_input can still handle whatver code you want because the event loop is running in the background.
Pulled from here
Edits: put the event defs above event declaration because I am teh dumb
While(1)'s are ugly. For the sake of staying a loop this should suffice though. Essentially this will run the keyboard input loop until you hit q. Use the elifs to handle other inputs. The else will capture unknown input and provide feedback. The break statment will boot the program out of the while loop and will reach the end of code and terminate.
Should you feel fancy and want to handle a lot of input I would look to use what other languages have a switch statement to clean up a bunch of elifs
Oh and standard warranty applies, I may have missed a tab or : here and there.
Your Python module will offer GPIO callbacks. Generally you specify
- a GPIO
- whether you are interested in rising edges (0->1), falling edges (1->0), or both edges
- and a function to be called when the event happens
The specified function will be called asynchronously to the main thread, i.e. it will still be called even if the main thread is waiting in a raw_input.
Generally you would use global variables to pass state information between the callback and main thread.