Store button command into an variable

Hi there,
is it possible to store a button command into a variable?. I have some txt files and buttons that are generated dynamically. I would like to store the dynamically created text file name into a variable, to use with the button command. Is this possible?
I have a test script, giving no error, but not working…
Any help will be appreciated …

import tkinter as tk
from tkinter import ttk

# root window
root = tk.Tk()
root.geometry('300x200')
root.resizable(False, False)
root.title('Button Demo')

button_var= 'root.quit()'

# exit button
exit_button = ttk.Button(
    root,
    text='Exit',
    command=lambda: button_var
)

print(button_var)

exit_button.pack(
    ipadx=5,
    ipady=5,
    expand=True
)

root.mainloop()

Your code is doing this:

# exit button
exit_button = ttk.Button(
    root,
    text='Exit',
    command=lambda: 'root.quit()'
)

when it should be this:

# exit button
exit_button = ttk.Button(
    root,
    text='Exit',
    command=root.quit
)

You also could have said command=lambda: root.quit(), but as the argument will be called with no arguments root.quit doesn’t have any parameters, command=root.quit will work the same and is shorter.

yes i know, but i am trying to store root.quit in to a variabele, is this possible?

I suppose you could do command=lambda: exec(button_var).

Please don’t encourage that.

variable = root.quit

exit_button = ttk.Button(
    root,
    text='Exit',
    command=variable
)

So today I learned that as well as all the unreproducible Discourse bugs that cause it to accidently change the content of posts made by email (e.g. inserting extra newlines, cutting off your post midway through) Discourse will also deliberately change the content of your posts if it thinks you are quoting too much.

So that’s yet another “quirk” of the platform that I have to remember to work around before posting. Yay.