What’s canvas in tkinter?
In tkinter, the canvas widget is used to attract graphics.
You may draw:
- Textual content
- Widgets
- Graph and plots
- Frames
A tkinter canvas is an oblong space.
See the examples under for drawing varied parts within the canvas widget.
A easy instance of making a canvas
Within the first instance, we simply created a canvas widget with none graphics to point out the way it seems.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
from tkinter import *
win = Tk()
#Creating window
win.geometry(“500×500”)
win.title(“Create a Canvas”)
#Creating canvas
can = Canvas(win,bg = “#004000”,peak = “250”)
can.pack()
win.mainloop() |
Output:
Objects that may be created in canvas
Tkinter canvas helps following gadgets:
- Line
- Arc
- Oval
- Textual content
- Picture
- Rectangle
See the examples under of making this stuff with their respective strategies.
Making a line in canvas instance
For making a line within the canvas, chances are you’ll use the create_line() technique. See an instance under the place we created a line within the canvas in yellow colour:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
from tkinter import *
win = Tk()
#Creating window
win.geometry(“500×500”)
win.title(“Create Canvas with a line”)
#Creating canvas
can = Canvas(win,bg = “#004000”,peak = “250”)
#Creating yellow colour line in canvas
line = can.create_line((100, 100), (300, 200), width=4, fill=‘#FFFF00’)
can.pack()
win.mainloop() |
Output:
Creating an arc in canvas instance
Use create_arc() technique for creating an arc within the canvas.
In this system under, we created an arc by offering arc coordinates.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
from tkinter import *
win = Tk()
#Creating window
win.geometry(“500×500”)
win.title(“Create Canvas with an arc”)
#Creating canvas
can = Canvas(win,bg = “#804000”,peak = “300”)
#Creating an arc within the canvas
arc = can.create_arc((15,20,240,210),begin = 50,extent = 90, fill= “#FFD7D7”)
can.pack()
win.mainloop() |
Output:
Creating an oval form instance
The syntax for creating an oval is:
create_oval(*args, **kw)
The place arg is 2 coordinate factors.
See an instance under the place we created a form like a rooster egg:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
from tkinter import *
win = Tk()
#Creating window
win.geometry(“500×500”)
win.title(“Create Canvas with an oval form”)
#Creating canvas
can = Canvas(win,bg = “#804000”,peak = “300”)
#specify oval factors
co_pnt = (
(70, 50),
(220, 250),
)
#Creating an oval within the canvas
ova = can.create_oval(co_pnt, fill=‘#FFFFFF’)
can.pack()
win.mainloop() |
Output:
An instance of polygon in canvas
Within the instance under, we created a polygon by specifying its factors.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
from tkinter import *
win = Tk()
#Creating window
win.geometry(“500×500”)
win.title(“Create Canvas with a polygon”)
#Creating canvas
can = Canvas(win,bg = “#804000”,peak = “300”)
#specify polygon factors
poly_pnt = (
(50, 150),
(100, 100),
(150, 150),
)
#Making a polygon
poly_g = can.create_polygon(poly_pnt, fill=‘#FFFF5E’)
can.pack()
win.mainloop() |
Output:
Making a textual content entry in canvas instance
The next program creates an enter entry field contained in the canvas.
Python program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
from tkinter import *
win = Tk()
#Creating window
win.geometry(“500×500”)
win.title(“Create Canvas with an entry textbox”)
#Creating canvas
can = Canvas(win,peak = “300”)
textentry = Entry(can)
can.create_window(600, 50, window=textentry, peak=50, width=1000)
can.pack()
win.mainloop() |
Output:
For detailed choices and options out there with canvas in tkinter, go to this documentation:
https://tkinter-docs.readthedocs.io/en/newest/widgets/canvas.html