【发布时间】:2017-02-28 12:41:10
【问题描述】:
我是python新手,正在练习,我现在已经写了这段代码,但是我遇到了一个错误,我不知道如何解决,有人可以帮我吗?
这是我的代码:
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()
当我点击提交时,我得到了这个错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\error\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
TypeError: printInput() missing 1 required positional argument: 'name'
我做错了什么?
谢谢!
【问题讨论】:
-
在
print("Your name is %s, and you are %s years old." % name)中有两个%s占位符,但您只提供一个数据。你需要print("Your name is %s, and you are %s years old." % name, something_else_here) -
@roganjosh 哦,谢谢,我现在修好了,但我得到了同样的错误......虽然只有当我点击提交时才会出现错误
-
说“我现在修复了它,但得到了同样的错误”显然意味着你没有正确修复它。您应该更具体并编辑您的问题以反映您所谓的“修复”和确切错误消息。
-
我要用 tkinter 标签来标记它。我可以让它工作,但它只在我最后关闭窗口时打印消息。自从我用 tkinter 做任何事情以来已经有一段时间了,所以我不记得正确的方法了。
-
@l'L'l 使用新代码编辑问题