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)
West
  • 2,350
  • 5
  • 31
  • 67

4 Answers4

4

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.

j_4321
  • 15,431
  • 3
  • 34
  • 61
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()
adder
  • 3,512
  • 1
  • 16
  • 28
  • Okay. Whats "w" stand for? I'm still new to python so getting trouble on a lot of stuff. – West Oct 19 '17 at 14:16
  • @West: it stands for 'write'. I've updated my answer to include a short explanation from the docs. – adder Oct 19 '17 at 14:22
  • Thanks. Is there a way to get the value selected and put it into a variable so I can use it anywhere? – West Oct 19 '17 at 14:34
  • I don't know which version of python you are using, but in python 3.6 `trace` is [deprecated](https://docs.python.org/3/whatsnew/3.6.html) and `options.trace_add('write', lambda *args: print(options.get()))` should be used instead. – j_4321 Oct 19 '17 at 14:35
  • @j_4321: TIL. Thanks for the heads up. – adder Oct 19 '17 at 14:38
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.

Matt
  • 225
  • 2
  • 9
-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()
  • 2
    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. – E. Zeytinci Jan 14 '20 at 19:50