The Canvas widget is one of the versatile widgets in Tkinter. It is used to create graphics, objects such as drawing shapes, arcs, animating objects, and more. We can add a Button in the Tkinter canvas by assigning the parent as the canvas in the Button object.
In this example, we have constructed a LabelFrame which contains a canvas. Initially, the Canvas widget will hold a set of Buttons in the range.
#import required libraries from tkinter import * from tkinter import ttk #Create an instance of Tkinter frame win= Tk() #Set the geometry of the window win.geometry("750x250") #Create a LabelFrame labelframe= LabelFrame(win) #Define a canvas in the window canvas= Canvas(labelframe) canvas.pack(side=RIGHT, fill=BOTH, expand=1) labelframe.pack(fill= BOTH, expand= 1, padx= 30, pady=30) #Create Button widget in Canvas for i in range(5): ttk.Button(canvas, text= "Button " +str(i)).pack() win.mainloop()
Running the above code will display a Window that contains some buttons defined in a Canvas.