I want to make Button2 do something, but only if Button1 has been pressed first and it's code was successfully run. Also: Button2 should not execute button_1's function, and I do not want to use global variables.
The code below was my initial thought, and it did not work. I've searched for the right way, but neither of the methods I found gave me the result I hoped for.
Can anyone tell me how to correct this? I'm also open for easier or better ways to achieve my goal.
import tkinter as tk
from sys import exit
window = tk.Tk()
def button_1():
print("Button 1 pressed")
button_1_pressed = "Yes"
return button_1_pressed
def button_2():
if (button_1_pressed == "Yes"):
print("Button 1 was pressed, taking action")
# Action goes here
else :
print("Button 1 was NOT pressed, no action is done")
btn_1 = tk.Button(master=window, text="Button 1", width=10, height=2, command=button_1)
btn_1.pack()
btn_2 = tk.Button(master=window, text="Button 2", width=10, height=2, command=button_2)
btn_2.pack()
btn_Close = tk.Button(master=window, text="Close", width=10, height=2, command=exit)
btn_Close.pack()
window.mainloop()
Note: This is just a quick and simple script, illustrating exactly what I need help with in all simplicity.