简体   繁体   中英

Inserting a dynamic tkinter variable into a function arguments

How do you insert a dynamic tkinter variable into a function's arguments? I'm a newbie that just recently starting learning python.

Let's say I have a function with tkinter after() method that updates every second and that constantly change an integer variable value to my local time's second value. Then I want to put that variable into a function that does something like down here.

import tkinter as tk
import time as tm

wdw = tk.Tk()
wdw.geometry('500x500')
insert = tk.IntVar() #defined the variable that will inserted to function something

def clock():
    scd= int(tm.strftime('%S')) #get the second local time
    insert.set(scd) # set the insert variable with the local time
    wdw.after(1000,clock) #update the variable scd every second

def something(val):
    u=val #set the wanted letter from the text below
    p=0
    q=0
    TXT= 'This is a text that will be rendered to the label below every 3 letter per second'
    while u < val+3:
        label=tk.Label(wdw,text=TXT[u],font="Courier",fg="red") #prints the needed letter
        label.place(x=17+p,y=17+q)
        p+=17 #translate by the x axis
        q+=17 #translate by the y axis
        u+=1 #cycle to the next letter

clock() #run the clock function to refresh the scd variable
something(insert.get()) #run the something function with the updated 'insert' variable
wdw.mainloop()

The problem with the code above is the variable insert is stuck and won't update every second, so I'm stuck with a window that only render a text that associated with the second I run the code.

How can I make the variable insert dynamic, so in a way that it would render different text depending with the second in my local time. Like it would render "Thi" during the first second, then render "his" during the second second, "is " during the third second, "sa" at fourth second, " a " at fifth second and so on.

Is there a function inside the tkinter that I wasn't aware of that could fix the problem, or is it the way python just can't update a variable dynamically? Any help will be appreciated

Just trigger the something function from the clock function. Then it will run every second. Like this:

import tkinter as tk
import time as tm

wdw = tk.Tk()
wdw.geometry('500x500')
insert = tk.IntVar() #defined the variable that will inserted to function something

def clock():
    scd= int(tm.strftime('%S')) #get the second local time
    insert.set(scd) # set the insert variable with the local time
    wdw.after(1000,clock) #update the variable scd every second
    something() #run the something function with the updated 'insert' variable

def something():
    val=insert.get() #set the wanted letter from the text below
    u=val #set the wanted letter from the text below
    p=0
    q=0
    TXT= 'This is a text that will be rendered to the label below every 3 letter per second'
    while u < val+3:
        label=tk.Label(wdw,text=TXT[u],font="Courier",fg="red") #prints the needed letter
        label.place(x=17+p,y=17+q)
        p+=17 #translate by the x axis
        q+=17 #translate by the y axis
        u+=1 #cycle to the next letter

clock() #run the clock function to refresh the scd variable
wdw.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM