Friday, December 27, 2024
Google search engine
HomeLanguagesCreating a LabelFrame inside a Tkinter Canvas

Creating a LabelFrame inside a Tkinter Canvas

In this article, we will discuss how to create a LabelFrame inside a Tkinter canvas. 

Syntax:

canvas = Canvas(app, bg=”#Colour of canvas”, height=#Height of canvas, width=#Width of canvas)

label_frame = LabelFrame(canvas, text=”#Text which you want to show in LabelFrame”)

label = Label(label_frame, text=”#Text which you want to show inside label”)

canvas.create_window(#Distance from x-axis, #Distance from y-axis, window=label_frame, anchor=’w’)

Stepwise Implementation

Step 1: First of all, import the library Tkinter.

from tkinter import *

Step 2: Now, create and resize a GUI app using Tkinter.

app = Tk()

app.geometry(“500×500”) #Give the size of app you want

Step 3: Then, create a canvas in the GUI app.

canvas = Canvas(app, bg=”#Colour of canvas”, height=#Height of canvas, width=#Width of canvas)

canvas.pack()

Step 4: Further, create and display a LabelFrame inside a canvas.

label_frame = LabelFrame(canvas, text=”#Text which you want to show in LabelFrame” )

Step 5: Moreover, create a label inside a LabelFrame created.

label = Label(label_frame, text=”#Text which you want to show in label”)

label.pack()

Step 6: Later, position and display the canvas and LabelFrame inside the canvas.

canvas.create_window(#Distance from x-axis, #Distance from y-axis, window=label_frame, anchor=’w’)

Step 7: Finally, make the loop for displaying the GUI app on the screen

app.mainloop()

Example:

In this example, a LabelFrame containing text ‘Data Structures‘ is displayed in the canvas containing text ‘Geeks For Geeks.

Python3




# Python program to create a
# LabelFrame inside a Tkinter canvas
  
# Import the library tkinter
from tkinter import *
  
# Create and resizing a GUI app
app = Tk()
app.geometry("500x500")
  
# Creating and displaying a canvas
canvas = Canvas(app, bg="yellow", height=200, width=300)
canvas.pack()
  
# Creating and displaying a LabelFrame
label_frame = LabelFrame(canvas, text="Geeks For Geeks")
label = Label(label_frame, text="Data Structures")
label.pack()
  
# Displaying and resizing of LabelFrame inside Canvas
canvas.create_window(100, 100, window=label_frame, anchor='w')
  
# Make the infinite loop for displaying the app
app.mainloop()


Output:

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments