Advertisement
steve-shambles-2109

182-Tk Paint

Oct 22nd, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.81 KB | None | 0 0
  1. """
  2. Python code snippets vol 37:
  3. 182-Tk Paint
  4. stevepython.wordpress.com
  5.  
  6. requirements: None
  7.  
  8. https://gist.github.com/nikhilkumarsingh/85501ee2c3d8c0cfa9d1a27be5781f06
  9. """
  10. from tkinter import Button, Canvas, HORIZONTAL, RAISED
  11. from tkinter import ROUND, Scale, SUNKEN, Tk, TRUE
  12. from tkinter.colorchooser import askcolor
  13.  
  14. class Paint(object):
  15.  
  16.     DEFAULT_PEN_SIZE = 5.0
  17.     DEFAULT_COLOR = 'black'
  18.  
  19.     def __init__(self):
  20.         self.root = Tk()
  21.  
  22.         self.pen_button = Button(self.root, text='pen', command=self.use_pen)
  23.         self.pen_button.grid(row=0, column=0)
  24.  
  25.         self.brush_button = Button(self.root, text='brush', command=self.use_brush)
  26.         self.brush_button.grid(row=0, column=1)
  27.  
  28.         self.color_button = Button(self.root, text='color', command=self.choose_color)
  29.         self.color_button.grid(row=0, column=2)
  30.  
  31.         self.eraser_button = Button(self.root, text='eraser', command=self.use_eraser)
  32.         self.eraser_button.grid(row=0, column=3)
  33.  
  34.         self.choose_size_button = Scale(self.root, from_=1, to=10, orient=HORIZONTAL)
  35.         self.choose_size_button.grid(row=0, column=4)
  36.  
  37.         self.c = Canvas(self.root, bg='white', width=600, height=600)
  38.         self.c.grid(row=1, columnspan=5)
  39.  
  40.         self.setup()
  41.         self.root.mainloop()
  42.  
  43.     def setup(self):
  44.         self.old_x = None
  45.         self.old_y = None
  46.         self.line_width = self.choose_size_button.get()
  47.         self.color = self.DEFAULT_COLOR
  48.         self.eraser_on = False
  49.         self.active_button = self.pen_button
  50.         self.c.bind('<B1-Motion>', self.paint)
  51.         self.c.bind('<ButtonRelease-1>', self.reset)
  52.  
  53.     def use_pen(self):
  54.         self.activate_button(self.pen_button)
  55.  
  56.     def use_brush(self):
  57.         self.activate_button(self.brush_button)
  58.  
  59.     def choose_color(self):
  60.         self.eraser_on = False
  61.         self.color = askcolor(color=self.color)[1]
  62.  
  63.     def use_eraser(self):
  64.         self.activate_button(self.eraser_button, eraser_mode=True)
  65.  
  66.     def activate_button(self, some_button, eraser_mode=False):
  67.         self.active_button.config(relief=RAISED)
  68.         some_button.config(relief=SUNKEN)
  69.         self.active_button = some_button
  70.         self.eraser_on = eraser_mode
  71.  
  72.     def paint(self, event):
  73.         self.line_width = self.choose_size_button.get()
  74.         paint_color = 'white' if self.eraser_on else self.color
  75.         if self.old_x and self.old_y:
  76.             self.c.create_line(self.old_x, self.old_y, event.x, event.y,
  77.                                width=self.line_width, fill=paint_color,
  78.                                capstyle=ROUND, smooth=TRUE, splinesteps=36)
  79.         self.old_x = event.x
  80.         self.old_y = event.y
  81.  
  82.     def reset(self, event):
  83.         self.old_x, self.old_y = None, None
  84.  
  85.  
  86. if __name__ == '__main__':
  87.     Paint()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement