Advertisement
here2share

# Tk_Paint_Demo.py

Apr 2nd, 2021
1,030
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.91 KB | None | 0 0
  1. # Tk_Paint_Demo.py
  2.  
  3. import tkinter as tk
  4. import tkinter.ttk as ttk
  5.  
  6. COLORS = '''
  7. black
  8. gray
  9. light gray
  10. white
  11. navy
  12. medium blue
  13. royal blue
  14. blue
  15. deep sky blue
  16. sky blue
  17. light sky blue
  18. light blue
  19. turquoise
  20. cyan
  21. aquamarine
  22. dark green
  23. lawn green
  24. forest green
  25. khaki
  26. yellow
  27. gold
  28. indian red
  29. orange
  30. dark orange
  31. red
  32. pink
  33. light pink
  34. maroon
  35. purple
  36. medium purple
  37. '''.splitlines()[1:-1]
  38.    
  39. GRAYS = '''
  40. gray1
  41. gray2
  42. gray3
  43. gray4
  44. gray5
  45. gray6
  46. gray7
  47. gray8
  48. gray9
  49. gray10
  50. gray11
  51. gray12
  52. gray13
  53. gray14
  54. gray15
  55. gray16
  56. gray17
  57. gray18
  58. gray19
  59. gray20
  60. gray21
  61. gray22
  62. gray23
  63. gray24
  64. gray25
  65. gray26
  66. gray27
  67. gray28
  68. gray29
  69. gray30
  70. gray31
  71. gray32
  72. gray33
  73. gray34
  74. gray35
  75. gray36
  76. gray37
  77. gray38
  78. gray39
  79. gray40
  80. gray42
  81. gray43
  82. gray44
  83. gray45
  84. gray46
  85. gray47
  86. gray48
  87. gray49
  88. gray50
  89. gray51
  90. gray52
  91. gray53
  92. gray54
  93. gray55
  94. gray56
  95. gray57
  96. gray58
  97. gray59
  98. gray60
  99. gray61
  100. gray62
  101. gray63
  102. gray64
  103. gray65
  104. gray66
  105. gray67
  106. gray68
  107. gray69
  108. gray70
  109. gray71
  110. gray72
  111. gray73
  112. gray74
  113. gray75
  114. gray76
  115. gray77
  116. gray78
  117. gray79
  118. gray80
  119. gray81
  120. gray82
  121. gray83
  122. gray84
  123. gray85
  124. gray86
  125. gray87
  126. gray88
  127. gray89
  128. gray90
  129. gray91
  130. gray92
  131. gray93
  132. gray94
  133. gray95
  134. gray97
  135. gray98
  136. gray99
  137. '''.splitlines()[1:-1]
  138.  
  139. class DrawApp(tk.Tk):
  140.     def __init__(self):
  141.         super().__init__()
  142.         self.title("Tk Paint Demo")
  143.         self._xold = None
  144.         self._yold = None
  145.         self.canvas = None
  146.         self.color = "Black"
  147.         self.thickness = 1
  148.         self.tag = ["tag", "0"] #Don't know why but tags can't be just a number
  149.         self._create_widgets()
  150.  
  151.     def _create_widgets(self):
  152.         topframe = tk.Frame(self)
  153.         topframe.grid(row=0, column=0, pady=10)
  154.  
  155.         self.col_select = tk.StringVar()
  156.         colorList = ttk.Combobox(topframe, textvariable=self.col_select, values=COLORS, state="readonly", width=10)
  157.         colorList.current(0)
  158.         self.option_add('*TCombobox*Listbox.selectBackground', 'skyblue')
  159.         colorList.bind('<<ComboboxSelected>>', self._change_color)
  160.         colorList.grid(row=0, column=0, padx=5)
  161.  
  162.         self.t_select = tk.StringVar()
  163.         tList = ttk.Combobox(topframe, textvariable=self.t_select, values=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], state="readonly", width=2)
  164.         tList.current(0)
  165.         tList.bind('<<ComboboxSelected>>', self._change_thickness)
  166.         tList.grid(row=0, column=1, padx=5)
  167.  
  168.         tk.Button(topframe, text="Undo", bg="blue", fg="white", activebackground="blue4", activeforeground="white", command=self._undo).grid(row=0, column=2, padx=5)
  169.         tk.Button(topframe, text="Clear", bg="brown", fg="white", activebackground="brown4", activeforeground="white", command=self._clear).grid(row=0, column=3, padx=5)
  170.         self.canvas = tk.Canvas(self, width=400, height=400, bg='white')
  171.         self.canvas.grid(row=1, column=0, padx=10, pady=(0,10))
  172.         self.canvas.bind("<ButtonRelease-1>", self._on_up)
  173.         self.canvas.bind("<B1-Motion>", self._on_motion)
  174.  
  175.     def _change_color(self, event=None):
  176.         self.color = self.col_select.get()
  177.  
  178.     def _change_thickness(self, event=None):
  179.         self.thickness = int(self.t_select.get())
  180.  
  181.     def _clear(self):
  182.         self.canvas.delete("all")
  183.         self.tag = ["tag", "0"]
  184.  
  185.     def _undo(self):
  186.         cur_tag = int(self.tag[1])
  187.         if cur_tag >= 1:
  188.             self.canvas.delete("tag"+str(cur_tag-1))
  189.             self.tag = ["tag", str(cur_tag - 1)]
  190.  
  191.     def _on_up(self, event):
  192.         self._xold = None
  193.         self._yold = None
  194.         self.tag = ["tag", str(int(self.tag[1])+1)]
  195.  
  196.     def _on_motion(self, event):
  197.         tag = "".join(self.tag)
  198.         x1, y1 = (event.x - self.thickness), (event.y - self.thickness)
  199.         x2, y2 = (event.x + self.thickness), (event.y + self.thickness)
  200.         event.widget.create_oval(x1, y1, x2, y2, width=0, fill=self.color, tag=tag)
  201.         if self._xold is not None and self._yold is not None:
  202.             self.canvas.create_oval(x1, y1, x2, y2, width=0, fill=self.color, tag=tag)
  203.             self.canvas.create_line(self._xold, self._yold, event.x, event.y, smooth=True, width=2*self.thickness, fill=self.color, tag=tag)
  204.     # here's where you draw it. smooth. neat.
  205.         self._xold = event.x
  206.         self._yold = event.y
  207.  
  208. DrawApp().mainloop()
  209.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement