Python Tkinter Cavas

In this tutorial we will learn about Python Tkinter Canvas. Python Tkinter Canvas allow us to add graphics in our Python application. With the help of Tkinter Canvas we can draw lines, circles, add images and other widgets. Furthermore we can draw graphs and plots with the help of Tkinter Canvas. Let's start with a small example Here we will try to draw a line in Python Tkinter Canvas. We will provide total 4 coordinates mean total four integer values like this(x1,y1) and (x2,y2).

Info! Here x is representing x axis(horizontally) and y is representing y axis(vertically).

Python Tkinter Canvas, Tkinter Canvas Options


Example to Draw a line in Python Tkinter Canvas

Example

from tkinter import *
mainWindow = Tk()

#Setting height and width of canvas
canvasWindow = Canvas(mainWindow, 
           width=200,
           height=200)

canvasWindow.pack()

#Getting mid of canvas vertically
screenMid = int(200 / 2)

# creating line in cavas using create_line method
canvasWindow.create_line(0, screenMid, 200, screenMid, fill="#333")

mainloop()    



Here you can see how we can create a simple line using create_line method in Python Tkinter Canvas. Here we draw a horizontal line. I will recommend you to draw a vertical line by yourself. It will help you to understand this create_line method. Let's move to next example. In next example we will learn how we can create rectangle in our Tkinter Canvas.

Example to Draw a rectangle in Python Tkinter Canvas

Example

from tkinter import *
mainWindow = Tk()

#Setting height and width of canvas
canvasWindow = Canvas(mainWindow, 
           width=300,
           height=200)

canvasWindow.pack()

# creating line in cavas using create_line method
canvasWindow.create_rectangle(50, 50, 250, 150, fill="red")

mainloop()    



Various Possible Options in Python Tkinter Canvas

Option Description
bd This option we can use to set border width of canvas in pixels. By default width is 2px
bg This option we can use to set background color of canvas
confine This option we can use to make canvas unscrollable
height This option we can use to set height of canvas
relief This option we can use to set border type. The possible types are SUNKEN, RAISED, GROOVE, and RIDGE.
width This option we can use to set width of canvas.


Share this tutorial with your friends on your social Media..

Python Tkinter Cavas Python Tkinter Cavas Python Tkinter Cavas Python Tkinter Cavas



Now You Can Follow All These Tutorials on Your Android Mobile Just Download This App.


Owlbuddy.com