I have a few different functions that I want to call, but I would like to use a the value of a variable to do it. I'm using buttons in tkinter to change the value of the variable, as well as a button that picks a random variable, and a label to show the current value (I left this out of the code below). I have another button that creates an AskYesNo messagebox to get confirmation from the user that the selected button/variable value picked is correct. If the user picks No, it returns to the root window. If the user picks Yes, I want the program to call the function that was associated with the variable.
I'm a beginner with both Python and tkinter, so please don't assume I know anything about even simple coding. Thanks.
See below for some sample code:
import random
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
root = Tk()
global tempchoice
global foo
tempchoice = StringVar()
item = StringVar()
def afunc():
foo.set('A')
tempchoice.set('afuncaction')
return()
def afuncaction():
print("Function A called")
return
def bfunc():
foo.set('B')
tempchoice.set('bfuncaction')
return()
def bfuncaction():
print("Function B called")
return
def mystery():
item = ['afuncaction', 'bfuncaction']
result = random.choice(item)
foo.set("Mystery")
tempchoice.set(result)
return()
def confirm():
n = messagebox.askyesno("Selected Choice", "Call " + foo.get() + "?")
if n:
tempchoice
return
aButton = Button(root, text="A Function",command=afunc)
aButton.grid(row=0, column=0, sticky=W+E+N+S)
bButton = Button(root, text="B Function",command=bfunc)
bButton.grid(row=1, column=0, sticky=W+E+N+S)
quitButton = Button(root, text="Quit", command=exit)
quitButton.grid(row=7, column=0, sticky=W+E+N+S)
confirmButton = Button(root, text="Confirm", command=confirm)
confirmButton.grid(row=7, column=7)
root.mainloop()