1

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?

bravoalpha90
  • 413
  • 4
  • 5
  • `mainloops()` runs all time but your function is executed only once (even before mainloop starts) so it set only one value. You have to do in totally different way. You would have to use `root.after(millisiecond, settings)` to run your function periodically. – furas Dec 13 '19 at 16:04
  • @furas why does the function only run once? Its in mainloop so shouldnt it run every time mainloop runs? – bravoalpha90 Dec 13 '19 at 16:06
  • because you execute it only once. `mainloop` doesn't know that you whan to run it many times. It even doesn't know that there is some `setting()` function. – furas Dec 13 '19 at 16:07

1 Answers1

1

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()
furas
  • 134,197
  • 12
  • 106
  • 148