Tkinter 不需要的窗口在循环内弹出

问题描述 投票:0回答:1

我试着用计时器做一个心算测验

但似乎在定时器循环内部会弹出空白窗口

控制台说问题出在线路上:

self.timer.config(text= str(timer) +" s" )

在“更新”方法中

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()

初学者请举个例子

感谢您的帮助=)

tkinter popupwindow
1个回答
0
投票

所以我不得不改变

tk.Tk().after(1000,self.update)

self.timer.after(1000,self.update)
© www.soinside.com 2019 - 2024. All rights reserved.