By: Archana Shukla And Rajesh Shukla
To add a label to our previous example, we will create a label using the label class like this:
lbl = Label(window, text=”Hello”)
Then we will set its position on the form using the grid function and give it the location like this:
lbl.grid(column=0, row=0)
So the complete code will be like this:
from
tkinter
import
*
window
=
Tk()
window.title(
"Welcome to tKinter"
)
lbl
=
Label(window, text
=
"Hello"
)
lbl.grid(column
=
0
, row
=
0
)
window.mainloop()
Output:
And this is the result:
Without calling the grid function for the label, it won’t show up.
Set label font size
You can set the label font so you can make it bigger and maybe bold. You can also change the font style.
To do so, you can pass the font parameter like this:
lbl = Label(window, text=”Hello”, font=(“Arial Bold”, 50))
Note: that the font parameter can be passed to any widget to change its font not labels only.
from tkinter import *
window = Tk()
window.title("Welcome to tkinter ")
lbl = Label(window, text="Hello", font=("Arial Bold", 50))
lbl.grid(column=0, row=0)
window.mainloop()
Output:
Great, but the window is so small, we cannot even see the title, what about setting the window size?
Setting window size
We can set the default window size using geometry function like this:
window.geometry(‘350×200’)
The above line sets the window width to 350 pixels and the height to 200 pixels.
On adding the above line we get the following result.
from tkinter import *
window = Tk()
window.title("Welcome to tkinter ")
lbl = Label(window, text="Hello", font=("Arial Bold", 50))
lbl.grid(column=0, row=0)
window.geometry('350x200')
window.mainloop()
Output:
Let’s try adding more GUI widgets like buttons and see how to handle button click event.