【问题标题】:tkinter label don't works with an iftkinter 标签不适用于 if
【发布时间】:2020-05-25 16:24:24
【问题描述】:

我有这个代码:

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

当我执行 if 时,问题出在 text2。输出必须是一个回归计时器,从spin4 开始,但text2 不会改变。

我试图在if text2 == 0: 上写一个ìnt,但无论如何都不起作用。 我尝试更改 if 并且只更改 text2 输出,例如:

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

但是输出是一样的:0

【问题讨论】:

  • 你把它放在函数ejercicio()中,但你从不运行函数ejercicio()
  • 顺便说一句:text2 是标签的 ID - 为什么将其与 0 进行比较?也许你的意思是text2["text"] == "0"
  • 也许你应该在 timer() - if timer.current == 0: 里面检查一下
  • 请修正你的缩进。您的代码无法按原样工作。还请包括root 定义和导入。 timer.current 是什么?你有一个函数被调用,但这是一个函数而不是一个类,所以.current 没有任何意义。
  • 您没有调用包含if 语句的函数ejercicio

标签: python python-3.x user-interface tkinter label


【解决方案1】:
if text2 == 0:
    text2.config(text="Hi!")

【讨论】:

    猜你喜欢
    • 2020-09-25
    • 1970-01-01
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 2017-07-15
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多