Python Tkinter | Create different type of lines using Canvas class (original) (raw)
Last Updated : 02 Nov, 2021
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 multiple points to create different drawings.
Class parameters:
Data members used:
master
canvas
Member functions used for the given class: create() method
Widgets used: Canvas
Tkinter method used:
canvas.create_line()
pack()
title()
geometry()
Below is the Python code –
Python3
from
tkinter
import
*
from
tkinter.ttk
import
*
class
GFG:
`` def
__init__(
self
, master
=
None
):
`` self
.master
=
master
`` self
.create()
`` def
create(
self
):
`` self
.canvas
=
Canvas(
self
.master)
`` self
.canvas.create_line(
15
,
25
,
200
,
25
)
`` self
.canvas.create_line(
300
,
35
,
300
,
200
, dash
=
(
5
,
2
))
`` self
.canvas.create_line(
55
,
85
,
155
,
85
,
105
,
180
,
55
,
85
)
`` self
.canvas.pack(fill
=
BOTH, expand
=
True
)
if
__name__
=
=
"__main__"
:
`` master
=
Tk()
`` geeks
=
GFG(master)
`` master.title(
"Lines"
)
`` master.geometry(
"400x250 + 300 + 300"
)
`` master.mainloop()
Output:
Example 2: For filing color in the shapes.
Python3
from
tkinter
import
*
root
=
Tk()
root.title(
"canvas line"
)
root.geometry(
"555x555"
)
our_canvas
=
Canvas(root,width
=
300
,height
=
200
,bg
=
"white"
)
our_canvas.pack()
our_canvas.create_rectangle(
50
,
150
,
250
,
50
,fill
=
"blue"
)
root.mainloop()
Output: