How To Draw Line In Python
Drawing in Tkinter
last modified July 6, 2020
In this part of the Tkinter tutorial we will do some cartoon. Drawing in Tkinter is washed on the Canvas widget. Canvas is a high-level facility for doing graphics in Tkinter.
Information technology can be used to create charts, custom widgets, or create games.
Tkinter canvass
A canvas widget manages a 2D collection of graphical objects — lines, circles, images, or other widgets. It is suitable for drawing or edifice more complex widgets.
Tkinter describe lines
A line is a simple geometric archaic. The create_line method creates a line item on the Canvass.
lines.py
#!/usr/bin/env python3 """ ZetCode Tkinter tutorial The example draws lines on the Canvas. Author: Jan Bodnar Website: www.zetcode.com """ from tkinter import Tk, Canvas, Frame, BOTH class Example(Frame): def __init__(cocky): super().__init__() self.initUI() def initUI(self): cocky.main.title("Lines") cocky.pack(fill=BOTH, expand=ane) canvas = Canvass(self) sail.create_line(15, 25, 200, 25) sheet.create_line(300, 35, 300, 200, dash=(4, two)) sail.create_line(55, 85, 155, 85, 105, 180, 55, 85) sheet.pack(fill=BOTH, expand=1) def main(): root = Tk() ex = Example() root.geometry("400x250+300+300") root.mainloop() if __name__ == '__main__': principal() In the code example, we draw unproblematic lines.
canvas.create_line(15, 25, 200, 25)
The parameters of the create_line method are the x and y coordinates of the start and terminate points of the line.
canvas.create_line(300, 35, 300, 200, dash=(4, two))
A vertical line is drawn. The nuance option specifies the dash pattern of the line. Nosotros have a line consisting of alternating segments of 4 px dash and 2 px infinite.
sail.create_line(55, 85, 155, 85, 105, 180, 55, 85)
The create_line method can accept multiple points. This line draws a triangle.
Tkinter colours
A colour is an object representing a combination of Red, Green, and Blue (RGB) intensity values.
colours.py
#!/usr/bin/env python3 """ ZetCode Tkinter tutorial This programme draws 3 rectangles filled with different colours. Author: Jan Bodnar Website: www.zetcode.com """ from tkinter import Tk, Canvass, Frame, BOTH class Example(Frame): def __init__(self): super().__init__() self.initUI() def initUI(self): self.master.title("Colours") self.pack(fill=BOTH, expand=one) canvas = Canvas(cocky) canvas.create_rectangle(30, 10, 120, 80, outline="#fb0", fill="#fb0") canvass.create_rectangle(150, 10, 240, 80, outline="#f50", fill up="#f50") canvas.create_rectangle(270, ten, 370, 80, outline="#05f", fill="#05f") canvas.pack(make full=BOTH, expand=one) def main(): root = Tk() ex = Example() root.geometry("400x100+300+300") root.mainloop() if __name__ == '__main__': master() In the code case, we draw 3 rectangles and fill them with dissimilar color values.
canvas = Sheet(self)
Nosotros create the Canvass widget.
sheet.create_rectangle(30, x, 120, 80, outline="#fb0", fill="#fb0")
The create_rectangle creates a rectangle item on the canvass. The first iv parameters are the x and y coordinates of the two bounding points: the summit-left and lesser-right points. With the outline parameter we control the color of the outline of the rectangle. Likewise, the make full parameter provides a color for the within of the rectangle.
Tkinter geometric shapes
We can draw various shapes on the Canvas. The post-obit code example volition prove some of them.
shapes.py
#!/usr/bin/env python3 """ ZetCode Tkinter tutorial In this script, we draw basic shapes on the canvass. Writer: Jan Bodnar Website: world wide web.zetcode.com """ from tkinter import Tk, Canvass, Frame, BOTH class Instance(Frame): def __init__(self): super().__init__() self.initUI() def initUI(self): self.main.title("Shapes") self.pack(make full=BOTH, expand=i) sheet = Canvas(self) canvas.create_oval(10, 10, 80, 80, outline="#f11", fill="#1f1", width=2) sheet.create_oval(110, x, 210, eighty, outline="#f11", fill="#1f1", width=2) canvas.create_rectangle(230, ten, 290, 60, outline="#f11", fill="#1f1", width=2) canvas.create_arc(30, 200, 90, 100, start=0, extent=210, outline="#f11", fill up="#1f1", width=2) points = [150, 100, 200, 120, 240, 180, 210, 200, 150, 150, 100, 200] sail.create_polygon(points, outline='#f11', make full='#1f1', width=2) sail.pack(fill=BOTH, aggrandize=1) def principal(): root = Tk() ex = Instance() root.geometry("330x220+300+300") root.mainloop() if __name__ == '__main__': primary() Nosotros describe v dissimilar shapes on the window: a circle, an ellipse, a rectangle, an arc, and a polygon. Outlines are drawn in ruby-red and insides in green. The width of the outline is 2px.
canvas.create_oval(10, 10, fourscore, lxxx, outline="#f11", fill up="#1f1", width=two)
Here the create_oval() method is used to create a circle particular. The showtime four parameters are the bounding box coordinates of the circle. In other words, they are x and y coordinates of the top-left and bottom-right points of the box, in which the circle is drawn.
canvas.create_rectangle(230, 10, 290, threescore, outline="#f11", fill="#1f1", width=2)
Nosotros create a rectangle item. The coordinates are once again the bounding box of the rectangle to exist drawn.
sheet.create_arc(30, 200, 90, 100, start=0, extent=210, outline="#f11", fill up="#1f1", width=2)
This lawmaking line creates an arc. An arc is a office of the circumference of the circumvolve. We provide the bounding box. The first parameter is the outset bending of the arc. The extent is the angle size.
points = [150, 100, 200, 120, 240, 180, 210, 200, 150, 150, 100, 200] canvass.create_polygon(points, outline='#f11', fill='#1f1', width=2)
A polygon is created. It is a shape with multiple corners. To create a polygon in Tkinter, nosotros provide the listing of polygon coordinates to the create_polygon method.
Tkinter draw image
In the post-obit example we draw an image item on the canvas.
draw_image.py
#!/usr/bin/env python3 """ ZetCode Tkinter tutorial In this script, we draw an image on the canvass. Author: Jan Bodnar Website: www.zetcode.com """ from tkinter import Tk, Sail, Frame, BOTH, NW from PIL import Image, ImageTk class Instance(Frame): def __init__(self): super().__init__() cocky.initUI() def initUI(cocky): cocky.master.title("High Tatras") cocky.pack(fill=BOTH, expand=1) self.img = Paradigm.open("tatras.jpg") self.tatras = ImageTk.PhotoImage(self.img) canvas = Canvas(cocky, width=cocky.img.size[0]+20, height=self.img.size[1]+xx) canvas.create_image(10, 10, anchor=NW, paradigm=self.tatras) canvass.pack(fill=BOTH, expand=i) def primary(): root = Tk() ex = Example() root.mainloop() if __name__ == '__main__': main() The instance displays an image on the canvas.
from PIL import Prototype, ImageTk
From the PIL (Python Imaging Library) module, we import the Image and ImageTk modules.
cocky.img = Image.open("tatras.jpg") cocky.tatras = ImageTk.PhotoImage(self.img) Tkinter does not support JPG images internally. Equally a workaround, nosotros use the Paradigm and ImageTk modules.
canvas = Canvas(self, width=self.img.size[0]+20, height=cocky.img.size[one]+twenty)
We create the Canvas widget. It takes the size of the image into account. It is 20px wider and 20px higher than the actual epitome size.
canvas.create_image(ten, 10, anchor=NW, image=self.tatras)
We use the create_image method to create an image item on the canvass. To bear witness the whole image, it is anchored to the north and to the w. The image parameter provides the photograph prototype to display.
Tkinter draw text
In the last example, we are going to draw text on the window.
draw_text.py
#!/usr/bin/env python3 """ ZetCode Tkinter tutorial In this script, nosotros depict text on the window. Author: Jan Bodnar Website: www.zetcode.com """ from tkinter import Tk, Canvas, Frame, BOTH, Westward class Example(Frame): def __init__(self): super().__init__() self.initUI() def initUI(cocky): cocky.master.championship("Lyrics") self.pack(fill up=BOTH, expand=i) canvas = Canvas(self) canvas.create_text(xx, thirty, anchor=W, font="Purisa", text="Most relationships seem so transitory") canvas.create_text(twenty, lx, anchor=Due west, font="Purisa", text="They're good but not the permanent ane") sail.create_text(xx, 130, anchor=W, font="Purisa", text="Who doesn't long for someone to concord") sail.create_text(xx, 160, anchor=W, font="Purisa", text="Who knows how to love without being told") canvas.create_text(20, 190, anchor=West, font="Purisa", text="Somebody tell me why I'grand on my ain") canvas.create_text(20, 220, anchor=Due west, font="Purisa", text="If there'south a soulmate for anybody") canvas.pack(fill=BOTH, expand=1) def main(): root = Tk() ex = Example() root.geometry("420x250+300+300") root.mainloop() if __name__ == '__main__': main() Nosotros depict lyrics of a song on the window.
canvas.create_text(twenty, 30, ballast=Due west, font="Purisa", text="Most relationships seem so transitory")
The showtime two parameters are the x and y coordinates of the heart signal of the text. If we anchor the text particular to the west, the text starts from this position. The font parameter provides the font of the text and the text parameter is the text to exist displayed.
In this function of the Tkinter tutorial, nosotros did some drawing.
Source: https://zetcode.com/tkinter/drawing/
Posted by: browncorgentor.blogspot.com

0 Response to "How To Draw Line In Python"
Post a Comment