tkinter draw rectangle code example

Example 1: rectangle in tkinter

from tkinter import *

root = Tk()

c = Canvas(root)
c.pack()
c.create_rectangle(100, 100, 200, 200)

root.mainloop()

Example 2: tkinter draw circle

from tkinter import *
root = Tk()
myCanvas = Canvas(root)
myCanvas.pack()

def create_circle(x, y, r, canvasName): #center coordinates, radius
    x0 = x - r
    y0 = y - r
    x1 = x + r
    y1 = y + r
    return canvasName.create_oval(x0, y0, x1, y1)

create_circle(100, 100, 20, myCanvas)
create_circle(50, 25, 10, myCanvas)
root.mainloop()

Example 3: python how to draw a rectangle

import turtle
for i in range(2):
  turtle.forward(20)
  turtle.right(90)
  turtle.forward(10)