pycharmtypeerroroptionmenutkinter.optionmenu

"TypeError: __init__() takes from 2 to 3 positional arguments but 9 were given" but i tested in a new project file and worked fine


I'm trying to get the optionmenu to work on my current project file but i keep getting the error TypeError: init() takes from 2 to 3 positional arguments but 9 were given so i wanted to see if it was the code, and i made a new project file and pasted it in there with a previous revision of the project that i currently have and it worked perfectly fine. Any ideas as to why i get the error on my current project file? heres the code

    def Pantsize():
       # Drop box for pant size
       # Figure out how to label for pant sizes
       PantsClick = StringVar()
       PantsClick.set("Select pant size")
       PantsDrop = OptionMenu(AppWindow, PantsClick, "Select pant size", "XS", "S", "M", "L", "XL")
       PantsDrop.place(relx=0.3, rely=0.25)

Solution

  • Based on what I've read from this site.

    tkinter.OptionMenu takes 2 or 3 parameters.

    tk.OptionMenu(parent_frame, variable, choice1, choice2, ...)
    

    Therefore, your third parameter "Select pant size" is now unnecessary since that is already your second parameter PantsClick because on line 5 PantsClick.set("Select pant size").

    PantsDrop = OptionMenu(AppWindow, PantsClick, "XS", "S", "M", "L", "XL")
    

    If the answer above still doesn't work. Try to add a new variable size_options then place all of the choices inside a tuple. Then use * on that tuple when passing it as an argument.

    def Pantsize():
       # Drop box for pant size
       # Figure out how to label for pant sizes
       PantsClick = StringVar()
       PantsClick.set("Select pant size")
       size_options = ( "XS", "S", "M", "L", "XL" )
       PantsDrop = OptionMenu(AppWindow, PantsClick, *size_options)
       PantsDrop.place(relx=0.3, rely=0.25)