AttributeError: 'float' object has no attribute 'Label'

本来想做个口算,但是计时出问题

源代码:

#coding:utf-8
from tkinter import *
import tkinter as t
import random as r
from tkinter.messagebox import *
import pygame as py
from time import time
z=700
q=70
o=65
c01=[]
c00=[]
dd=[]
py.mixer.init() 
py.mixer.music.load(r'muisc\nb.wav')
math=0
def random():
    global label,s,q,o,l1,u,l2,l3,l4,l5,l6,l7,l8,l9,l10,math,a,b,c,c01,c00,d,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10
    for u in range(0,10):
        a=r.randint(1,20)
        b=r.randint(1,20)
        c1=r.randint(1,100)
        d=0
        if c1>=0 and c1<=50:
            d='+'
        elif c1>=51 and c1<=100:
            d='-'
        c00.append(a)
        c01.append(b)
        dd.append(d)
        s=str(a)+dd[u]+str(b)+'='    
        label=t.Label(w,text=s,font=('i.ttf',20))
        label.place(x=600,y=o)
        o+=35
    l1=Entry(w)
    l1.place(x=700,y=q)
    q+=35
    l2=Entry(w)
    l2.place(x=z,y=q)
    q+=35
    l3=Entry(w)
    l3.place(x=z,y=q)
    q+=35
    l4=Entry(w)
    l4.place(x=z,y=q)
    q+=35
    l5=Entry(w)
    l5.place(x=z,y=q)
    q+=35
    l6=Entry(w)
    l6.place(x=z,y=q)
    q+=35
    l7=Entry(w)
    l7.place(x=z,y=q)
    q+=35
    l8=Entry(w)
    l8.place(x=z,y=q)
    q+=35
    l9=Entry(w)
    l9.place(x=z,y=q)
    q+=35
    l10=Entry(w)
    l10.place(x=z,y=q) 
    q+=35
    math+=1
    if math==1:
        button['state'] = 'disabled'
    
    
def pf():
    try:
        e1=int(l1.get())
        e2=int(l2.get())
        e3=int(l3.get())
        e4=int(l4.get())
        e5=int(l5.get())
        e6=int(l6.get())
        e7=int(l7.get())
        e8=int(l8.get())
        e9=int(l9.get())
        e10=int(l10.get())
    except Exception:  
        showerror('一定是哪里出现了问题', '你输入的有非数字或没有输入完!')
    fenshu=0
    if dd[0]=='+' and e1==c00[0]+c01[0]:
        fenshu+=10
    elif dd[0]=='-'  and e1==c00[0]-c01[0]:
        fenshu+=10
    if dd[1]=='+'  and e2==c00[1]+c01[1]:
        fenshu+=10
    elif dd[1]=='-'  and e2==c00[1]-c01[1]:
        fenshu+=10
    if dd[2]=='+'  and e3==c00[2]+c01[2]:
        fenshu+=10
    elif dd[2]=='-'  and e3==c00[2]-c01[2]:
        fenshu+=10
    if dd[3]=='+'  and e4==c00[3]+c01[3]:
        fenshu+=10
    elif dd[3]=='-'  and e4==c00[3]-c01[3]:
        fenshu+=10
    if dd[4]=='+'  and e5==c00[4]+c01[4]:
        fenshu+=10
    elif dd[4]=='-'  and e5==c00[4]-c01[4]:
        fenshu+=10
    if dd[5]=='+'  and e6==c00[5]+c01[5]:
        fenshu+=10
    elif dd[5]=='-'  and e6==c00[5]-c01[5]:
        fenshu+=10
    if dd[6]=='+'  and e7==c00[6]+c01[6]:
        fenshu+=10
    elif dd[6]=='-'  and e7==c00[6]-c01[6]:
        fenshu+=10
    if dd[7]=='+'  and e8==c00[7]+c01[7]:
        fenshu+=10
    elif dd[7]=='-'  and e8==c00[7]-c01[7]:
        fenshu+=10
    if dd[8]=='+'  and e9==c00[8]+c01[8]:
        fenshu+=10
    elif dd[8]=='-'  and e9==c00[8]-c01[8]:
        fenshu+=10
    if dd[9]=='+'  and e10==c00[9]+c01[9]:
        fenshu+=10
    elif dd[9]=='-'  and e10==c00[9]-c01[9]:
        fenshu+=10
    fs=t.Label(w,text=str(fenshu)+"      ",font=('img/i.ttf',20))
    fs.place(x=1300,y=10) 
    if fenshu==100:
        py.mixer.music.play()

def clock():
    global time1
    time1=True
    t=time()
    while time1:
        now=time()-t
        time_1=t.Label(w,text=str(now),font=('img/i.ttf',20))

def time_end():
    global time1
    time1=False
w=t.Tk()
w.geometry("1400x788")
w.resizable(False,False)
w.title('加减法口算题卡')
lab=t.Label(w,text='分数:',font=('img/i.ttf',20))
lab.place(x=1200,y=10)
button=Button(w,text='生成算式',command=lambda:[clock(), random()])
button.pack()
m=Button(w,text='评分',command=pf)
m.pack()
photo=t.PhotoImage(file="img/bg.png")
bg=t.Label(w,image=photo,compound = t.CENTER,)
bg.pack()
w.mainloop()


出问题的地方:

def clock():
    global time1
    time1=True
    t=time()
    while time1:
        now=time()-t
        time_1=t.Label(w,text=now,font=('img/i.ttf',20))

def time_end():
    global time1
    time1=False
w=t.Tk()
w.geometry("1400x788")
w.resizable(False,False)
w.title('加减法口算题卡')
lab=t.Label(w,text='分数:',font=('img/i.ttf',20))
lab.place(x=1200,y=10)
button=Button(w,text='生成算式',command=lambda:[clock(), random()])
button.pack()
m=Button(w,text='评分',command=pf)
m.pack()
photo=t.PhotoImage(file="img/bg.png")
bg=t.Label(w,image=photo,compound = t.CENTER,)
bg.pack()
w.mainloop()

我想按下生成算式就开始计时,但是报错

clock()函数中局部变量t 与全局变量t 重名了
t=time() 局部变量t赋值为数值
数值对象没有.Label方法
clock()函数中局部变量t 改成t1 即可

def clock():
    global time1
    time1=True
    t1=time()
    while time1:
        now=time()-t1
        time_1=t.Label(w,text=str(now),font=('img/i.ttf',20))

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

clock函数有问题,局部变量t = time()覆盖了全局t(tkinter),导致t.Label报错,建议换个名字

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632