- 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 to create a Button on a Tkinter Canvas?
Canvas provides direct control over the drawing of individual pixels and helps to combine them all to make a visualization. With the Tkinter Canvas widget, we can create images, draw shapes, and even, we can animate objects in an application.
In order to define a button widget, we have to provide the parent window or frame object to a button so that it ensures where it should have to be displayed. By passing the canvas object to the Tkinter Button, we can create buttons in the canvas too.
Example
#Import the required Libraries from tkinter import * from tkinter import ttk import random #Create an instance of Tkinter frame win = Tk() #Set the geometry of Tkinter frame win.geometry("750x250") def change_style(): label.config(font=('Impact', 15 ,'italic'), foreground= "white", background="black") button.config(text= "Close", command=lambda:win.destroy()) #Create a Canvas canvas= Canvas(win, width= 600, height=200, bg='bisque') canvas.pack(fill= BOTH, expand= True) #Create a Label label=Label(canvas, text= "Welcome to TutorialsPoint.com", font=(None, 15)) label.pack(pady=14) #Create a button in canvas button= Button(canvas, text="Click Me", command= change_style) button.pack(pady=20) win.mainloop()
Output
Executing the above code will display a canvas with a button and a Label Text in it.
Now, click the button "Click Me" to change the style of the label Text.
- Related Articles
- How do you create a Button on a Tkinter Canvas?
- 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?
- 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 do I update images on a Tkinter Canvas?
- How to draw a dot on a canvas on a click event in Tkinter Python?
- Add image on a Python Tkinter button
- How to create a left-arrow button on a Toolbar on iPhone/iPad?
- How to get coordinates on scrollable canvas in Tkinter?

Advertisements