Python TypeError:jeu()正好接受1个参数(给定0)
我正在尝试创建一个小游戏,但我遇到了一个错误 “TypeError:jeu()正好接受1个参数(给定0)” 我真的不知道代码是否正确。我是Python TypeError:jeu()正好接受1个参数(给定0),python,tkinter,Python,Tkinter,我正在尝试创建一个小游戏,但我遇到了一个错误 “TypeError:jeu()正好接受1个参数(给定0)” 我真的不知道代码是否正确。我是python和tkinter def jeu(arg): root2 = Tk() root2.title("Binary Game") root2.geometry("500x350+50+50") root2.resizable(width=False, height=False) root2['bg'] = 'black' ###
python
和tkinter
def jeu(arg):
root2 = Tk()
root2.title("Binary Game")
root2.geometry("500x350+50+50")
root2.resizable(width=False, height=False)
root2['bg'] = 'black'
#####
menu = Menu(root2)
root2.config(menu=menu)
subFichier=Menu(menu)
menu.add_cascade(label="Fichier", menu=subFichier)
subFichier.add_command(label="Nouvelle partie")
subFichier.add_separator()
subFichier.add_command(label="Quitter", command=root2.quit)
#####
difchoisie = Label(root2, pady=30, text="Donnez la valeur décimale
de ce nombre : ", font=("Courier New", 18), bg="black",
fg="green").pack()
nbdisp = Label(root2, text=nb, font=("Courier New", 20),
bg="black",
fg="green").pack()
entrynbdec = Entry(root2, width=5, font=("Courier New", 20),
justify=CENTER).pack(side=TOP, pady=30)
boutonvalid = Button(root2, text="Valider",
highlightbackground="black").pack()
root2.mainloop()
root = Tk()
root.title("Binary Game")
root.geometry("500x350+50+50")
root.resizable(width=False, height=False)
root['bg'] = 'black'
#####
menu = Menu(root)
root.config(menu=menu)
subFichier=Menu(menu)
menu.add_cascade(label="Fichier", menu=subFichier)
subFichier.add_command(label="Nouvelle partie")
subFichier.add_separator()
subFichier.add_command(label="Quitter", command=root.quit)
#####
bienvenue = Label(root, pady=30, text="Bienvenue sur Binary Game !",
font =("Courier New", 24), bg="black", fg="green").pack()
choixdif = Label(root, pady=25, text="Veuillez choisir la .
difficulté.", font =("Courier New", 18), bg="black",
fg="green").pack()
boutondif1 = Button(root, text="Facile", highlightbackground
="black", command=jeu).pack()
boutondif2 = Button(root, text="Moyenne", highlightbackground
="black", command=root.destroy and jeu).pack()
root.mainloop()
根据您的函数定义,
jeu()
当前假定接受一个参数arg
def jeu(arg):
但是,在所有函数定义中,都没有使用任何传递的参数,也没有向其传递任何参数,这就是为什么会出现自解释错误的原因
TypeError:jeu()正好接受1个参数(给定0)
其中,“正好1个参数”指函数定义中定义的arg
,def-jeu(arg):
因此,只需使用不带任何参数的函数定义即可
def jeu():
要扩展Bazingaa的答案,函数
jue()
不需要任何参数,因为绑定到tkinter
按钮的函数不传递事件(与键盘绑定不同)
但是,如果确实希望将值传递给jue
,则需要使用类似functools
库的功能,该库具有部分
功能
例如:
import functools
def jue(arg):
#code here
#rest of code
boutondif1 = Button(root, text="Facile", highlightbackground
="black", command=functools.partial(jeu, some_args)).pack() #replace some_args with the value(s) you would pass to the function
boutondif2 = Button(root, text="Moyenne", highlightbackground
="black", command=functools.partial(jeu, some_args)).pack()
请注意,如果要在单击boutondif2
时使用root.destroy()
,则需要在jue()
中包含root.destroy()
。可以通过向函数中添加一个参数来实现这一点,如果设置为True
,该参数将调用root.destroy()
,如下所示:
def jue(arg, do_destroy):
if do_destroy:
root.destroy()
#rest of code here
谢谢你的回答,我尝试了你给我的“do_destroy”,当我点击按钮时,当函数jeu启动时,它会破坏第一个窗口,但不起作用,它说TypeError:jeu()在我删除(arg,do_destroy)时正好接受2个参数(0给定),所以只有def jeu():if do_destroy:root.destroy()它只是说没有定义全局名称dou_destroy