Using a variable to call a function in Python 3 and tkinter

1.7k Views Asked by At

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()
2

There are 2 best solutions below

0
On BEST ANSWER
def afunc():
    foo.set('A')
    tempchoice.set('afuncaction')
    global myfunc
    myfunc = afuncaction # save a reference to this function

...

if messagebox.askyesno:
    myfunc()

By the way, you don't need parentheses after return. It's a statement, not a function. And it's not strictly necessary to put it at the end of every function - if a function reaches the end of its available statements, it will return automatically (returning the value None).

4
On

If you want to call a function using the name of the function as a string, see this question/answer - Calling a function of a module from a string with the function's name in Python

A couple examples:

First looking up the function in the global symbol table -

def foo(): 
  print 'hello world'

globals()['foo']()
# > hello world

Or second, if your function is a method on a class -

class Foo(object): 
  def bar(self): 
    print 'hello world'

foo = Foo()

getattr( foo, 'bar' )()
# > hello world