- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do you create a Button on a Tkinter Canvas?
Canvas widget is one of the versatile widgets in the Tkinter library. You can use canvas to draw different shapes, arcs, and objects to animate within the canvas. To create a button on a Tkinter Canvas, simply pass the parent as the canvas in place of a parent in the Button constructor.
Example
In this example, we will see how to create a Button inside a canvas widget.
#Import the required libraries from tkinter import * from tkinter import ttk #Create an instance of Tkinter Frame win = Tk() #Set the geometry of Tkinter Frame win.geometry("700x350") #Define a function for exit def exit_program(): win.destroy() #Add a canvas widget canvas = Canvas(win, width= 350) #Add a Label widget in the Canvas label = Label(canvas, text= "Click the Button to Exit", font= ('Helvetica 17 bold')) label.pack(pady= 30) #Create a button in canvas widget ttk.Button(canvas, text= "Exit", command= exit_program).pack() canvas.pack() win.mainloop()
Output
Running the above code will display a window with a button inside the canvas.
Clicking the button "Exit" will close the window.
- Related Articles
- How to create a Button on a Tkinter Canvas?
- How do you create a Tkinter GUI stop button to break an infinite loop?
- How do I update images on a Tkinter Canvas?
- How do you create a clickable Tkinter Label?
- How to create a Tkinter toggle button?
- How to make a Button using the Tkinter Canvas widget?
- How to draw a line on a Tkinter canvas?
- How to draw a dashed line on a Tkinter canvas?
- How to draw an arc on a tkinter canvas?
- How to put an outline on a canvas text on Tkinter?
- Add image on a Python Tkinter button
- How to draw a png image on a Python tkinter canvas?
- Tkinter - How to put an outline on a canvas text
- How to press a button without touching it on Tkinter?
- How to draw a dot on a canvas on a click event in Tkinter Python?

Advertisements