I want to perform a task if a button is clicked. How do I do this? My code is:
from tkinter import *
root = Tk()
def hey():
if bt1 is clicked:
#do something
if bt2 is clicked:
#do something
#some piece of code
bt1 = Button(root, text = 'yes', command = hey)
bt2 = Button(root, text = 'no', command = hey)
bt1.pack()
bt2.pack()
Based on what you have shared, you can use a parameter to pass a flag in the buttons, e.g.
bt1 = Button(root, text = 'yes', command =lambda: hey(True))
bt2 = Button(root, text = 'no', command =lambda: hey(False))
And in the code, you can do,
def hey(which): # `which` decides which statement to be followed
if which:
#do something
else:
#do something