Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

python - tkinter TypeError: missing 1 required positional argument:

I′m new to python and practicing, and I have now written this piece of coding, but I′m getting an error I do not know how to resolve, could someone help me please?

This is my code:

from tkinter import *

root = Tk()

name = 'donut'

def printInput(event, name):
    print("Your name is %s, and you are years old." % (name))

button_1 = Button(root, text="Submit")
button_1.bind("<Button-1>", printInput)
button_1.pack()

root.mainloop()

And when I click on submit, I get this error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:UserserrorAppDataLocalProgramsPythonPython36-32libkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
TypeError: printInput() missing 1 required positional argument: 'name'

What am I doing wrong?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have the function printInput which has two parameters, but when it is called, tkinter only passes it one argument, i.e. the event.

One way to pass the name argument as well is to use a lambda function:

button_1.bind("<Button-1>", lambda event:printInput(event,name))

This will pass the global name variable to the function as an argument.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...