首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过"KeyRelease“函数中的lambda传递更改的值?

如何通过"KeyRelease“函数中的lambda传递更改的值?
EN

Stack Overflow用户
提问于 2021-09-16 20:57:29
回答 1查看 61关注 0票数 0

每次用户输入正确的答案时,我都想更改分数标签,但是当我输入正确的答案时,分数会增加到10,但是生成的下一个方程的预期答案不会改变,并且保持在它开始时的初始值,因为"check“函数接收前面分配的"ans”变量。

我怎样才能解决这个问题?

代码语言:javascript
复制
def check(ans, text:tkinter.Text,score,current_stage,q_num,eq, event=None):
    
    ans = calc(eq)

    print(f"real answear = {ans}")
    temp = str(text.get("0.0", "end").strip())
    print(f"my answear = {temp}")

    if str(text.get("0.0", "end").strip()) == str(ans):
         is_correct = True       
    else:
         is_correct = False

    if is_correct:
        ans = calc(eq)
        score += 10
        q_num += 1
        inputtxt.delete(1.0,END)

        eq = generate_equation(stage=current_stage)
        ans = calc(eq)
        score_label_new    = tkinter.Label(master=master, text=f"Score: {score}").grid(row=0, column=1, sticky=W)
        question_label_new = tkinter.Label(master=master, text=f"{q_num}: {eq}").grid(row=0, column=0, sticky=W)
        stage_label_new    = tkinter.Label(master = master,text=f"Stage: {current_stage}").grid(row=0, column= 3,sticky=W)

        if score % 100 == 0:
            current_stage += 1

    else:
        inputtxt.grid()
    

master = tkinter.Tk()
master.title("Math")
master.geometry('400x400')

eq  = generate_equation(stage=current_stage)
ans = calc(eq)

score_label    = tkinter.Label(master=master, text=f"score: {score}").grid(row=0, column=1, sticky=W)
question_label = tkinter.Label(master=master, text=f"{q_num}: {eq}").grid(row=0, column=0, sticky=W)
stage_label    = tkinter.Label(master = master,text=f"Stage: {current_stage}").grid(row=0, column= 3,sticky=W)

inputtxt = tkinter.Text(master=master, height=5, width=20)

inputtxt.bind("<KeyRelease>", 
lambda
 ans=ans,
 text=inputtxt,
 score = score,
 current_stage = current_stage,
 q_num=q_num,
 eq = eq: 
 #linked ==>
check(ans, text,score,current_stage,q_num,eq))


inputtxt.grid()

master.mainloop()
EN

回答 1

Stack Overflow用户

发布于 2021-09-16 21:36:32

这能做你想做的。您不希望在您的lambda中捕获任何这些全局值;您总是希望引用这些全局元素本身。最好将您的状态信息存储在某种状态类中,这样您就不会真正使用globals了,但这是可行的。请注意,我必须伪造您的calcgenerate_equation函数,因为您没有给出一个完整的、可运行的示例:

代码语言:javascript
复制
import tkinter 
from tkinter import W,END
import random

def calc(eq):
    return int(eq)

def generate_equation(stage=0):
    return str(random.randint(0,100))

current_stage = 1
score = 0
q_num = 1

def check(text:tkinter.Text, event=None):
    global eq 
    global score
    global q_num
    global current_stage
    ans = calc(eq)

    print(f"real answear = {ans}")
    temp = str(text.get("0.0", "end").strip())
    print(f"my answear = {temp}")

    if str(text.get("0.0", "end").strip()) == str(ans):
         is_correct = True       
    else:
         is_correct = False

    if is_correct:
        score += 10
        q_num += 1
        inputtxt.delete(1.0,END)

        eq = generate_equation(stage=current_stage)
        score_label_new    = tkinter.Label(master=master, text=f"Score: {score}").grid(row=0, column=1, sticky=W)
        question_label_new = tkinter.Label(master=master, text=f"{q_num}: {eq}").grid(row=0, column=0, sticky=W)
        stage_label_new    = tkinter.Label(master = master,text=f"Stage: {current_stage}").grid(row=0, column= 3,sticky=W)

        if score % 100 == 0:
            current_stage += 1

    else:
        inputtxt.grid()
    

master = tkinter.Tk()
master.title("Math")
master.geometry('400x400')

eq  = generate_equation(stage=current_stage)
ans = calc(eq)

score_label    = tkinter.Label(master=master, text=f"score: {score}").grid(row=0, column=1, sticky=W)
question_label = tkinter.Label(master=master, text=f"{q_num}: {eq}").grid(row=0, column=0, sticky=W)
stage_label    = tkinter.Label(master = master,text=f"Stage: {current_stage}").grid(row=0, column= 3,sticky=W)

inputtxt = tkinter.Text(master=master, height=5, width=20)

inputtxt.bind("<KeyRelease>", lambda ev: check(inputtxt,ev))

inputtxt.grid()

master.mainloop()

这里有一个使用简单类的版本。我还将其更改为更新三个标签中的文本,而不是每次创建新控件。

代码语言:javascript
复制
import tkinter 
from tkinter import W,END
import random

def calc(eq):
    return int(eq)

def generate_equation(stage=0):
    return str(random.randint(0,100))

class State:
    current_stage = 1
    score = 0
    q_num = 1
    eq  = generate_equation(stage=current_stage)

    def check(self, text:tkinter.Text, event=None):
        ans = calc(self.eq)

        print(f"real answer = {ans}")
        temp = text.get("0.0", "end").strip()
        print(f"my answer = {temp}")

        is_correct = text.get("0.0", "end").strip() == str(ans)

        if is_correct:
            self.score += 10
            self.q_num += 1
            inputtxt.delete(1.0,END)

            self.eq = generate_equation(stage=self.current_stage)
            score_label.configure(text=f"Score: {state.score}")
            question_label.configure(text=f"{state.q_num}: {state.eq}")
            stage_label.configure(text=f"Stage: {state.current_stage}")

            if self.score % 100 == 0:
                self.current_stage += 1

        else:
            inputtxt.grid()

state = State()

master = tkinter.Tk()
master.title("Math")
master.geometry('400x400')

score_label    = tkinter.Label(master=master, text=f"score: {state.score}")
score_label.grid(row=0, column=1, sticky=W)
question_label = tkinter.Label(master=master, text=f"{state.q_num}: {state.eq}")
question_label.grid(row=0, column=0, sticky=W)
stage_label    = tkinter.Label(master = master,text=f"Stage: {state.current_stage}")
stage_label.grid(row=0, column= 3,sticky=W)

inputtxt = tkinter.Text(master=master, height=5, width=20)

inputtxt.bind("<KeyRelease>", lambda ev: state.check(inputtxt,ev))

inputtxt.grid()

master.mainloop()
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69215079

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档