1+ # @autor Magno Efren
2+ # Youtube: https://www.youtube.com/c/MagnoEfren
3+ # GitHub: https://github.com/MagnoEfren
4+
5+ from tkinter import Canvas , Tk , Frame , Button ,messagebox , filedialog , Scale , HORIZONTAL ,ALL
6+ import PIL .ImageGrab as ImageGrab
7+
8+ linea_x = 0
9+ linea_y = 0
10+ color = 'black'
11+
12+ def linea_xy (event ):
13+ global linea_x
14+ global linea_y
15+
16+ linea_x = event .x
17+ linea_y = event .y
18+
19+
20+ def linea (event ):
21+ global linea_x , linea_y
22+ canvas .create_line ((linea_x , linea_y , event .x , event .y ), fill = color , width = espesor_pincel .get ())
23+ linea_x = event .x
24+ linea_y = event .y
25+
26+
27+ def mostrar_color (nueva_color ):
28+ global color
29+ color = nueva_color
30+
31+
32+ def borrar ():
33+ global color
34+ color = 'White'
35+
36+
37+ def limpiar ():
38+ canvas .delete (ALL )
39+
40+
41+ def salir ():
42+ ventana .destroy ()
43+ ventana .quit ()
44+
45+
46+
47+ def guardar_dibujo ():
48+
49+ try :
50+ filename = filedialog .asksaveasfilename (defaultextension = '.png' )
51+
52+ x = ventana .winfo_rootx () + canvas .winfo_x ()
53+ y = (ventana .winfo_rooty () + canvas .winfo_y ())
54+
55+ x1 = x + canvas .winfo_width ()
56+ y1 = y + canvas .winfo_height ()
57+
58+ ImageGrab .grab ().crop ((x , y , x1 , y1 )).save (filename )
59+ messagebox .showinfo ('Guardar Dibujo' ,'Imagen guardada en: ' + str (filename ) )
60+ except :
61+ messagebox .showerror ('Guardar Dibujo' , 'Imagen no guardada\n Error' )
62+
63+
64+
65+ ventana = Tk ()
66+ ventana .state ('zoomed' )
67+ ventana .config (bg = 'black' )
68+ ventana .title ('Dibujar' )
69+ ventana .iconbitmap ('icono_dibujo.ico' )
70+
71+ ventana .rowconfigure (0 , weight = 1 )
72+ ventana .columnconfigure (0 , weight = 1 )
73+
74+ # frame principal comandos y canvas de dibujo
75+
76+ frame = Frame (ventana , bg = 'black' , height = 200 )
77+ frame .grid (column = 0 , row = 0 , sticky = 'ew' )
78+
79+ frame .columnconfigure (0 , minsize = 200 , weight = 1 )
80+
81+
82+ # canvas de dibujo
83+ canvas = Canvas (ventana , height = 660 , bg = 'white' )
84+ canvas .grid (row = 1 ,column = 0 , sticky = 'nsew' )
85+
86+
87+ canvas .rowconfigure (0 ,weight = 1 )
88+ canvas .columnconfigure (0 , weight = 1 , minsize = 100 )
89+
90+ canvas .bind ('<Button-1>' , linea_xy )
91+ canvas .bind ('<B1-Motion>' , linea )
92+
93+
94+ # Canvas para colores
95+
96+ canvas_colores = Canvas (frame , bg = 'black' , width = 5 , height = 40 )
97+ canvas_colores .grid (column = 0 , row = 0 , sticky = 'ew' , padx = 1 , pady = 1 )
98+
99+
100+ id = canvas_colores .create_rectangle ((10 ,10 ,30 ,30 ),fill = 'red' )
101+ canvas_colores .tag_bind (id , '<Button-1>' , lambda x : mostrar_color ('red' ))
102+
103+ id = canvas_colores .create_rectangle ((40 ,10 ,60 ,30 ),fill = 'green' )
104+ canvas_colores .tag_bind (id , '<Button-1>' , lambda x : mostrar_color ('green' ))
105+
106+ id = canvas_colores .create_rectangle ((70 ,10 ,90 ,30 ),fill = 'yellow' )
107+ canvas_colores .tag_bind (id , '<Button-1>' , lambda x : mostrar_color ('yellow' ))
108+
109+ id = canvas_colores .create_rectangle ((100 ,10 ,120 ,30 ),fill = 'magenta' )
110+ canvas_colores .tag_bind (id , '<Button-1>' , lambda x : mostrar_color ('magenta' ))
111+
112+ id = canvas_colores .create_rectangle ((130 ,10 ,150 ,30 ),fill = 'blue' )
113+ canvas_colores .tag_bind (id , '<Button-1>' , lambda x : mostrar_color ('blue' ))
114+
115+ id = canvas_colores .create_rectangle ((160 ,10 ,180 ,30 ),fill = 'orange' )
116+ canvas_colores .tag_bind (id , '<Button-1>' , lambda x : mostrar_color ('orange' ))
117+
118+ id = canvas_colores .create_rectangle ((190 ,10 ,210 ,30 ),fill = 'salmon' )
119+ canvas_colores .tag_bind (id , '<Button-1>' , lambda x : mostrar_color ('salmon' ))
120+
121+ id = canvas_colores .create_rectangle ((220 ,10 ,240 ,30 ),fill = 'sky blue' )
122+ canvas_colores .tag_bind (id , '<Button-1>' , lambda x : mostrar_color ('sky blue' ))
123+
124+ id = canvas_colores .create_rectangle ((250 ,10 ,270 ,30 ),fill = 'gold' )
125+ canvas_colores .tag_bind (id , '<Button-1>' , lambda x : mostrar_color ('gold' ))
126+
127+ id = canvas_colores .create_rectangle ((280 ,10 ,300 ,30 ),fill = 'hot pink' )
128+ canvas_colores .tag_bind (id , '<Button-1>' , lambda x : mostrar_color ('hot pink' ))
129+
130+ id = canvas_colores .create_rectangle ((310 ,10 ,330 ,30 ),fill = 'bisque' )
131+ canvas_colores .tag_bind (id , '<Button-1>' , lambda x : mostrar_color ('bisque' ))
132+
133+ id = canvas_colores .create_rectangle ((340 ,10 ,360 ,30 ),fill = 'brown4' )
134+ canvas_colores .tag_bind (id , '<Button-1>' , lambda x : mostrar_color ('brown4' ))
135+
136+ id = canvas_colores .create_rectangle ((370 ,10 ,390 ,30 ),fill = 'gray' )
137+ canvas_colores .tag_bind (id , '<Button-1>' , lambda x : mostrar_color ('gray' ))
138+
139+ id = canvas_colores .create_rectangle ((400 ,10 ,420 ,30 ),fill = 'purple' )
140+ canvas_colores .tag_bind (id , '<Button-1>' , lambda x : mostrar_color ('purple' ))
141+
142+ id = canvas_colores .create_rectangle ((430 ,10 ,450 ,30 ),fill = 'green2' )
143+ canvas_colores .tag_bind (id , '<Button-1>' , lambda x : mostrar_color ('green2' ))
144+
145+ id = canvas_colores .create_rectangle ((460 ,10 ,480 ,30 ),fill = 'dodger blue' )
146+ canvas_colores .tag_bind (id , '<Button-1>' , lambda x : mostrar_color ('dodger blue' ))
147+
148+ id = canvas_colores .create_rectangle ((490 ,10 ,510 ,30 ),fill = 'black' )
149+ canvas_colores .tag_bind (id , '<Button-1>' , lambda x : mostrar_color ('black' ))
150+
151+ # botones y scale de control
152+
153+ espesor_pincel = Scale (frame , orient = HORIZONTAL , from_ = 0 , to = 50 , length = 200 ,relief = 'groove' , bg = 'gold' , width = 17 , sliderlength = 20 , highlightbackground = 'white' ,activebackground = 'red' )
154+ espesor_pincel .set (1 )
155+ espesor_pincel .grid (column = 1 , row = 0 , sticky = 'ew' , pady = 1 , padx = 2 )
156+
157+
158+ bt_guardar = Button (frame , text = 'Guardar' , bg = 'green2' , command = guardar_dibujo , width = 10 , height = 2 ,activebackground = 'white' , font = ('Comic sens MS' ,10 ,'bold' ))
159+ bt_guardar .grid (column = 2 , row = 0 , sticky = 'ew' ,pady = 1 ,padx = 4 )
160+
161+ bt_borrar = Button (frame , text = 'Borrar' , bg = 'cyan2' , command = borrar , width = 10 , height = 2 ,activebackground = 'white' ,font = ('Comic sens MS' ,10 ,'bold' ))
162+ bt_borrar .grid (column = 3 , row = 0 , sticky = 'ew' , pady = 1 , padx = 4 )
163+
164+
165+ bt_limpiar = Button (frame , text = 'Limpiar' , bg = 'violet red' , command = limpiar , width = 10 , height = 2 ,activebackground = 'white' ,font = ('Comic sens MS' ,10 ,'bold' ))
166+ bt_limpiar .grid (column = 4 , row = 0 , sticky = 'ew' , pady = 1 , padx = 4 )
167+
168+ bt_salir = Button (frame , text = 'Salir' , bg = 'firebrick1' , command = salir , width = 10 , height = 2 , activebackground = 'white' ,font = ('Comic sens MS' ,10 ,'bold' ))
169+ bt_salir .grid (column = 5 , row = 0 , sticky = 'ew' ,pady = 1 , padx = 4 )
170+
171+
172+ ventana .mainloop ()
0 commit comments