text: from tkinter import *
  from tkinter import messagebox
  from tkinter.colorchooser import askcolor
  import threading
  import time
  from PIL import Image
  import os
  
  class Paint(object):
  
      DEFAULT_PEN_SIZE = 5.0
      DEFAULT_COLOR = 'black'
      DEFAULT_DELAY = 5 * pow(10,8)
  
      def __init__(self,w,h,q):
          self.root = Tk()
          self.q    = q
          self.time = time.time_ns()
  
          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=1)
  
          self.eraser_button = Button(self.root, text='eraser', command=self.use_eraser)
          self.eraser_button.grid(row=0, column=2)
          self.save_button = Button(self.root, text='save', command=self.save)
          self.save_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=w, height=h)
          self.c.grid(row=1, columnspan=5)
  
          self.setup()
          self.t = threading.Thread(target=self.handle).start()
          self.root.mainloop()
  
      def handle(self):
          while True:
              event = self.q.get()
              self.paint(event)
              self.time = time.time_ns()
  
      def save(self,filename=str(int(time.time()))):
          self.c.postscript(file=f"{filename}.eps")
          img = Image.open(f"{filename}.eps")
          img.save(f"{filename}.png","png")
          os.remove(f"{filename}.eps")
          #print(f"Saved canvas to {filename}!!!")
          messagebox.showinfo("Saved",f"Saved canvas to {filename}.png!!!")
  
      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):
          #print(f"Painting with event: {event.x} and {event.y}")
          diff = time.time_ns() - self.time
          print(diff)
          if diff > self.DEFAULT_DELAY:
              self.reset(None)
              return
              #self.time = time.time_ns()
          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()
  