1
コードは基本的に、ユーザーに回答を入力するよう求める数学クイズです。答えが正しいか間違っているかどうかを示すコードが表示されます。しかし、self.feedback.configure(text = "")が "Check"メソッドに挿入されている場合、現在、self.feedback.configure(text = "Correct")はメソッド "Next"で実行されていませんコードが戻って空の文字列「」に設定する前に、0.2秒の「正しい」に設定する必要がありますので、0.2秒の遅延)tkinter。構成が実行されていない
これはコードです:
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()
私のコードは今のところ動作していますが、** update_idletasks **が何をするかは不明です。 "他のイベントを処理することなく、すべての未処理のアイドルタスクを呼び出します。これは、コールバックを呼び出さずに、必要に応じてジオメトリ管理と再描画ウィジェットを実行するために使用できます。 **保留中のアイドルタスク**はroot.after(200)を参照していますか?したがって、** update_idletasks **メソッドはコードを停止し( "他のイベントを処理せずに")、root.after(200)を実行します( "すべての未処理のアイドルタスクを呼び出します)。 (200)が実行されていますか? –
'root.after(200)'に2番目の引数を指定しても何も行われません。 – Junuxx
Qで意図された 'Next()'の実行を遅らせるには、おそらく 'Check()'の中で 'root.after(200、self.Next)'を呼びたいと思うでしょう。 – Junuxx