The Wayback Machine - https://web.archive.org/web/20240926114621/https://www.geeksforgeeks.org/python-tkinter-canvas-widget/
Open In App

Python Tkinter – Canvas Widget

Last Updated : 31 Aug, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Tkinter is a GUI toolkit used in python to make user-friendly GUIs.Tkinter is the most commonly used and the most basic GUI framework available in python. Tkinter uses an object-oriented approach to make GUIs.
Note: For more information, refer to Python GUI – tkinter

Canvas widget

The Canvas widget lets us 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.

Syntax: 

C = Canvas(root, height, width, bd, bg, ..)

Optional parameters: 

  • root = root window.
  • height = height of the canvas widget.
  • width = width of the canvas widget.
  • bg = background colour for canvas.
  • bd = border of the canvas window.
  • scrollregion (w, n, e, s)tuple defined as a region for scrolling left, top, bottom and right.
  • highlightcolor colour shown in the focus highlight.
  • cursor It can be defined as a cursor for the canvas which can be a circle, a do, an arrow etc.
  • confine decides if canvas can be accessed outside the scroll region.
  • relief type of the border which can be SUNKEN, RAISED, GROOVE and RIDGE. 
     

Some common drawing methods:

  • Creating an Oval
 oval = C.create_oval(x0, y0, x1, y1, options)
  • Creating an arc
 arc = C.create_arc(20, 50, 190, 240, start=0, extent=110, fill="red")
  • Creating a Line
 line = C.create_line(x0, y0, x1, y1, ..., xn, yn, options)
  • Creating a polygon
 oval = C.create_polygon(x0, y0, x1, y1, ...xn, yn, options)

Example 1: Simple Shapes Drawing 

Python3




from tkinter import *
 
 
root = Tk()
 
C = Canvas(root, bg="yellow",
           height=250, width=300)
 
line = C.create_line(108, 120,
                     320, 40,
                     fill="green")
 
arc = C.create_arc(180, 150, 80,
                   210, start=0,
                   extent=220,
                   fill="red")
 
oval = C.create_oval(80, 30, 140,
                     150,
                     fill="blue")
 
C.pack()
mainloop()


Output:
 

python-tkinter-canvas

Example 2: Simple Paint App 

Python3




from tkinter import *
 
 
root = Tk()
 
# Create Title
root.title(  "Paint App ")
 
# specify size
root.geometry("500x350")
 
# define function when 
# mouse double click is enabled
def paint( event ):
    
    # Co-ordinates.
    x1, y1, x2, y2 = ( event.x - 3 ),( event.y - 3 ), ( event.x + 3 ),( event.y + 3 )
     
    # Colour
    Colour = "#000fff000"
     
    # specify type of display
    w.create_line( x1, y1, x2,
                  y2, fill = Colour )
 
 
# create canvas widget.
w = Canvas(root, width = 400, height = 250)
 
# call function when double
# click is enabled.
w.bind( "<B1-Motion>", paint )
 
# create label.
l = Label( root, text = "Double Click and Drag to draw." )
l.pack()
w.pack()
 
mainloop()


Output: 

python-tkinter-canvas



Previous Article
Next Article

Similar Reads

Python Tkinter | Moving objects using Canvas.move() method
The Canvas class of Tkinter supports functions that are used to move objects from one position to another in any canvas or Tkinter top-level. Syntax: Canvas.move(canvas_object, x, y)Parameters: canvas_object is any valid image or drawing created with the help of Canvas class. To know how to create object using Canvas class take reference of this. x
2 min read
Python Tkinter | Create different type of lines using Canvas class
In Tkinter, Canvas.create_line() method is used to create lines in any canvas. These lines can only be seen on canvas so first, you need to create a Canvas object and later pack it into the main window. Syntax: Canvas.create_line(x1, y1, x2, y2, ...., options = ...) Note: Minimum of 4 points are required to create a line, but you can also add multi
2 min read
Python Tkinter | Create different shapes using Canvas class
In Tkinter, Canvas class is used to create different shapes with the help of some functions which are defined under Canvas class. Any shape that Canvas class creates requires a canvas, so before creating any shapes a Canvas object is required and needs to be packed to the main window. Canvas Methods for shapes: Canvas.create_oval(x1, y1, x2, y2, op
3 min read
Difference Between The Widgets Of Tkinter And Tkinter.Ttk In Python
Python offers a variety of GUI (Graphical User Interface) frameworks to develop desktop applications. Among these, Tkinter is the standard Python interface to the Tk GUI toolkit. Tkinter also includes the 'ttk' module, which enhances the visual appeal and functionality of the applications. In this article, we will cover the differences, providing a
4 min read
How to clear Tkinter Canvas?
Tkinter is a Python Package for creating effective GUI applications. Tkinter's Canvas widget is nothing but a rectangular area that is used for drawing pictures, simple shapes, or any complex graph. We can place any widgets like text, button, or frames on the canvas. The task here is to generate a Python script that can clear Tkinter Canvas. For th
2 min read
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 :
1 min read
Creating a LabelFrame inside a Tkinter Canvas
In this article, we will discuss how to create a LabelFrame inside a Tkinter canvas. Syntax: canvas = Canvas(app, bg="#Colour of canvas", height=#Height of canvas, width=#Width of canvas) label_frame = LabelFrame(canvas, text="#Text which you want to show in LabelFrame") label = Label(label_frame, text="#Text which you want to show inside label") c
2 min read
How to Use Entry Box on Canvas - Tkinter
There are various ways of creating GUI applications in Python. One of the easiest and most commonly used ways is through the use of the Tkinter module. Tkinter offers various widgets for creating web apps. One such widget is the Entry box, which is used to display a single line of text. Are you facing the issue of creating an entry box on the canva
5 min read
Python | Menu widget in Tkinter
Tkinter is Python’s standard GUI (Graphical User Interface) package. It is one of the most commonly used package for GUI applications which comes with the Python itself. Menus are the important part of any GUI. A common use of menus is to provide convenient access to various operations such as saving or opening a file, quitting a program, or manipu
2 min read
Progressbar widget in Tkinter | Python
The purpose of this widget is to reassure the user that something is happening. It can operate in one of two modes - In determinate mode, the widget shows an indicator that moves from beginning to end under program control. In indeterminate mode, the widget is animated so the user will believe that something is in progress. In this mode, the indica
2 min read
Python | PanedWindow Widget in Tkinter
Tkinter supports a variety of widgets to make GUI more and more attractive and functional. The PanedWindow widget is a geometry manager widget, which can contain one or more child widgets panes. The child widgets can be resized by the user, by moving separator lines sashes using the mouse. Syntax: PanedWindow(master, **options) Parameters: master:
3 min read
Python Tkinter - Toplevel Widget
Tkinter is a GUI toolkit used in python to make user-friendly GUIs.Tkinter is the most commonly used and the most basic GUI framework available in Python. Tkinter uses an object-oriented approach to make GUIs. Note: For more information, refer to Python GUI – tkinter Toplevel widget A Toplevel widget is used to create a window on top of all other w
3 min read
Combobox Widget in tkinter | Python
Python provides a variety of GUI (Graphic User Interface) types such as PyQT, Tkinter, Kivy, WxPython, and PySide. Among them, tkinter is the most commonly used GUI module in Python since it is simple and easy to understand. The word Tkinter comes from the Tk interface. The tkinter module is available in Python standard library which has to be impo
3 min read
Python Tkinter - ListBox Widget
Tkinter is a GUI toolkit used in python to make user-friendly GUIs.Tkinter is the most commonly used and the most basic GUI framework available in python. Tkinter uses an object-oriented approach to make GUIs. Note: For more information, refer to Python GUI – tkinter ListBox widget The ListBox widget is used to display different types of items. The
2 min read
Python Tkinter - Scale Widget
Tkinter is a GUI toolkit used in python to make user-friendly GUIs.Tkinter is the most commonly used and the most basic GUI framework available in python. Tkinter uses an object-oriented approach to make GUIs. Note: For more information, refer to Python GUI – tkinter Scale widget The Scale widget is used whenever we want to select a specific value
2 min read
Python Tkinter - MessageBox Widget
Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter is the fastest and easiest way to create the GUI applications. Creating a GUI using tkinter is an easy ta
2 min read
Python Tkinter - Menubutton Widget
Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter is the fastest and easiest way to create the GUI applications. Creating a GUI using tkinter is an easy ta
4 min read
Creating Tabbed Widget With Python-Tkinter
Python offers a range of GUI frameworks that can be used to develop GUI based applications in Python. The most widely used Python interface is Tk interface or tkinter( as renamed in Python 3.x) . The Tkinter module offers a wide range of widgets that can be used to develop GUI applications much faster and easier compared to the other interfaces off
5 min read
Python Tkinter - Validating Entry Widget
Python offers a variety of frameworks to work with GUI applications. Tkinter or Tk interface is one of the most widely used Python interface to build GUI based applications. There are applications that require validation of text fields to prevent invalid input from the user before the form is submitted. Python allows input validation by allowing va
5 min read
Python Tkinter - ScrolledText Widget
Tkinter is a built-in standard python library. With the help of Tkinter, many GUI applications can be created easily. There are various types of widgets available in Tkinter such as button, frame, label, menu, scrolledtext, canvas and many more. A widget is an element that provides various controls. ScrolledText widget is a text widget with a scrol
3 min read
Python Tkinter – How to display a table editor in a text widget?
In this article, we will discuss how to display a table editor in a text widget. Before moving ahead let's create a simple text widget first with the help of Tkinter. For the link to the Excel file used in the code click here. Text Widget The Text widget is used to show the text editor in the Tkinter window. A user can modify the text in the text e
3 min read
Python Tkinter - Frame Widget
Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to create GUI applications. Creating a GUI using Tkinter is an easy task.
3 min read
Python Tkinter - Checkbutton Widget
The Checkbutton widget is a standard Tkinter widget that is used to implement on/off selections. Checkbuttons can contain text or images. When the button is pressed, Tkinter calls that function or method. Note: For more reference, you can read our article, What is WidgetsPython Tkinter OverviewPython Tkinter TutorialTkinter Checkbutton Widget Synta
5 min read
Hide Python Tkinter Text widget border
Tkinter is a standard GUI library for Python, providing tools to create graphical user interfaces easily. When designing interfaces, developers often encounter the need to customize widget appearances, including removing widget borders. Borders can sometimes clash with the desired aesthetic or layout of the application. This article explores what w
3 min read
Validating Entry Widget In Python Tkinter
When developing graphical user interfaces (GUIs) in Python, Tkinter is a powerful and widely-used toolkit that simplifies the process. One of the common tasks in GUI applications is obtaining user input through form fields. Tkinter's Entry widget is designed for this purpose, allowing users to input text. However, ensuring the correctness of the en
7 min read
Access the Actual Tab Widget of ttk.Notebook in Python Tkinter
Tab widgets of ttk.Notebook are essential components in a Tkinter GUI for organizing content into multiple tabs. Accessing these tab widgets allows for customization and manipulation of their properties. In this article, we will explore two different approaches to accessing the actual tab widget of ttk.Notebook. Access the actual tab widget of ttk.
2 min read
Python Tkinter - Create Button Widget
The Tkinter Button widget is a graphical control element used in Python's Tkinter library to create clickable buttons in a graphical user interface (GUI). It provides a way for users to trigger actions or events when clicked. Note: For more reference, you can read our article: What is WidgetsPython Tkinter OverviewPython Tkinter TutorialTkinter But
6 min read
Python Tkinter - Text Widget
Tkinter is a GUI toolkit used in python to make user-friendly GUIs.Tkinter is the most commonly used and the most basic GUI framework available in python. Tkinter uses an object-oriented approach to make GUIs.Note: For more information, refer to Python GUI – tkinter  Text WidgetText Widget is used where a user wants to insert multiline text fields.
5 min read
Python Tkinter - Entry Widget
Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. Python with Tkinter is the fastest and easiest way to create GUI applications. Creating a GUI using Tkinter is an easy task.In Python3 Tkinter is come preinstalled But you can also install it by using
5 min read
Tkinter | Adding style to the input text using ttk.Entry widget
Tkinter is a GUI (Graphical User Interface) module which is widely used to create GUI applications. It comes along with the Python itself. Entry widgets are used to get the entry from the user. It can be created as follows- entry = ttk.Entry(master, option = value, ...) Code #1: Creating Entry widget and taking input from user (taking only String d
2 min read
Article Tags :
Practice Tags :