Canvas
Canvas is a widget that lets you to draw on, following program demonstrates how to draw some basic shapes
import tkinter base = tkinter.Tk() base.title("canvas example") canvas = tkinter.Canvas(base, bg="white", height=250, width=300) canvas.pack() canvas.create_line(0, 100, 100, 100, fill="black") #x, y to x, y canvas.create_oval(10, 10, 50, 50, fill="red", outline="green") canvas.create_oval(100, 100, 150, 150, outline="green") canvas.create_polygon(0, 100, 100, 150, 200, 250, outline="green", fill="white") points = [100, 140, 110, 110, 140, 100, 110, 90, 100, 60, 90, 90, 60, 100, 90, 110] canvas.create_polygon(points, outline="black",fill='yellow', width=3) canvas.create_line(0, 0, canvas["height"], canvas["width"], fill="black") #x, y to x, y base.mainloop()
Painting using ovals example from http://www.python-course.eu/tkinter_canvas.php
from tkinter import * canvas_width = 500 canvas_height = 150 def paint( event ): python_green = "#476042" x1, y1 = ( event.x - 1 ), ( event.y - 1 ) x2, y2 = ( event.x + 1 ), ( event.y + 1 ) w.create_oval( x1, y1, x2, y2, fill = python_green ) master = Tk() master.title( "Painting using Ovals" ) w = Canvas(master, width=canvas_width, height=canvas_height) w.pack(expand = YES, fill = BOTH) w.bind( "<B1-Motion>", paint ) message = Label( master, text = "Press and Drag the mouse to draw" ) message.pack( side = BOTTOM ) mainloop()
With clear button
from tkinter import * canvas_width = 500 canvas_height = 150 def clearall(): w.delete("all") def paint( event ): python_green = "#476042" x1, y1 = ( event.x - 1 ), ( event.y - 1 ) x2, y2 = ( event.x + 1 ), ( event.y + 1 ) w.create_oval( x1, y1, x2, y2, fill = python_green ) master = Tk() master.title( "Painting using Ovals" ) w = Canvas(master, width=canvas_width, height=canvas_height) w.pack(expand = YES, fill = BOTH) w.bind( "<B1-Motion>", paint ) message = Label( master, text = "Press and Drag the mouse to draw" ) message.pack( side = BOTTOM ) button = Button(master, text="Clear", command=clearall) button.pack(side=BOTTOM) mainloop()