TypeError:jeu()恰好接受1个参数(给定0)

时间:2018-12-30 23:56:38

标签: python tkinter

我正在尝试创建一个小游戏,但我遇到了错误。

  

“ TypeError:jeu()恰好接受1个参数(给定0)”

我真的不知道代码是否正确。我是pythontkinter

的初学者
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()

2 个答案:

答案 0 :(得分:4)

根据您的函数定义,目前假设jeu()接受一个参数arg

def jeu(arg):

但是,在所有函数定义中,您无处使用任何传递的参数,也没有向其传递任何参数,这就是为什么会出现自解释错误的原因

  

TypeError:jeu()恰好接受1个参数(给定0个参数)

其中“恰好是1个参数” 指的是函数定义 {arg

中定义的def jeu(arg):

因此,只需使用不带任何参数的函数定义作为

def jeu():

答案 1 :(得分:0)

要扩展Bazingaa的答案,函数jue()不需要任何参数,因为绑定到tkinter按钮的函数不会传递事件(与键盘绑定不同)。

但是,如果您确实想将值传递给jue,则需要使用类似functools库的功能,该库具有partial功能。

例如:

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()

请注意,如果要在单击root.destroy()时使用boutondif2,则需要在root.destroy()中包含jue()。您可以通过在函数中添加一个参数来实现此目的,如果将其设置为True,则会调用root.destroy(),如下所示:

def jue(arg, do_destroy):
    if do_destroy:
        root.destroy()

    #rest of code here
相关问题