3

I'm working on a simple Python-Tkinter application. Here's a simplification of the code I'm having now.

from Tkinter import *
root = Tk()

def function(x): 

    if x == "yes":
        a.set("hello")
    else:
        a.set("bye")

#-----------------------------

a = StringVar()
a.set("default")

oc = StringVar(root)
oc.set("Select")
o = OptionMenu(root, oc, "yes", "no", command=function)
o.pack()

z = a.get() 
print z # prints default

root.mainloop()

I want this code to print "hello" or "bye" to the console but instead it prints "default".

I have been trying to make this work but somehow I can't figure it out. The code works fine if I call the function directly instead of using an Optionmenu widget:

z = function("yes")
print z #prints hello

or:

z = function("no") 
print z #prints bye

Could someone explain why it doesn't print "hello" or "bye" when I use the Optionmenu widget. And how do I fix it so I can use the variable z without changing the part above the line?

Thanks!

martineau
  • 119,623
  • 25
  • 170
  • 301
Roundhouse
  • 98
  • 4
  • 12

1 Answers1

5
from Tkinter import *
root = Tk()

a = StringVar()
a.set("default")

oc = StringVar(root)
oc.set("Select")

def function(x):

  if x == "yes":
      a.set("hello")
      print a.get()

  else:
      a.set("bye")
      print a.get()

o = OptionMenu(root, oc, "yes", "no", command=function)
o.pack()


z = a.get()    
print z

root.mainloop()

OptionMenu executes function now when the OptionMenu selection is changed -- if the option menu reads "yes", function executes with "yes" as its parameter and sets a to "hello" and sets a to "bye" for all other options.

JoshieSimmons
  • 2,721
  • 2
  • 14
  • 23
  • Sorry, I edited my original question, the subfunction wasn't necessary after all ... Thanks for the answer! But how can I change z = "bye" into z = "hello" using the Optionmenu widget? – Roundhouse Jun 16 '14 at 13:37
  • Check it out now. All you need to do is set `function` to change according to some condition -- it takes your currently selected option as its input. `z` will always return `default` outside of the scope of the function -- so you `print a.get()` within the scope of the function. – JoshieSimmons Jun 16 '14 at 14:02
  • So it isn't possible to assign the changed value of a (changed inside the function) to z (outside the function)? – Roundhouse Jun 16 '14 at 14:12
  • 1
    You initialize `a` and then set it to `"default"`. You then initialize `z` to `a.get()`. `a.get()` will always return `"default"` unless you change the value of `a`. Your helper function in `OptionMenu` executes when you change a selector in the menu. It will change `a` if the criteria are met. You assign `z` after you define your function and before the end of the loop. It gets printed one time and one time only, and because it is called before you can change the menu (which is what changes your settings for `a`), it will always return `"default"`. – JoshieSimmons Jun 16 '14 at 14:17
  • Ah, Now I see it, I guess I have to redo my code if want to further calculate with the "hello"/"bye" value. Nevertheless thanks for your help. – Roundhouse Jun 16 '14 at 14:33
  • The fact that the `OptionMenu` widget accepts a `command` callback option is very poorly documented (if at all). – martineau Jan 21 '21 at 23:34