Creating a LabelFrame inside a Tkinter Canvas


Tkinter provides many built-in widgets which can be used to create highlevel desktop applications. The LabelFrame widget is one of them, which allows the users to add a labelled frame. The Label is another widget in the LabelFrame, which is used to add text or images in a frame or any container.

There are two main components of the LabelFrame widget,

  • The Title Bar (also known as the text of the LabelFrame widget).

  • The content (the content of the LabelFrame widget. You can add an image, or text as the content inside the LabelFrame widget.)

To define a LabelFrame widget, you’ll need to define the constructor of the LabelFrame(root) widget.

Example

Here is a working example of the LabelFrame widget in which we will add some text as the content of the LabelFrame widget.

# Import the required libraries
from tkinter import *

# Create an instance of tkinter frame or window
win = Tk()

# Set the size of the window
win.geometry("700x350")

# Create a canvas widget
canvas= Canvas(win)
canvas.pack()

# Create a LabelFrame widget
lf = LabelFrame(canvas,text= "Welcome Window")

# Add a label in the labelFrame widget
label= Label(lf, text= "This text is inside the LabelFrame.")
label.config(font= 'Arial 12')
label.pack(padx=20, pady=20)

lf.pack()
win.mainloop()

Output

Running the above code will display a window that contains a LabelFrame widget and some text inside it.

Updated on: 16-Dec-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements