A library for writing GUI programs in Python Tkinter. In this library, you can draw a picture using the Canvas inside.
I think that every human being has the opportunity to draw a picture using Canvas, but I couldn't find out clearly by examining the program that acquires the information written in Canvas at set intervals. I will write a program that also serves as a memorandum.
By the way, the program itself to draw a picture using Canvas referred to the following site. Draw a line on Python canvas with mouse
#!/usr/bin/env python
# -*- coding: utf8 -*-
import tkinter
import time
from PIL import Image, ImageDraw
class Application(tkinter.Frame):
	def __init__(self, master=None):
		super().__init__(master)
		self.master = master
		self.master.title('Measure the time to draw')
		self.create_variables()
		self.create_widgets()
		self.pack()
	def create_widgets(self):
		self.draw_canvas = tkinter.Canvas(self, bg='white', width=self.img_size[0], height=self.img_size[1])
		self.draw_canvas.grid(row=1, column=0, columnspan=4)
		self.draw_canvas.bind('<Button-1>', self.start)
		self.draw_canvas.bind('<B1-Motion>', self.paint)
		self.draw_canvas.bind('<ButtonRelease-1>', self.reset)
	
	def create_variables(self):
		self.img_size = (600, 600)
		self.old_x, self.old_y = None, None
		self.color = 'black'
		self.pen_width = 5.0
		self.im = Image.new('RGB', self.img_size, 'white')
		self.draw = ImageDraw.Draw(self.im)
		
		self.tick_time = 30
		self.timer_id = None
		self.start_time = None
		
	#methods
	def start(self, event):
		if self.timer_id is None:
			self.timer_start()
		self.draw_canvas.create_line(event.x, event.y, event.x, event.y, width=self.pen_width, fill=self.color, capstyle="round", smooth=True, splinesteps=36)
		self.draw.line((event.x, event.y, event.x, event.y), fill=self.color, width=int(self.pen_width))
		self.old_x, self.old_y = event.x, event.y
	
	def paint(self, event):
		if self.old_x and self.old_y:
			self.draw_canvas.create_line(self.old_x, self.old_y, event.x, event.y, width=self.pen_width, fill=self.color, capstyle="round", smooth=True, splinesteps=36)
			self.draw.line((self.old_x, self.old_y, event.x, event.y), fill=self.color, width=int(self.pen_width))
		self.old_x, self.old_y = event.x, event.y
	def reset(self, event):
		self.old_x, self.old_y = None, None
		self.timer_reset()
		
	#timer
	def timer_start(self):
		if self.timer_id is not None:
			self.after_cancel(self.timer_id)
		self.start_time = time.time()
		self.timer_id = self.after(self.tick_time, self.timer_count)
	
	def timer_count(self):
		t = time.time() - self.start_time
		x = self.old_x if self.old_x is not None else -1
		y = self.old_y if self.old_y is not None else -1
		print("elapsed time:%f ,X position%d,Y position%d" % (t, x, y))
		self.timer_id = self.after(self.tick_time, self.timer_count)
	
	def timer_reset(self):
		if self.timer_id is None:
			return
		self.after_cancel(self.timer_id)
		self.timer_id = None
if __name__ == "__main__":
	root = tkinter.Tk()
	app = Application(master=root)
	app.mainloop()
When you run it, you will see a canvas for drawing. When I draw a picture on this canvas with the mouse, it becomes a terminal

Functions related to timer behavior are defined by _timer_start () _, _timer_count () _, _timer_reset () _. These functions are called in response to _start () _, _reset () _ bound to Canvas, and now _timer_count () _ is called every time defined in the tick_time variable by the _after () _ function. It is supposed that the drawing information of
It was my first article writing.
I was just stuck in writing letters and investigating how to write them, so I hope this program will be useful in situations where you want to get handwritten online information.
Draw a line on Python canvas with mouse [tkinter - .after() - RIP Tutorial] (https://riptutorial.com/ja/tkinter/example/22870/-after--)
Recommended Posts