Python tKinter 3

Create a label widget

A label can be created in the UI in Python using the Label class. The Label constructor requires the top-level window object and options parameters.

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 Python TKinter")

lbl = Label(window, text="Hello")

lbl.grid(column=0, row=0)

window.mainloop()

Output:

from tkinter import *
window = Tk()
window.title("Welcome to Python TKinter")
lbl = Label(window, text="Hello")
lbl.grid(column=0, row=0)
window.geometry("400x300+1000+20")
window.mainloop()

Output:

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.

Great, but the window is so small, we can 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.

from tkinter import *
window = Tk()
window.title("Welcome to Python TKinter")
#lbl = Label(window, text="Hello")
lbl = Label(window, text="Hello", font=("Arial Bold", 50))
lbl.grid(column=0, row=0)
window.geometry("400x300+100+20")
window.mainloop()

Output:

To change the background color and Text Color

bg=’color’
example: gf=’red’
fg=’color’
example: fg=’yellow’

example:
lbl = Label(window, text=”Hello”, font=(“Arial Bold”, 50),bg=’red’)
lbl = Label(window, text=”Hello”, font=(“Arial Bold”, 50),bg=’red’,fg=’yellow’)

from tkinter import *

window = Tk()

window.title("Welcome to Python TKinter")

#lbl = Label(window, text="Hello")
lbl = Label(window, text="Hello", font=("Arial Bold", 50),bg='red',fg='yellow')

lbl.grid(column=0, row=0)
window.geometry("400x300+100+20")
window.mainloop()

Output:

Adding Multiple Labels with different text color and background color

 

from tkinter import *

window = Tk()

window.title("Welcome to Python TKinter")

#lbl = Label(window, text="Hello")
lbl = Label(window, text="Kishan", font=("Arial Bold", 20),bg='red',fg='yellow')
lbl2 = Label(window, text="Amit", font=("Arial Bold", 20),bg='green',fg='white')
lbl3 = Label(window, text="Sumit", font=("Arial Bold", 20),bg='gray',fg='yellow')
lbl4 = Label(window, text="Mohan", font=("Arial Bold", 20),bg='blue',fg='yellow')
lbl5 = Label(window, text="Madan", font=("Arial Bold", 20),bg='pink',fg='yellow')
lbl6 = Label(window, text="Monika", font=("Arial Bold", 20),bg='red',fg='yellow')
lbl7 = Label(window, text="Monty", font=("Arial Bold", 20),bg='yellow',fg='red')
lbl8 = Label(window, text="Rohit", font=("Arial Bold", 20),bg='red',fg='yellow')
lbl9 = Label(window, text="Neelam", font=("Arial Bold", 20),bg='green',fg='yellow')


lbl.grid(column=0, row=0)
lbl2.grid(column=1,row=0)
lbl3.grid(column=2,row=0)
lbl4.grid(column=0,row=1)
lbl5.grid(column=1,row=1)
lbl6.grid(column=2,row=1)
lbl7.grid(column=0,row=2)
lbl8.grid(column=1,row=2)
lbl9.grid(column=2,row=2)

window.geometry("400x300+100+20")
window.mainloop()

Output:

Visitor Counter

0051040