【问题标题】:Mainloop only seems to be running once in tkinterMainloop 似乎只在 tkinter 中运行一次
【发布时间】:2020-04-07 02:33:44
【问题描述】:

好吧,我正在编写一个程序来为应该在循环中显示字母的文本设置动画: T

寿

汤姆

托马

托马斯

托马斯

托马斯苏

托马斯成功了……

以此类推,直到它重置然后再次循环。 问题是,tkinter 主循环似乎只运行一次然后退出。 代码如下:

from tkinter import *
import time

def setting():
    global thoms
    if thoms.get() == "":
        thoms.set("T")
        return
    if thoms.get() == "T":
        thoms.set("Th")
        return
    if thoms.get() == "Th":
        thoms.set("Tho")
        return
    if thoms.get() == "Tho":
        thoms.set("Thom")
        return
    if thoms.get() == "Thom":
        thoms.set("Thoma")
        return
    if thoms.get() == "Thoma":
        thoms.set("Thomas")
        return
    if thoms.get() == "Thomas":
        thoms.set("Thomas s")
        return
    if thoms.get() == "Thomas s":
        thoms.set("Thomas su")
        return
    if thoms.get() == "Thomas su":
        thoms.set("Thomas suc")
        return
    if thoms.get() == "Thomas suc":
        thoms.set("Thomas suck")
        return
    if thoms.get() == "Thomas suck":
        thoms.set("Thomas sucks")
        return
    if thoms.get() == "Thomas sucks":
        thoms.set("")
        return


window = Tk()
thoms = StringVar()
lbl = Label(window, textvariable=thoms)
lbl.grid(row=1, column=1)
setting()
time.sleep(1)
print("Run")
window.mainloop()

它第一次将变量设置为 T 然后停止,所以我输入 print 以查看它是否正在循环,它只打印到控制台一次。我该如何解决这个问题?

【问题讨论】:

  • mainloops() 一直运行,但你的函数只执行一次(甚至在主循环开始之前),所以它只设置一个值。你必须以完全不同的方式去做。您必须使用root.after(millisiecond, settings) 定期运行您的函数。
  • @furas 为什么函数只运行一次?它在主循环中,所以它不应该在每次主循环运行时运行吗?
  • 因为你只执行一次。 mainloop 不知道你想运行它很多次。它甚至不知道有一些setting() 函数。

标签: python tkinter mainloop


【解决方案1】:

您的函数只执行一次——甚至在mainloop() 开始运行之前。 mainloop 甚至不知道有函数setting()

使用window.after(100, setting),您可以要求 mainloop 在 100 毫秒(0.1 秒)后再次运行它

#from tkinter import * # not preferred
import tkinter as tk

def setting():
    if thoms.get() == "":
        thoms.set("T")
    elif thoms.get() == "T":
        thoms.set("Th")
    elif thoms.get() == "Th":
        thoms.set("Tho")
    elif thoms.get() == "Tho":
        thoms.set("Thom")
    elif thoms.get() == "Thom":
        thoms.set("Thoma")
    elif thoms.get() == "Thoma":
        thoms.set("Thomas")
    elif thoms.get() == "Thomas":
        thoms.set("Thomas s")
    elif thoms.get() == "Thomas s":
        thoms.set("Thomas su")
    elif thoms.get() == "Thomas su":
        thoms.set("Thomas suc")
    elif thoms.get() == "Thomas suc":
        thoms.set("Thomas suck")
    elif thoms.get() == "Thomas suck":
        thoms.set("Thomas sucks")
    elif thoms.get() == "Thomas sucks":
        thoms.set("")
    window.after(100, setting) # ask `mainloop` to run it again after 100ms (0.1s)

window = tk.Tk()

thoms = tk.StringVar()
lbl = tk.Label(window, textvariable=thoms)
lbl.grid(row=1, column=1)

setting() # run first time

window.mainloop()

顺便说一句:你可以写得更短

import tkinter as tk

text = "Thomas sucks"

def setting():

    l = len(thoms.get()) + 1

    if l <= len(text):
        thoms.set(text[:l])
    else:
        thoms.set("")

    window.after(100, setting) # run again after 100ms (0.1s)

window = tk.Tk()

thoms = tk.StringVar()
lbl = tk.Label(window, textvariable=thoms)
lbl.grid(row=1, column=1)

setting()

window.mainloop()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-10
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    • 2021-08-31
    相关资源
    最近更新 更多