Advertisement
Chl_Snt

tkinter code

Mar 9th, 2024
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.37 KB | None | 0 0
  1. from pprint import pprint
  2. from tkinter import *
  3.  
  4. root = Tk()
  5. root.geometry("600x400")
  6. root.title("Рисовалка")
  7. root['bg'] = "gray75"
  8. root.resizable(False, False)
  9.  
  10. cv = Canvas(root, width=540, height=400, bg="white")
  11. cv.grid(row=0, column=0, rowspan=7)
  12.  
  13. state = "circle"
  14. brush = 10
  15. color = "red"
  16.  
  17.  
  18. def choose(button_name):
  19.     global state
  20.     state = button_name
  21.  
  22.  
  23. def paint(event):
  24.     if event.widget.__class__ is not Canvas:
  25.         print("Ты нажал не на Холст!")
  26.         return
  27.     if state == "circle":
  28.         cv.create_oval(event.x - brush,  # Координата x1
  29.                        event.y - brush,  # Координата y1
  30.                        event.x + brush,  # Координата x2
  31.                        event.y + brush,  # Координата y2
  32.                        fill=color,  # Цвет заливки
  33.                        outline=color)  # Цвет обводки
  34.     elif state == "square":
  35.         cv.create_rectangle(event.x - brush, event.y - brush,
  36.                             event.x + brush, event.y + brush,
  37.                             fill=color, outline=color)
  38.     elif state == "line1":
  39.         cv.create_line(event.x - brush, event.y - brush,
  40.                        event.x + brush, event.y + brush, fill=color)
  41.     elif state == "line2":
  42.         cv.create_line(event.x + brush, event.y - brush,
  43.                        event.x - brush, event.y + brush, fill=color)
  44.  
  45.     pprint(event.__dict__)
  46.  
  47.  
  48. root.bind_all("<1>", paint)
  49. root.bind_all("<B1-Motion>", paint)
  50.  
  51. square_btn = Button(root, text="🟥", font=(None, 20), command=lambda: choose("square"))
  52. square_btn.grid(row=0, column=1)
  53. circle_btn = Button(root, text="🔴", font=(None, 20), command=lambda: choose("circle"))
  54. circle_btn.grid(row=1, column=1)
  55. line1_btn = Button(root, text=" ↘ ", font=(None, 20), command=lambda: choose("line1"))
  56. line1_btn.grid(row=2, column=1)
  57. line2_btn = Button(root, text=" ↙ ", font=(None, 20), command=lambda: choose("line2"))
  58. line2_btn.grid(row=3, column=1)
  59.  
  60. plus_btn = Button(root, text="➕", font=(None, 20), command=lambda: choose("plus"))
  61. plus_btn.grid(row=4, column=1)
  62. minus_btn = Button(root, text="➖", font=(None, 20), command=lambda: choose("minus"))
  63. minus_btn.grid(row=5, column=1)
  64. size = Label(root, text=brush, fg=color, font=(None, 32))
  65. size.grid(row=6, column=1)
  66.  
  67. root.mainloop()
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement