bravoalpha90
bravoalpha90

Reputation: 463

Mainloop only seems to be running once in tkinter

Ok so, I'm writing a program out of spite to animate text that is supposed to display letters like so on a loop: T

Th

Tho

Thom

Thoma

Thomas

Thomas s

Thomas su

Thomas suc...

and so on till it resets and then loops again. The problem is, the tkinter mainloop only seems to run once and then quits. Here's the code:

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

It sets the variable the first time to T and then stops, so I put in the print to see if it was looping, and it only prints to the console a single time. How do I fix this?

Upvotes: 1

Views: 1114

Answers (1)

furas
furas

Reputation: 143097

Your function is executed only once - even before mainloop() start running. mainloop even doesn't know that there is function setting().

Using window.after(100, setting) you can ask mainloop to run it again after 100ms (0.1s)

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

BTW: you can write it shorter

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

Upvotes: 1

Related Questions