0

Hi im trying to add options to an option menu depending on how many options the user wants, I haven't included the user input part because it isn't necessary in solving the problem. I want all the options in the option menu to call the class optionshow but for some reason i cant get it working please help. Here's the code, thanks for any help in advance.

import tkinter as tk
root = tk.Tk()
root.geometry('1000x600')

class optionshow:
    def __init__(self,p):
        self.p = p.get()
        print(self.p)

option = tk.StringVar()
option.set('Select')
optionmenu = tk.OptionMenu(root, option, 'Select', command=lambda: optionshow(option))
optionmenu.place(x=350, y=20)

choices = ('12345')
for choice in choices:
    optionmenu['menu'].add_command(label=choice, command=tk._setit(option, choice))

root.mainloop()
coderoftheday
  • 1,987
  • 4
  • 7
  • 21

1 Answers1

1

You instantiate the class only for the 'Entry' option (and not correctly). Why don't you take a different approach and add all the options at once while creating the menu:

import tkinter as tk
root = tk.Tk()
root.geometry('1000x600')

class optionshow():
    def __init__(self,p):
        self.p = p.get()
        print(self.p)

option = tk.StringVar(root)
option.set('Select')
choices = ('12345')
optionmenu = tk.OptionMenu(root, option, 'Select', *choices, command=lambda x: optionshow(option))
optionmenu.place(x=350, y=20)

root.mainloop()

Note the necessary correction in the command=lambda part.

Ronald
  • 2,930
  • 2
  • 7
  • 18
  • thankyou very much, this is what i was trying to do, what does the 'x' in command=lambda x: do – coderoftheday Jul 03 '20 at 15:06
  • It can be any variable name. If you leave it out, you will get an error message that lambda takes no arguments. However, you specify option as a parameter, so you need to give lambda an argument to take that parameter. – Ronald Jul 03 '20 at 15:12