Creating a LabelFrame inside a Tkinter Canvas
Last Updated :
18 Mar, 2022
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
from tkinter import *
app = Tk()
app.geometry( "500x500" )
canvas = Canvas(app, bg = "yellow" , height = 200 , width = 300 )
canvas.pack()
label_frame = LabelFrame(canvas, text = "Geeks For Geeks" )
label = Label(label_frame, text = "Data Structures" )
label.pack()
canvas.create_window( 100 , 100 , window = label_frame, anchor = 'w' )
app.mainloop()
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...