如何将(Answer)变量从creatLabels函数传递给check函数?
def createLabels(Ques):
AnswerINP = Entry(root)
AnswerINP.grid(column=3, sticky=W )
Answer = AnswerINP.get()
Checkbutton = tk.Button(root, text="CHECK", command= check)
Checkbutton.grid()
def check ():
r=requests.get('https://opentdb.com/api.phpamount='+TextINP1.get()+'&difficul)
res = r.json ()
for data in res ['results']:
if Answer.title()== data["correct_answer"]:
print("Correct")当我以这种方式编写变量时,我得到的结果是变量答案没有定义
发布于 2019-05-26 21:39:20
您可以使用Lambda Expression将变量传递给检查函数。Lambda表达式基本上是一行匿名(未命名)函数。
为此,您只需将您的Checkbuttons命令选项更改为λ表达式,并将参数添加到您的check(),函数如下:
def createLabels(Ques):
AnswerINP = Entry(root)
AnswerINP.grid(column=3, sticky=W )
Answer = AnswerINP.get()
Checkbutton = tk.Button(root, text="CHECK", command=lambda a=Answer: check(a))
Checkbutton.grid()
def check (answer):
...这应该会给你想要的结果。
https://stackoverflow.com/questions/56309975
复制相似问题