首页 > 解决方案 > tkinter 标签不适用于 if

问题描述

我有这个代码:

def timer():
text2['text'] = str(timer.current)
timer.current = timer.current - 1
if timer.current >= 0:
    f.after(1000, timer)
def codigo():
    global text2
    f2=Frame(f, width=300, height=500)
    f2.pack()
    f2.config(bg='dark grey')
    text2=Label(f, text=spin.get(), font=('Arial Bold', 90), bg=('dark grey'))
    text2.place(x=80, y=200)
    timer.current = int(spin.get())
    timer()

    text3=Label(f, text = int(spin4.get()), font=('Arial Bold', 30), bg=('dark grey'))
    text3.place(x=200, y=350)
    text4=Label(f, text=spin4.get(), font=('Arial Bold', 30), bg=('dark grey'))
    text4.place(x=170, y=400)
    text5=Label(f, text='Ejercicios:', font=('Arial Bold', 30), bg=('dark grey'))
    text5.place(x=5, y=350)
    text6=Label(f, text='Rondas:', font=('Arial Bold', 30), bg=('dark grey'))
    text6.place(x=5, y=400)
    def ejercicio():
        #here is the problem, I think
        if text2 == 0:
            text2(text=spin4.get())
            timer.current(int(spin4.get()))
btn1 = tk.Button(f, text='Empezar', command=codigo, bg=('dark grey'))
btn1.pack()
btn1.place(x=150, y=150)
window.mainloop()

问题出在text2我做if的时候。输出必须是一个回归计时器,从 开始spin4,但text2不要改变。

我试着写一个ìntif text2 == 0:但无论如何都行不通。我尝试更改if并且仅更改text2输出,例如:

if text2 == 0:
    text2(text='Hi!')

但是输出是一样的:0

标签: pythonpython-3.xuser-interfacetkinterlabel

解决方案


if text2 == 0:
    text2.config(text="Hi!")

推荐阅读