annstasi

draw

Nov 14th, 2022
805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.66 KB | None | 0 0
  1. from tkinter import *
  2. from PIL import Image, ImageDraw
  3. from tkinter import colorchooser
  4. from tkinter.colorchooser import askcolor
  5.  
  6.  
  7. line_id = None
  8. line_points = []
  9. #line_options = {}
  10.  
  11.  
  12. def use_line():
  13.     line_button.config(relief="sunken") # Кнопка зажата
  14.   #  erase_button.config(relief="raised")
  15.     hand_button.config(relief="raised")
  16.     root.config(cursor='plus')
  17.     canvas.bind('<B1-Motion>', create_line)
  18.     canvas.bind('<ButtonPress-1>', on_start)
  19.     canvas.bind('<ButtonRelease-1>', reset)
  20.  
  21.  
  22. def on_start(event):
  23.     global drawn, x1, y1
  24.     drawn = None
  25.     x1 = event.x
  26.     y1 = event.y
  27.  
  28.  
  29. def reset(event):
  30.     global x, y
  31.     x = 0
  32.     y = 0
  33.  
  34.  
  35. def create_line(event):
  36.     global drawn
  37.     if drawn:
  38.         canvas.delete(drawn)
  39.     objectid = canvas.create_line(x1, y1, event.x, event.y, fill=color, width=brush_size)
  40.     drawn = objectid
  41.  
  42. def bg_color():
  43.     c = askcolor()
  44.     bg_col = c[1]
  45.     canvas.configure(bg=bg_col)
  46.  
  47.  
  48. def hand_line():
  49.     root.config(cursor='arrow')
  50.     hand_button.config(relief="sunken")
  51.    # erase_button.config(relief="raised")
  52.     line_button.config(relief="raised")
  53.     canvas.bind('<B1-Motion>', draw_line)
  54.     canvas.bind('<ButtonRelease-1>', end_line)
  55.     canvas.bind('<Button-1>', set_start)
  56.  
  57.  
  58. def choose_color():
  59.     global color
  60.     (rgb, hx) = colorchooser.askcolor()
  61.     color = hx
  62.     color_lab['bg'] = hx
  63.  
  64.  
  65. def draw_line(event):
  66.     global line_id
  67.     line_points.extend((event.x, event.y))
  68.     if line_id is not None:
  69.         canvas.delete(line_id)
  70.     line_id = canvas.create_line(line_points, width=brush_size, fill=color, capstyle=ROUND)
  71.  
  72.  
  73. #def erase():  # Ластик берёт цвет фона
  74.  #   erase_button.config(relief="sunken")
  75.   #  hand_button.config(relief="raised")
  76.    # line_button.config(relief="raised")
  77.    # global color
  78.    # prev_col = color
  79.    # color = canvas['bg']
  80.    # hand_line()
  81.    # root.config(cursor='arrow')
  82.  
  83.  
  84. def set_start(event):
  85.     line_points.extend((event.x, event.y))
  86.  
  87.  
  88. def end_line(event=None):
  89.     global line_id
  90.     line_points.clear()
  91.     line_id = None
  92.  
  93.  
  94. def select(value):
  95.     global brush_size
  96.     brush_size = int(value)
  97.  
  98.  
  99. def clear_canvas():
  100.     canvas.delete('all')
  101.     canvas['bg'] = 'white'
  102.     draw_img.rectangle((0,  0, 1280, 720), width=0, fill='white')
  103.  
  104.  
  105. x = 0
  106. y = 0
  107.  
  108. root = Tk()
  109. root.title("Рисовалка")
  110. root.geometry('1280x720')
  111.  
  112. brush_size = 10
  113. color = 'black'
  114.  
  115.  
  116. root.columnconfigure(5, weight=1)
  117.  
  118. root.rowconfigure(2, weight=1)
  119.  
  120. canvas = Canvas(root, bg='white')
  121. canvas.grid(row=2, column=0, columnspan=7, padx=5, pady=5, sticky="news")
  122.  
  123.  
  124. image1 = Image.new('RGB', (1280, 640), 'white')
  125. draw_img = ImageDraw.Draw(image1)
  126.  
  127. Button(root, text='Выбрать цвет', width=11, command=choose_color).grid(row=0, column=0, padx=5)
  128.  
  129. color_lab = Label(root, bg=color, width=5)
  130. color_lab.grid(row=0, column=1)
  131.  
  132. v = IntVar(value=10)
  133. Scale(root, variable=v, from_=1, to=100, orient=HORIZONTAL, command=select).grid(row=0, column=2, padx=50)
  134.  
  135.  
  136. #erase_button = Button(root, text='Ластик', width=10, command=erase)
  137. #erase_button.grid(row=1, column=7)
  138.  
  139.  
  140. hand_button = Button(root, text='Кисть', width=10, command=hand_line)
  141. hand_button.grid(row=0, column=3, padx=5)
  142.  
  143. line_button = Button(root, text='Линия', width=10, command=use_line)
  144. line_button.grid(row=0, column=4)
  145.  
  146. clear_button = Button(root, text='Очистить', width=10, command=clear_canvas)
  147. clear_button.grid(row=0, column=5, sticky="e")
  148.  
  149. bg_button = Button(root, text='Залить фон', width=10, command=bg_color)
  150. bg_button.grid(row=0, column=6, padx=5)
  151.  
  152.  
  153. hand_line()
  154. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment