首页 > 解决方案 > 如何在 Tkinter 中使用带有 root.after() 的嵌套函数?

问题描述

我正在尝试在以下代码中Tkinter使用嵌套函数:.after()

from tkinter import *

a=0

def fct1() :
    global a
    global b

    b=0
    text = Label(root, text="Hey!")
    text.pack()

    def fct2() :
        global b

        text2 = Label(root, text="Yes")
        text2.pack()

        b+=1
        if b>3 :
            return
        root.after(5,fct2)

    fct2()

    a+=1
    if a>5 :
        return
    root.after(25,fct1)



root = Tk()

fct1()

root.mainloop()

使用此代码,我想显示一个“嘿!” 在我的 Tkinter 根目录中,然后运行函数fct2将显示 4“是”,然后再执行 5 次。

但是当我运行代码时,6“嘿!” 按预期显示,但在每一个中,我们看到显示的“是”的数量看起来是随机的,而每次应该是 4。有谁知道我为什么会遇到这个问题?

标签: pythontkinter

解决方案


推荐阅读