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()
CodePudding user response:
You need to call get()
on the associated variable, not the widget itself.
def printtxt():
x = menu_txt.get()
print(x)