In this TKinter Tutorial we want to learn creating of TKinter Label, so using label you can create and add text in your tkinter window. now this is the complete code for creating of label in python.
This is the complete code for this lesson.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
from tkinter import * class Window(Tk): def __init__(self): super(Window, self).__init__() self.title("Tkinter Label") self.minsize(500,400) self.wm_iconbitmap("myicon.ico") self.createLabel() def createLabel(self): labelFont = ('times', 40, 'bold') label = Label(self, text = "TKinter Label Creation") label.config(font = labelFont, bg = 'black', fg ='yellow') label.grid(column = 0, row = 0) window = Window() window.mainloop() |
in here we have used Object Oriented Programming in Python, first we have created a class, after that our class extends from TK.
For creating of the label you can use Label class and we have given the text for the label.
1 |
Label(self, text = "TKinter Label Creation") |
If you want to give font size, foreground and background for the label than you can use this code.
1 |
label.config(font = labelFont, bg = 'black', fg ='yellow') |
Also you can use layout management in tkinter, there are different layouts in tkinter, we will talk about this in the later part of the course, in here we have used grid layout, and we want to add our label in the grid layout, you need to specify the row and column in the grid.
1 |
label.grid(column = 0, row = 0) |
At the end create the object of your Window class and start your main loop.
1 2 |
window = Window() window.mainloop() |
Run the complete code and this will be the result.