user9872319
user9872319

Reputation:

Why does this create 2 windows?

I have a program with a function that takes times from an API and returns those times if called. The following code should create a window with Tkinter and display the time resp() returns. As the times from API always change, it updates those times every 2 seconds.

L=Label(text=resp(), font=("Arial Bold", 35))
L.grid(row=1, column=1)


# Call this function where the value of your variable/number changes
def ChangeValue():
    y=resp()
    root.config(text=y)
    print("Value Changed")
    root.after(2000,lambda :ChangeValue())

root=Tk()
root.update()
root.after(2000,lambda :ChangeValue())
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
root.title("Abfahrt nächster Zug")
root.geometry('1920x1080')
root.mainloop()

If I run this code, it creates a small window that contains resp - formatted like it is being told to in line 1. But that window isn't 1920x1080 like it should be.

But it creates a second window that is 1920x1080 and doesn't contain any text.

What I want is a 1920x1080 window that contains resp (font = arial, fontsize = 35)

Upvotes: 1

Views: 42

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385970

Why does this create 2 windows?

First, you create a label with this statement:

L=Label(text=resp(), font=("Arial Bold", 35))

Because you haven't yet created a root window, tkinter will create one for you since there must be a window in which to put the label.

Next, you explicitly create another window with this statement:

root=Tk()

If you want a single window, you need to explicitly create a root window before you create any other widgets.

Upvotes: 3

Related Questions