In this TKinter Tutorial we are going to learn about TKinter Canvas, The Canvas is a rectangular area intended for drawing pictures or other complex layouts. You can place graphics, text,widgets or frames on a Canvas. We Can Create Difference like arc, polygon, image, line.
This is the complete code for this lesson.
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 |
import tkinter as tk class Window(tk.Tk): def __init__(self): super(Window, self).__init__() self.title("Canvas In TKinter") self.minsize(500,400) self.wm_iconbitmap("myicon.ico") self.create_canvas() def create_canvas(self): canvas = tk.Canvas(self, bg = "red", height = 250, width = 300) coord = 10,50, 240, 210 #canvas.pack(expand = YES, fill=BOTH) arc = canvas.create_arc(coord, start = 0, extent = 150, fill = "yellow") canvas.pack() window = Window() window.mainloop() |
You can use tk.Canvas() for creating of the canvas, you need to give some parameters like background color, height and weight of the canvas.
1 |
canvas = tk.Canvas(self, bg = "red", height = 250, width = 300) |
In here we are going to create a simple Arc, and you can use create_arc() from canvas, you need to give the coords for the arc that we have already created, also you can give the start position with the fill color.
1 |
arc = canvas.create_arc(coord, start = 0, extent = 150, fill = "yellow") |
i have commented the canvas.pack(). so if you want your canvas takes whole TKinter window you can uncomment that.
1 |
#canvas.pack(expand = YES, fill=BOTH) |
Run the complete code and this is the result.