Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python Tkinter | Create different type of lines using Canvas class
Next article icon

Tkinter, the standardPythonlibrary for creating graphical user interfaces (GUIs), provides a powerful widget called the Canvas that allows us to draw and manipulate shapes. The Canvas widget inTkinteris an excellent tool for building 2D graphics. Our task is to create various shapes such as ovals, rectangles, arcs, and polygons using the Canvas class.

Canvas Methods for Shapes

Here are some essential methods provided by the Canvas class to create various shapes:

  1. Canvas.create_oval(x1, y1, x2, y2, options = ...):Used to create ovals, pie slices, and chords. The parameters (x1, y1) and (x2, y2) define the bounding box of the oval.
  2. Canvas.create_rectangle(x1, y1, x2, y2, options = ...):Used to create rectangles and squares. The coordinates (x1, y1) and (x2, y2) define the opposite corners of the rectangle.
  3. Canvas.create_arc(x1, y1, x2, y2, options = ...):Used to create arcs. Similar to the rectangle and oval, the coordinates (x1, y1) and (x2, y2) define the bounding box of the arc.
  4. Canvas.create_polygon(coordinates, options = ...):Used to create any valid shape defined by a series of points. The coordinates are passed as a list of (x, y) pairs.

Drawing Shapes on the Canvas

This Python code uses Tkinter to create a simple GUI that displays various shapes on a canvas. TheShapeclass defines methods to draw ovals, rectangles, arcs, and polygons using theCanvaswidget. These shapes are customized with different colors and line widths. The canvas is packed into the main window, and the program runs in the Tkinter event loop, allowing the shapes to be displayed interactively.

Python
fromtkinterimport*fromtkinter.ttkimport*classShape:def__init__(self,master=None):self.master=masterself.create()defcreate(self):self.canvas=Canvas(self.master)self.canvas.create_oval(10,10,80,80,outline="black",fill="white",width=2)self.canvas.create_oval(110,10,210,80,outline="red",fill="green",width=2)self.canvas.create_rectangle(230,10,290,60,outline="black",fill="blue",width=2)self.canvas.create_arc(30,200,90,100,start=0,extent=210,outline="green",fill="red",width=2)points=[150,100,200,120,240,180,210,200,150,150,100,200]self.canvas.create_polygon(points,outline="blue",fill="orange",width=2)self.canvas.pack(fill=BOTH,expand=1)if__name__=="__main__":master=Tk()shape=Shape(master)master.title("Shapes")master.geometry("330x220+300+300")mainloop()

Output:  

shapesUsingCanvasClass

Explanation:

  • TheShapeclass initializes the canvas and draws the shapes using methods likecreate_oval,create_rectangle,create_arc, andcreate_polygon.
  • The window size and position are set usinggeometry(), and the layout is managed with thepack() method.
  • Themainloop()keeps the application running and interactive.

Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp