5

I'm trying to print out the value selected in the option menu but only the first value gets printed everytime I run the code, even if I change my selection to b or c. Not sure where I'm wrong.This is my code:

from tkinter import *
window=Tk()
window.geometry("700x400")

options=StringVar(window)
options.set("a")
menu=OptionMenu(window,options, "a","b","c")
menu.grid(row=2,column=2)
selection=options.get()
print(selection)

4 Answers 4

5

Instead of tracing the variable, you can use the command option of the OptionMenu. Each time a menu item is clicked, the command is called. This command takes one argument: the item which is selected.

import tkinter as tk

def callback(selection):
    print(selection)

root = tk.Tk()
options = tk.StringVar()
menu = tk.OptionMenu(root, options, 'a', 'b', 'c', command=callback)
menu.pack()
options.set('a')
root.mainloop()

In this case, the initially selected item is not printed because the user have not clicked on it. If you need options.set('a') to trigger your callback, then you will have to trace the variable like in mentalita's answer.

Sign up to request clarification or add additional context in comments.

Comments

0

First of all, you need to invoke Tk's mainloop at the end of your code. Also, try tracing the options class variable.

From the docs: You can use the trace method to attach “observer” callbacks to the variable. The callback is called whenever the contents change.

import tkinter as tk

root = tk.Tk()
options = tk.StringVar()
options.trace_add('write', lambda *args: print(options.get()))
menu = tk.OptionMenu(root, options, 'a', 'b', 'c')
menu.pack()
options.set('a')
root.mainloop()

5 Comments

Okay. Whats "w" stand for? I'm still new to python so getting trouble on a lot of stuff.
@West: it stands for 'write'. I've updated my answer to include a short explanation from the docs.
Thanks. Is there a way to get the value selected and put it into a variable so I can use it anywhere?
I don't know which version of python you are using, but in python 3.6 trace is deprecated and options.trace_add('write', lambda *args: print(options.get())) should be used instead.
@j_4321: TIL. Thanks for the heads up.
0

You're assigning selection before the user has the chance to change the selected option.

Putting it in a function that calls when a 'save' button is pressed would likely be the simplest fix. You could also use a recursion loop to update it in real time but it's not a clean solution.

I'm assuming you've already included root.mainloop() in your actual script.

Comments

-1

try this

import tkinter
window=tkinter.Tk()
window.geometry("700x400")

option_Menu = tkinter.StringVar(window)
options = ("a","b","c")
menu = tkinter.OptionMenu(window,option_Menu,*options)
menu.grid(row=2,column=2)
option_Menu.set(2)
selection=option_Menu.get()

print(selection)

tkinter.mainloop()

1 Comment

While this code may provide a solution to OP's problem, it is highly recommended that you provide additional context regarding why and/or how this code answers the question. Code only answers typically become useless in the long-run because future viewers experiencing similar problems cannot understand the reasoning behind the solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.