Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_Paint_Demo.py
- import tkinter as tk
- import tkinter.ttk as ttk
- COLORS = '''
- black
- gray
- light gray
- white
- navy
- medium blue
- royal blue
- blue
- deep sky blue
- sky blue
- light sky blue
- light blue
- turquoise
- cyan
- aquamarine
- dark green
- lawn green
- forest green
- khaki
- yellow
- gold
- indian red
- orange
- dark orange
- red
- pink
- light pink
- maroon
- purple
- medium purple
- '''.splitlines()[1:-1]
- GRAYS = '''
- gray1
- gray2
- gray3
- gray4
- gray5
- gray6
- gray7
- gray8
- gray9
- gray10
- gray11
- gray12
- gray13
- gray14
- gray15
- gray16
- gray17
- gray18
- gray19
- gray20
- gray21
- gray22
- gray23
- gray24
- gray25
- gray26
- gray27
- gray28
- gray29
- gray30
- gray31
- gray32
- gray33
- gray34
- gray35
- gray36
- gray37
- gray38
- gray39
- gray40
- gray42
- gray43
- gray44
- gray45
- gray46
- gray47
- gray48
- gray49
- gray50
- gray51
- gray52
- gray53
- gray54
- gray55
- gray56
- gray57
- gray58
- gray59
- gray60
- gray61
- gray62
- gray63
- gray64
- gray65
- gray66
- gray67
- gray68
- gray69
- gray70
- gray71
- gray72
- gray73
- gray74
- gray75
- gray76
- gray77
- gray78
- gray79
- gray80
- gray81
- gray82
- gray83
- gray84
- gray85
- gray86
- gray87
- gray88
- gray89
- gray90
- gray91
- gray92
- gray93
- gray94
- gray95
- gray97
- gray98
- gray99
- '''.splitlines()[1:-1]
- class DrawApp(tk.Tk):
- def __init__(self):
- super().__init__()
- self.title("Tk Paint Demo")
- self._xold = None
- self._yold = None
- self.canvas = None
- self.color = "Black"
- self.thickness = 1
- self.tag = ["tag", "0"] #Don't know why but tags can't be just a number
- self._create_widgets()
- def _create_widgets(self):
- topframe = tk.Frame(self)
- topframe.grid(row=0, column=0, pady=10)
- self.col_select = tk.StringVar()
- colorList = ttk.Combobox(topframe, textvariable=self.col_select, values=COLORS, state="readonly", width=10)
- colorList.current(0)
- self.option_add('*TCombobox*Listbox.selectBackground', 'skyblue')
- colorList.bind('<<ComboboxSelected>>', self._change_color)
- colorList.grid(row=0, column=0, padx=5)
- self.t_select = tk.StringVar()
- tList = ttk.Combobox(topframe, textvariable=self.t_select, values=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], state="readonly", width=2)
- tList.current(0)
- tList.bind('<<ComboboxSelected>>', self._change_thickness)
- tList.grid(row=0, column=1, padx=5)
- tk.Button(topframe, text="Undo", bg="blue", fg="white", activebackground="blue4", activeforeground="white", command=self._undo).grid(row=0, column=2, padx=5)
- tk.Button(topframe, text="Clear", bg="brown", fg="white", activebackground="brown4", activeforeground="white", command=self._clear).grid(row=0, column=3, padx=5)
- self.canvas = tk.Canvas(self, width=400, height=400, bg='white')
- self.canvas.grid(row=1, column=0, padx=10, pady=(0,10))
- self.canvas.bind("<ButtonRelease-1>", self._on_up)
- self.canvas.bind("<B1-Motion>", self._on_motion)
- def _change_color(self, event=None):
- self.color = self.col_select.get()
- def _change_thickness(self, event=None):
- self.thickness = int(self.t_select.get())
- def _clear(self):
- self.canvas.delete("all")
- self.tag = ["tag", "0"]
- def _undo(self):
- cur_tag = int(self.tag[1])
- if cur_tag >= 1:
- self.canvas.delete("tag"+str(cur_tag-1))
- self.tag = ["tag", str(cur_tag - 1)]
- def _on_up(self, event):
- self._xold = None
- self._yold = None
- self.tag = ["tag", str(int(self.tag[1])+1)]
- def _on_motion(self, event):
- tag = "".join(self.tag)
- x1, y1 = (event.x - self.thickness), (event.y - self.thickness)
- x2, y2 = (event.x + self.thickness), (event.y + self.thickness)
- event.widget.create_oval(x1, y1, x2, y2, width=0, fill=self.color, tag=tag)
- if self._xold is not None and self._yold is not None:
- self.canvas.create_oval(x1, y1, x2, y2, width=0, fill=self.color, tag=tag)
- self.canvas.create_line(self._xold, self._yold, event.x, event.y, smooth=True, width=2*self.thickness, fill=self.color, tag=tag)
- # here's where you draw it. smooth. neat.
- self._xold = event.x
- self._yold = event.y
- DrawApp().mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement