我正在构建一个测验应用程序,我希望在用户回答问题后,正确的 answear 将变为绿色,另一个变为红色,然后恢复正常
我尝试使用 Time.sleep() 方法,但只做了一个交易,GUI 根本没有改变
def send_answer(self, text):
return self.success() if text == self.correct else self.end_game()
def get_new_question(self):
rnd_sql = "SELECT * FROM persons ORDER BY RANDOM() LIMIT 4;"
four_persons = GAME_DB.execute(rnd_sql, ())
four_persons_names = [" ".join([person[0], person[1]]) for person in four_persons]
self.answers = four_persons_names
rnd_num = random.randrange(0, 4)
self.correct = four_persons_names[rnd_num]
print four_persons_names[rnd_num]
self.pic = CoreImage(io.BytesIO(four_persons[rnd_num][2]), ext=four_persons[rnd_num][3])
self.ids.main_pic.texture = self.pic.texture
buttons = ["button_{0}".format(i + 1) for i in range(0, 4)]
for b in buttons:
# Return to normal color
self.ids[b].background_color = [0.2, 0.5, 0.7, 1]
def success(self):
self.score += 10
buttons = ["button_{0}".format(i + 1) for i in range(0, 4)]
for b in buttons:
if self.ids[b].text == self.correct:
#Change to Green
self.ids[b].background_color = [0, 1, 0, 1]
else:
#Change to Red
self.ids[b].background_color = [1, 0, 0, 1]
self.get_new_question()
我预计颜色会在短时间内变为红色/绿色,然后恢复正常等等
你的success()
方法改变了background_color
,然后调用get_new_question()
,从而改变background_color
恢复正常。通常,当对 GUI 元素进行快速连续的一系列更改时,只会出现最后一个,因此在这种情况下您不会看到任何更改。此外,调用Time.sleep()
主线程只会导致延迟,但不会显示颜色变化。
我建议你self.get_new_question()
用类似的东西改变你的电话
Clock.schedule_once(self.get_new_question, 0.5)
这会将调用延迟self.get_new_question()
半秒,因此您应该会看到颜色变化。您还需要将签名更改self.get_new_question()
为
def get_new_question(self, dt):
或者
def get_new_question(self, *args):
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句