0

I am try to use a OptionMenu in my GUI, I am unable to get the value of selected item in OptionMenu.I am unable to write the function properly.

Following is my code.

from tkinter import *
root = Tk()

variable = StringVar(root)
options = {"one": 1, "two": 2, "three": 3}

O_menu = OptionMenu(root, variable, *options.keys()).pack()

def sample():
    #here i want to write a function so that when I select "one", the result should print 1.
    pass


bu = Button(root, text="print", command=sample).pack()

root.mainloop()

Additionally I want OptionMenu with default value of one to be selected when I start the GUI.

Zeryab Hassan Kiani
  • 467
  • 1
  • 7
  • 18
  • Look at [this example](https://stackoverflow.com/questions/64521595/how-to-get-selected-value-from-the-tkinter-optionmenu). – Rory Aug 09 '22 at 13:05
  • FYI, you said `combobox` in your question but you are using an `optionmenu` which is different. – Rory Aug 09 '22 at 13:06
  • you have common mistake - `O_menu = OptionMenu().pack()` assigns `None` to `variable` because `pack()`/`grid()`/`place()` always give `None`. If you need to use `O_menu` in other palce then you should do it in two steps `O_menu = OptionMenu()` and later `O_menu.pack()` – furas Aug 09 '22 at 13:19
  • how about `print( variable.get() )` ? It gives selected text. – furas Aug 09 '22 at 13:22

2 Answers2

3

If you use variable in OptionMenu then you can use

  • variable.get() to get value selected in OptionMenu
  • variable.set("one") to set value in OptionMenu

Minimal working code with other changes

import tkinter as tk  # PEP8: `import *` is not preferred

# --- functions ---  # PEP8: all functions before main code

def sample():
    selected = variable.get()
    print(selected, options[selected])

# --- main ---

options = {"one": 1, "two": 2, "three": 3}

root = tk.Tk()

variable = tk.StringVar(root)
variable.set("one")

o_menu = tk.OptionMenu(root, variable, *options.keys())
o_menu.pack()

bu = tk.Button(root, text="print", command=sample)
bu.pack()

root.mainloop()

PEP 8 -- Style Guide for Python Code

furas
  • 134,197
  • 12
  • 106
  • 148
  • It should be no parameter variable = StringVar()? – toyota Supra Aug 09 '22 at 19:06
  • 1
    @toyotaSupra I have `variable = tk.StringVar(root)`. First: I use preferred `import tkinter as tk` so I have to use `tk.` before `StringVar`. Second: I could skip `root` from `StringVar(root)` (like I could skip `root` also from `Button(root, ...)` and `OptionMenu(root, ...)`) and it would use `root` as default - but sometimes it can make problem when you will have other windows (`Toplevel`) - so I prefer to do it explicitly in all situations. – furas Aug 09 '22 at 19:30
2

Here is the solution:

from tkinter import *
root = Tk()

variable = StringVar(root)
options = {"one": 1, "two": 2, "three": 3}
variable.set("one")

O_menu = OptionMenu(root, variable, *options.keys()).pack()


def sample():
    result = variable.get()
    print(options[result])

bu = Button(root, text="print", command=sample).pack()

root.mainloop()
Mujtaba Mateen
  • 202
  • 2
  • 7