I have a problem getting python tkinter Labels to appear

Here is my code:
import tkinter as tk
root = tk.Tk()
root.geometry(“500x500”)
label1=tk.Label(root,text=“11111”,bg=“white”,fg=“black”)
label2=tk.Label(root,text=“22222”,bg=“white”,fg=“black”)
label3=tk.Label(root,text=“33333”,bg=“white”,fg=“black”)
label4=tk.Label(root,text=“44444”,bg=“white”,fg=“black”)
label1.place=(x:=50,y:=250,width:=30,height:=15)
label2.place=(x:=100,y:=250,width:=30,height:=15)
label3.place=(x:=150,y:=250,width:=30,height:=15)
label4.place=(x:=200,y:=250,width:=30,height:=15)
root.update()
root.mainloop()

A totally empty window opens. What am I doing wrong?

Look closely at these lines:

label1.place=(x:=50,y:=250,width:=30,height:=15)
label2.place=(x:=100,y:=250,width:=30,height:=15)
label3.place=(x:=150,y:=250,width:=30,height:=15)
label4.place=(x:=200,y:=250,width:=30,height:=15)

label1.place=(x:=50,y:=250,width:=30,height:=15) is assigning a tuple to label1.place, and the := are not keyword parameters.

That line should be label1.place(x=50,y=250,width=30,height=15).

Similarly for the other lines.

I tried that first! I got an error suggesting “:=” or “==”, So I put in the colons.

It probably suggested “:=” or “==” because it looked you were assigning a tuple to label1.place!

So what should I set it to?

You shouldn’t set it; you should call it, like I showed in my first reply.

1 Like

Thank you! That works.

When Python saw the first attempt at the code, which I am guessing was

label1.place=(x=50,y=250,width=30,height=15)

Because of the first = (before the (), this is not valid syntax. Python would guess that you want to make something that looks like (x=50,y=250,width=30,height=15), and then say that label1.place should be equal to that. But (x=50,y=250,width=30,height=15) is not a valid thing.

Using := instead of the = here, like (x:=50,y:=250,width:=30,height:=15), means to create separate variables x y, width and height; set their values; and then also use those values in order to make the tuple (50, 250, 30, 15).

Using == instead, like (x==50,y==250,width==30,height==15), means to compare those variables to see if the values are equal, then make a tuple with the result of those comparisons. This would obviously be wrong in your case, because you did not already have those variables. But it would be legal Python syntax.

Both of these would make the code into valid code that Python can make sense of, and try to run later (it would fix the SyntaxError - this explains what “syntax” actually means). This is the only kind of error that Python tries to detect ahead of time - anything else that can go wrong, will go wrong when the code actually runs.

In your case, both of these attempts make the code wrong. You do not want to assign anything to label1.place at all. This .place is a method, meaning that once you have created label1, it is code that you can call (like a function) to work with the label1 (which is a tk.Label that you made in the previous code). This is the same as when you do root.geometry("500x500"); or you may have seen other examples, for example calling methods on a string like "hello world".title().