Answer the question
In order to leave comments, you need to log in
How to pass Lable as function parameter (Tkinter Python3)?
There is a code that draws a window and a Label inside it. There is a function that simulates typing, you need the ability to pass the name of the label in which the animation should take place, but when you try to do this, it is passed as an int. How to fix it?
PS If inside the function with animation the name of the label is written not as a parameter, but simply as a global name, then everything works.
Non-working code with an attempt to pass a parameter:
import random
from tkinter import *
root = Tk()
root.title("Initial Fantasy")
root.geometry("800x600")
def typewrite(labelWidget,counter=1):
labelWidget.config(text="Some Text"[:counter])
root.after(random.randrange(30,150), lambda: typewrite(counter+1))
mainTextBox = Label(text="", fg="#eee", bg="#333")
mainTextBox.place(relx=.50, rely=.325, anchor="center")
typewrite(mainTextBox)
root.mainloop()
import random
from tkinter import *
root = Tk()
root.title("Initial Fantasy")
root.geometry("800x600")
def typewrite(counter=1):
mainTextBox.config(text="Some Text"[:counter])
root.after(random.randrange(30,150), lambda: typewrite(counter+1))
mainTextBox = Label(text="Этого текста быть не должно", fg="#eee", bg="#333")
mainTextBox.place(relx=.50, rely=.325, anchor="center")
typewrite()
root.mainloop()
Answer the question
In order to leave comments, you need to log in
In the first code example, label is not always passed. Fix it lambda: typewrite(counter+1)
and lambda: typewrite(labelWidget, counter+1)
everything should work.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question