Of course! It looks like you're on the right track. There are just a few small typos and variable name issues in your code. The main thing to remember is that the widget is always called Label, and you use different variable names (like myLabel1, myLabel2) to keep track of each one you create.
Here is the corrected and working version of your code:
from tkinter import *
# import random # This import isn't used in the code, so it can be removed.
# Create the main window
root = Tk()
# 1. Create the first label and assign it to the variable 'myLabel1'
# The widget is called 'Label', not 'Label1'
myLabel1 = Label(root, text="hello")
# 2. Create the second label and assign it to a different variable, 'myLabel2'
myLabel2 = Label(root, text="ola")
# 3. Place the first label (myLabel1) on the grid
# It's good practice to name the arguments 'row' and 'column' for clarity
myLabel1.grid(row=0, column=0)
# 4. Place the second label (myLabel2) on the grid
myLabel2.grid(row=1, column=1)
# Start the application's main loop
root.mainloop()
Explanation of the Changes:
-
Correct Widget Name: The class for creating a label in Tkinter is always Label, not Label1 or Label2. You use the same Label class every time you want to make a new label.
-
Unique Variable Names: In your original code, you assigned both labels to the same variable myLabel.
# Original code
myLabel = Label1(...) # myLabel now points to the first label
myLabel = Label2(...) # myLabel is immediately overwritten to point to the second label
This means you lost the reference to the first label. In the corrected code, we use two different variable names, myLabel1 and myLabel2, to store each label instance separately.
-
Correct Variable References: Because your original code didn't correctly create variables named myLabel1 and myLabel2, the lines myLabel1.grid(...) and myLabel2.grid(...) would have caused a NameError. By fixing the variable names in step 2, these lines now work perfectly.
Keep up the great work! Tkinter is a fun way to start building graphical applications.