Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I can't get the input from OptionMenu in tkinter

I made a simple code, where the user selects an option from the OptionMenu, then this option gets print, when I use .get() I get the error
AttributeError: 'OptionMenu' object has no attribute 'get'

Full Code:

from tkinter import *

root = Tk()

def printtxt():
    x = menu.get()
    print(x)

menu_txt = StringVar(root)
menu_txt.set('Text')

texts = ['Blue', 'Red', 'Green', 'Yellow']

menu = OptionMenu(root, menu_txt, *texts)
menu.pack()

bttn = Button(root, text='Submit', command=printtxt)
bttn.pack()

root.mainloop()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to call get() on the associated variable, not the widget itself.

def printtxt():
    x = menu_txt.get()
    print(x)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading