Skip to content Skip to sidebar Skip to footer

Python: Getting Option From Function?

I'm working with Tkinter in Python and am using OptionMenu and want to get the selection the user makes. ex1 = StringVar(root) ex1.set('Pick Option') box = OptionMenu(root, 'one','

Solution 1:

This works, but not sure it is what you wanted.

from Tkinter import StringVar
from Tkinter import OptionMenu
from Tkinter import Tk
from Tkinter import Button
from Tkinter import mainloop

root = Tk()
ex1 = StringVar(root)
ex1.set("Pick Option")
option = OptionMenu(root, ex1, "one", "two", "three")
option.pack()


defchoice():
    chosen = ex1.get()
    print'chosen {}'.format(chosen)
    # set and hold using StringVar
    ex1.set(chosen)
    root.quit()
    # return chosen


button = Button(root, text="Please choose", command=choice)
button.pack()
mainloop()
# acess the value from StringVar ex1.getprint'The final chosen value {}'.format(ex1.get())

Solution 2:

Add global statement inside the method:

defchoice(self,option):
   global foo
   foo = option

Post a Comment for "Python: Getting Option From Function?"