I try to make a mental calculus quiz with a timer
but it seems that inside the timer loop something make blank windows pop up
the console says that the problem is with the line :
self.timer.config(text= str(timer) +" s" )
inside the "update" method
import random
import tkinter as tk
import time
a =""
d=0
range_max = 10
c = random.randint(1,range_max)
b = random.randint(1,range_max)
timer = 0
class Window(tk.Frame):
def __init__(self):
tk.Frame.__init__(self)
self.master.title("Calcul mental")
self.master.minsize(620,620)
self.grid(sticky=tk.E+tk.W+tk.N+tk.S)
top=self.winfo_toplevel()
top.rowconfigure(0, weight=1)
top.columnconfigure(0, weight=1)
global a, range_max
self.question = tk.Label(self , text = "question : " + str(c)+ " * " + str(b) ,font=("default" , 20) )
self.question.place(x = 75,y = 30)
self.myEntry = tk.Entry (self,width = 20,textvariable= "" ,font=("default" , 20))
self.myEntry.place(x = 75,y = 490)
self.myEntry.focus_set()
self.score = tk.Label(self , text = "score = "+str(d) ,font=("default" , 20))
self.score.place(x = 75,y = 540)
self.myEntry.bind("<Return>",self.getEntry)
self.timer = tk.Label(self, text = str(timer) +" s" ,font=("default" , 20) )
self.timer.place(x = 550,y = 30)
self.update()
def getEntry(self, event):
a = self.myEntry.get()
global c,b,d,timer
if a != str(c*b): #if answer is wrong
d=0
self.answer = tk.Label(self , text = str(c) + " * " + str(b) +" = " + str(c*b) ,font=("default" , 20) )
self.answer.place(x = 75,y = 100)
else: #if answer is right
d= d+1
range_max = 10**(1+(d//10))
c = random.randint(1,range_max)
b = random.randint(1,range_max)
self.question.config(text="question : " + str(c)+ " * " + str(b))
self.myEntry.delete(0,"end")
self.myEntry = tk.Entry (self,width = 20,textvariable= "" ,font=("default" , 20))
self.myEntry.place(x = 75,y = 490)
self.myEntry.focus_set()
self.score.config(text = "score = "+str(d) )
self.myEntry.bind("<Return>",self.getEntry)
timer = 0
def update(self):
global timer
timer += 1
self.timer.config(text= str(timer) +" s" )
tk.Tk().after(1000,self.update)
Window().mainloop()
Please give example for beginner
Thanks for helping =)
So i had to change
tk.Tk().after(1000,self.update)
for
self.timer.after(1000,self.update)