发际线堪忧 2022-04-11 20:46 采纳率: 66.7%
浏览 131
已结题

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

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

  • 写回答

3条回答 默认 最新

  • 关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月20日
  • 已采纳回答 4月12日
  • 创建了问题 4月11日

悬赏问题

  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥30 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡