首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >tkinter .Configure未运行

tkinter .Configure未运行
EN

Stack Overflow用户
提问于 2017-03-24 09:09:50
回答 1查看 1.8K关注 0票数 1

该代码本质上是一个数学测验,要求用户输入答案。然后,代码将显示答案是对还是错。

代码如下:

代码语言:javascript
复制
from tkinter import *
from tkinter import ttk
import random
import time

class TimesTable:

    def __init__(self, parent):
        """ Sets up the GUI. 
        """
        self.points = 0

        self.problem_label = ttk.Label(parent, text = "Question:") #empty for now
        self.problem_label.grid(row = 0, column=0, sticky = W, padx = 10, pady = 10)

        self.answer_entry = ttk.Entry(parent, width = 7)

        self.check_btn = ttk.Button(parent, text = "Check Answer", command = self.Check)

        self.next_btn = ttk.Button(parent, text = "Start", command = self.Next)
        self.next_btn.grid(row = 0, column = 1, sticky = W, padx = 10, pady = 10)

        self.feedback = ttk.Label(parent, text = "Click 'Start' to begin!")
        self.feedback.grid(row = 1, column = 0, sticky = W, padx = 10, pady = 10)


    def Next(self):
        time.sleep(0.2)
        ***self.feedback.configure(text = "")*** #Fix this (taking it out makes the .configure in Check work)
        number1 = random.randrange(2,10)
        number2 = random.randrange(2,10)
        operation = ["*", "+", "-"]
        operation_ran = operation[(random.randrange((len(operation))))]
        display_question = "Question: {} {} {} = ".format(number1, operation_ran, number2)
        self.ans = eval("{} {} {} ".format(number1, operation_ran, number2))
        self.problem_label.configure(text = display_question)
        self.check_btn.grid(row = 1, column = 1, sticky = W, padx = 10, pady = 10)
        self.answer_entry.grid(row = 0, column = 1, sticky = W, padx = 10, pady = 10)
        self.next_btn.grid_remove()

    def Check(self):
        try:
            if int(self.answer_entry.get()) == int(self.ans):
                ***self.feedback.configure(text = "Correct!")*** #Why does this not work? in def Next, it is configured to nothing but shouln't it still sleep for 0.2s before configuring to nothing via the calling of method def Next???
                self.points += 1
                self.answer_entry.delete(0,END)
                self.answer_entry.focus()
                print(self.points) #Just to check point system works
                self.Next()
            else:
                ***self.feedback.configure(text = "Wrong. The answer is {}".format(self.ans))***
                if self.points > 0:
                    self.points -= 1
                self.answer_entry.delete(0,END)
                self.answer_entry.focus()
                print(self.points)
                self.Next()

        except ValueError:
            self.feedback.configure(text = "Please enter a valid number")
            self.answer_entry.delete(0,END)
            self.answer_entry.focus()

#main routine
if __name__ == "__main__":
    root = Tk()
    root.title("Math Quiz")
    tester = TimesTable(root)
    root.mainloop()
EN

回答 1

Stack Overflow用户

发布于 2017-03-24 14:10:33

这两个配置都运行正常,但你看不到“正确”的文本,因为它正在等待,你需要更新你的屏幕,然后才能让它等待看到变化。

为此,您可以使用update_idletasks方法。

此外,您还可以使用after方法来代替睡眠。对于这段特定的代码,这并不重要,但至少可以避免另一次导入(time模块)。

代码语言:javascript
复制
def Next(self):
    root.update_idletasks()
    root.after(200)
    self.feedback.configure(text = "")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42989711

复制
相关文章

相似问题

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