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

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

我试图让选项菜单在我当前的项目文件上工作,但我一直收到错误 TypeError: init() 接受 2 到 3 个位置参数,但给出了 9 个 所以我想看看它是否是代码,我制作了一个新的项目文件并将其与我当前拥有的项目的先前版本一起粘贴到那里并且它工作得很好。关于为什么我在当前项目文件中出现错误的任何想法? 这是代码

    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)

基于我从这篇 site 中读到的内容。

tkinter.OptionMenu 接受 2 或 3 个参数。

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

因此,您的第三个参数 "Select pant size" 现在是不必要的,因为它已经是您的第二个参数 PantsClick 因为在第 5 行 PantsClick.set("Select pant size").

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

如果上面的答案还是不行。尝试添加一个新变量 size_options 然后将所有选项放在一个元组中。然后在将该元组作为参数传递时在该元组上使用 *

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)