M
M
MaksaTaksa2019-04-14 05:28:23
Python
MaksaTaksa, 2019-04-14 05:28:23

How to save drawn picture in graphical application in tkinter (python) module?

Hello, I found on the Internet such a code that opens self-made paint in a separate window, with only one brush and color. The problem arose when it became necessary to save what was drawn, or integrate this very paint into another code, and select a drawing for use, because it will be used in the neural network to test it, here is the code itself ...
from tkinter import *
from tkinter.colorchooser import askcolor
class Paint(object):
DEFAULT_PEN_SIZE = 10.0
DEFAULT_COLOR = 'black'
def __init__(self):
self.root = Tk() self.choose_size_button = Scale(self.root, from_=1, to=10, orient=HORIZONTAL) self.choose_size_button.grid(row=0, column=4)
self.pen_button = Button(self.root, text='pen', command=self.use_pen)
self.pen_button.grid(row=0, column=0)
self.c = Canvas(self.root, bg='white', width=600, height=600)
self.c.grid(row=1, columnspan=5)
self.setup()
self.root.mainloop()
def setup(self):
self.old_x = None
self.old_y = None
self.line_width = self.choose_size_button.get()
self.color = self.DEFAULT_COLOR
self.eraser_on = False
self.active_button = self.pen_button
self.c. bind('', self.paint) self.activate_button(self.pen_button) def activate_button(self, some_button, eraser_mode=False): self.active_button.config(relief=RAISED) some_button.config(relief=SUNKEN)
self.c.bind ('', self.reset)
def use_pen(self):
self.active_button = some_button
self.eraser_on = eraser_mode
def paint (self, event):
self.line_width = self.choose_size_button.get()
paint_color = 'white' if self.eraser_on else self.color
if self.old_x and self.old_y:
self.c.create_line(self.old_x, self .old_y, event.x, event.y,
width=self.line_width, fill=paint_color,
capstyle=ROUND, smooth=TRUE, splinesteps=36)
self.old_x = event.x
self.old_y = event.y
def reset(self, event):
self.old_x, self.old_y = None, None
if __name__ == '__main__':
Paint()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2019-04-14
@NeiroNx

you can use PIL.ImageGrab
to take a screenshot of the widget.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question