Open In App

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.



 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 :

  1. Import tkinter from
  2. Then define the window size and other requirements.
  3. First create canvas from the above given syntax.
  4. With the help of place function in tkinter place the button.




# import everything from tkinter module
from 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:

Article Tags :