Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple paint application using tkinter in Python 3
from tkinter import *
from tkinter.colorchooser import askcolor
class Paint(object):
DEFAULT_PEN_SIZE = 5.0
DEFAULT_COLOR = 'black'
def __init__(self):
self.root = Tk()
self.pen_button = Button(self.root, text='pen', command=self.use_pen)
self.pen_button.grid(row=0, column=0)
self.brush_button = Button(self.root, text='brush', command=self.use_brush)
self.brush_button.grid(row=0, column=1)
self.color_button = Button(self.root, text='color', command=self.choose_color)
self.color_button.grid(row=0, column=2)
self.eraser_button = Button(self.root, text='eraser', command=self.use_eraser)
self.eraser_button.grid(row=0, column=3)
self.choose_size_button = Scale(self.root, from_=1, to=10, orient=HORIZONTAL)
self.choose_size_button.grid(row=0, column=4)
self.c = Canvas(self.root, bg='white', width=600, height=600)
self.c.grid(row=1, columnspan=5)
self.setup()
self.root.mainloop()
def setup(self):
self.old_x = None
self.old_y = None
self.line_width = self.choose_size_button.get()
self.color = self.DEFAULT_COLOR
self.eraser_on = False
self.active_button = self.pen_button
self.c.bind('<B1-Motion>', self.paint)
self.c.bind('<ButtonRelease-1>', self.reset)
def use_pen(self):
self.activate_button(self.pen_button)
def use_brush(self):
self.activate_button(self.brush_button)
def choose_color(self):
self.eraser_on = False
self.color = askcolor(color=self.color)[1]
def use_eraser(self):
self.activate_button(self.eraser_button, eraser_mode=True)
def activate_button(self, some_button, eraser_mode=False):
self.active_button.config(relief=RAISED)
some_button.config(relief=SUNKEN)
self.active_button = some_button
self.eraser_on = eraser_mode
def paint(self, event):
self.line_width = self.choose_size_button.get()
paint_color = 'white' if self.eraser_on else self.color
if self.old_x and self.old_y:
self.c.create_line(self.old_x, self.old_y, event.x, event.y,
width=self.line_width, fill=paint_color,
capstyle=ROUND, smooth=TRUE, splinesteps=36)
self.old_x = event.x
self.old_y = event.y
def reset(self, event):
self.old_x, self.old_y = None, None
if __name__ == '__main__':
Paint()
@andreys42
Copy link

andreys42 commented Mar 6, 2019

how to implement eraser tool in this app? I mean not just painting white...

@SubhoBasak
Copy link

SubhoBasak commented Nov 9, 2019

Line no 52, in choose_color function

self.color = askcolor(color=self.color)[1]

it is better to write it in this way,

color = askcolor(color = self.color)[1]
if color != None:
    self.color = color
else:
    return None

@RVSNS
Copy link

RVSNS commented Nov 19, 2019

code to completely clear the canvas ?
thank you in advance
and also is there any way to save my "Masterpiece"

@SubhoBasak
Copy link

SubhoBasak commented Nov 20, 2019

Tkinter canvas keep track of every elements is added to it and return an id of each element. You can store the id, to specify which element to delete.
id1= canvas.create_line(0, 0, 100, 100)
And then to delete the element use the following code,
canvas.delete(id1)

And, to delete all the elements just use 'all' as the parameter to delete method,
canvas.delete('all')

@niralivasoya
Copy link

niralivasoya commented Mar 10, 2020

Hey @nikhilkumarsingh, how can i save masterpiece so that i can directly use it in another application? is there any way to directly connect this paint with another python file?

@LosGnidoS
Copy link

LosGnidoS commented Jul 26, 2020

Super!

@dubeyji10
Copy link

dubeyji10 commented Aug 14, 2020

Hey @nikhilkumarsingh, how can i save masterpiece so that i can directly use it in another application? is there any way to directly connect this paint with another python file?

if you plan on saving it as an image grab the rectangle ( could use win32gui.GetWindowRect) and then save it using 'filedialog.asksaveasfilename'

@hq9000
Copy link

hq9000 commented Nov 16, 2020

if anyone, like me, is wondering if we can make it anti aliased somehow, here is an interesting approach:

vlachoudis/bCNC#986

@PVkolos
Copy link

PVkolos commented Apr 24, 2021

код, чтобы полностью очистить холст?
заранее спасибо,
а также есть ли способ спасти мой "Шедевр"
Found the best repository, everything you need is there, here it is: https://github.com/PVkolos/Paint_Python
You can save the drawing and paint over with white, in general, everything you need

@PVkolos
Copy link

PVkolos commented Apr 24, 2021

Привет, @nikhilkumarsingh , как я могу сохранить шедевр, чтобы напрямую использовать его в другом приложении? есть ли способ напрямую связать эту краску с другим файлом python?
Found the best repository, everything you need is there, here it is: https://github.com/PVkolos/Paint_Python
You can save the drawing and paint over with white, in general, everything you need

@Kaabasane
Copy link

Kaabasane commented May 19, 2021

Thank you @nikhilkumarsingh. I have updated the code a bit. May I upload that code on my git, crediting you for base code?

@Kaabasane
Copy link

Kaabasane commented Jul 2, 2021

code to completely clear the canvas ?
thank you in advance
and also is there any way to save my "Masterpiece"

&

how to implement eraser tool in this app? I mean not just painting white...

@andreys42 & @RVSNS try this fork by me - https://gist.github.com/Kaabasane/a4f1fb10d27611bdd6b4ccf6b9206a9b

@tlantzakis
Copy link

tlantzakis commented Jan 26, 2022

Thank you.

@taoclav
Copy link

taoclav commented Feb 2, 2022

can you do a save option? i have a similar program but i don't know how to make a file save option

@Kaabasane
Copy link

Kaabasane commented Feb 3, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment