-1

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.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
SG-Lars
  • 3
  • 1
  • 1
    The natural way to do this is to have `Button_1` call `Button_2`. The ugly way is to use a global variable and a check loop. You seem to have a [XY Problem]( https://en.wikipedia.org/wiki/XY_problem) -- you'll need to explain these artificial restrictions. – Prune May 28 '21 at 14:18
  • You've already got global variables in the form of the `btn_X` variables. You could use those. – quamrana May 28 '21 at 14:19
  • @Prune XY explained: User will input number in input field Button1 will run a SQL SELECT and dispaly the values to the user (Verification). Button2 will run a SQL UPDATE command, but I want to force the user to press Button1 first, so that he verifies that the displayed information is indeed the desired data he want's to change. – SG-Lars May 28 '21 at 14:28

1 Answers1

0

How to pass variable from one function to another (button pressed)

To get returned value from the callback you should use invoke() method of the Button.

BUT it's just returned value, and unfortunately you cannot use it as a flag, as it doesn't track was the button pressed or not.

So the trick is to create your custom button with a flag which will be changed to True by callback and pass the flag as an argument to the button_2, to do so you can use lambda or partial func.

I hope it's fine for you, despite of it uses the vars out of function scope.

Updated code:

import tkinter as tk
from sys import exit

window = tk.Tk()


class MyButton(tk.Button):
    pressed = False


def button_1():
    print("Button 1 pressed")
    button_1_pressed = "Yes"
    btn_1.pressed = True
    return button_1_pressed


btn_1 = MyButton(master=window, text="Button 1", width=10, height=2, command=button_1)
btn_1.pack()


def button_2(state):
    if state:
        print("Button 1 was pressed, taking action")
        print('gotcha!')
    else:
        print("Button 1 was NOT pressed, no action is done")


btn_2 = tk.Button(master=window, text="Button 2", width=10, height=2, command=lambda: button_2(btn_1.pressed))
btn_2.pack()

btn_Close = tk.Button(master=window, text="Close", width=10, height=2, command=exit)
btn_Close.pack()

window.mainloop()

Useful links:

https://www.tutorialspoint.com/python/tk_button.htm

How to pass arguments to a Button command in Tkinter?

  • Thanks @viacheslav-khvorostianyi it works perfect! Can I ask you to explain the "state" part? I did not quite understand it, and I want to understand every line of code in the programs I write. Maybe provide a link to a useful article? – SG-Lars Jun 03 '21 at 13:45
  • Glad to be helpful) `state` is just an argument of a callback function, you can named whatever you want, the thing is - if you want to use callbacks with arguments you should use a wrapper for the function. The simplest wrapper is `lambda`. So basically - that's it) – Viacheslav Khvorostianyi Jun 04 '21 at 08:52