3

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()
DeZeeuw2
  • 33
  • 4
  • By "the function that was associated with the variable," do you mean the function that was responsible for the variable's current value, e.g. `afunc()` if it's `'A'`? – TigerhawkT3 May 30 '15 at 03:42
  • If the user hits the A Button, afunc is called, which sets the tempchoice to afuncaction. afuncaction is only called if the Confirm button is pressed and then the user picks Yes. – DeZeeuw2 May 30 '15 at 03:53

2 Answers2

1

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
Community
  • 1
  • 1
metahamza
  • 1,405
  • 10
  • 26
  • So if I use one of those methods within my Confirm function, it will call whichever function is currently set as the value of tempchoice? – DeZeeuw2 May 30 '15 at 04:10
  • You have `tempchoice` as an instance of a class, which I'm assuming has a member string where you are storing the function name. You'll have to get that value back out and use is as the key to lookup the function out of the symbol table as in my first example. – metahamza May 30 '15 at 04:19
  • I wouldn't recommend `globals()` as a first resort. – TigerhawkT3 May 30 '15 at 04:38
  • @TigerhawkT3 agreed. It would be better to wrap the provided code in a class – metahamza May 30 '15 at 04:42
0
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).

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97