Tkinter Frame and Label: An easy reference

Tkinter Frame And Label

In this article, we’ll talk about the Tkinter Frame and Label widgets.

Let’s Talk About the Tkinter Frame and Label Widgets

We’ll go over both the widgets one by one to understand the fundamentals

What is a Tkinter Frame?

A frame in tkinter is a widget that appears as a rectangular area on the screen. This widget serves as a base for the placement of other widgets such as Labels, Checkbuttons, RadioButtons, etc. Tkinter frame widgets are basically used to group the other widgets on the screen.

Frames are not only used for widgets, they can also be used to place video overlays and other external processes. The frame widget has various properties such as background color, height and width.

Coding a Tkinter Frame

Here’s an example for the Frame is as shown below.

from tkinter import *

root=Tk()

frame = Frame(width=100, height=100, bg="red", colormap="new")
frame.pack(side=LEFT)

# set size of window
root.geometry('500x500')

# set an infinite loop so window stays in view
root.mainloop()
Output For Frame Example
Output For Frame Example

The small red box on the left side of the output is the tkinter frame that we created. Let’s move on to creating a label widget.

What is a Tkinter Label?

Tkinter provides the Label widget to insert any text or images into the frame. Tkinter allows several lines of text to be displayed on the frame however, only one choice of font to the user.

Labels are like typical text boxes and can be of any size. If the user defines the size, then the contents are adjusted within that size and if not it adjusts according to the length of your content on its own.

Labels have foreground and background fields to specify your choice of color. The syntax of the same is as shown below.

w = Label(root, text="Hey there", fg="red")

For background color make sure you use bg as the attribute.

Labels can be used to display PhotoImages and BitMapImages. The syntax for this is also shown below.

photo = PhotoImage(file="welcome.jpg")
w = Label(root, image=photo)
w.photo = photo

Coding a Label

from tkinter import *

root=Tk()

label1 = Label(root, text="hi, welcome to GUI using Tkinter")
label1.pack()

root.geometry('500x400')
root.mainloop()

The output of the above code is as shown below.

Output For Label Example
Output For Label Example

Implementing Tkinter Frame and Label Widgets

from tkinter import *

root=Tk()

root.title("My first GUI")

# set resizing to false
root.resizable(width=FALSE, height=FALSE)

# set size of window
root.geometry('500x400')

leftFrame = Frame(root)
leftFrame.pack(side=LEFT)

rightFrame = Frame(root)
rightFrame.pack(side=RIGHT)

label1 = Label(leftFrame, text="Left column")
label1.pack()

label3 = Label(leftFrame, text="Column content")
label3.pack()

label2 = Label(rightFrame, text="Right column")
label2.pack()

# set an infinite loop so window stays in view
root.mainloop()

The output of the above code is as shown below.

Output For Hybrid Example
Output For Hybrid Example

Conclusion

This comes to the end of our tutorial on the creation of a Frame and Labels using Tkinter. Try out this code and let us know your feedback in the comment section below.