How do you create a Button on a tkinter Canvas?
In this article, we will see how to create a button on a Tkinter Canvas.
The Canvas widget display various graphics on the application. It can be used to draw simple shapes to complicated graphs. We can also display various kinds of custom widgets according to our needs.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
It is used trigger any function which is presented in the code
Intro to code :
In this we try to create button on canvas widget. Firstly make canvas then place the button on the canvas.
Syntax:
C = Canvas(root, height, width, bd, bg)
Syntax:
button = Button ( root,height,width,bg,command)
Steps :
- Import tkinter from
- Then define the window size and other requirements.
- First create canvas from the above give syntax.
- With the help of place function in tkinter place the button.
Python3
# import everything from tkinter modulefrom tkinter import * root = Tk() root.geometry('430x300') title = Label(root, text="Geeksforgeeks", bg="green", font=("bold", 30))title.pack()c = Canvas(root, width=330, height=200, bg="red")c.place(x=50, y=50)btn = Button(root, text='Welcome to Tkinter!', width=40, height=5, bd='10', command=root.destroy) btn.place(x=65, y=100) root.mainloop() |
OUTPUT:

