Is trying to do a more perfect drawing software , Planned use tkinter In the library canvas Control , Then, we are working on the location of graphic items , coordinate , Storage and loading of size and other attribute items , You just need coordinates , Knowledge of location , So it's a byproduct of this drawing software , This is a blog post , Share it with you , Study together , Looking forward to bringing inspiration and help to your work .
Remain true to our original aspiration , We have to always , Let's encourage each other !
well , get down to business , Return to the theme of this blog post :python tkinter preservation canvas Three methods of drawing canvas
Method 1 :
from tkinter import * from PIL import ImageGrab def getter(widget):
widget.update() x=root.winfo_rootx()+widget.winfo_x()
y=root.winfo_rooty()+widget.winfo_y() x1=x+widget.winfo_width()
y1=y+widget.winfo_height() ImageGrab.grab().crop((x,y,x1,y1)).save("first.jpg")
root = Tk() cv = Canvas(root) cv.create_rectangle(10,10,50,50)
cv.create_line([0, 10, 100, 100], fill='green') cv.pack() getter(cv)
root.mainloop()
The operation effect is as follows :
Method 2 :
from tkinter import * import win32gui from PIL import ImageGrab
# Then there is access Canvas The realization of def CaptureScreen(): HWND = win32gui.GetFocus() # Gets the current window handle
rect=win32gui.GetWindowRect(HWND) # Get current window coordinates im=ImageGrab.grab(rect) # Capture target image
im.save("second.jpeg",'jpeg') # The previous parameter is the save path , The next parameter is the save format root = Tk() cv =
Canvas(root, width=300, height=150) cv.create_rectangle(10,10,50,50) cv.pack()
b = Button(root, text=' screenshot ', command=CaptureScreen) b.pack() mainloop()
The operation effect is as follows :
You can find the time of the screenshot through the figure above , hold root The whole screen is captured , But the following part is not what we need , that , We are right RECT The script is as follows :
from tkinter import * import win32gui from PIL import ImageGrab def
CaptureScreen(): HWND = win32gui.GetFocus() rect=win32gui.GetWindowRect(HWND) x
= rect[0] x1=x+cv.winfo_width() y = rect[1] y1=y+cv.winfo_height()
im=ImageGrab.grab((x,y,x1,y1)) im.save("second.jpeg",'jpeg') root = Tk() cv =
Canvas(root, width=300, height=150) cv.pack() cv.create_rectangle(10,10,50,50)
cv.update() b = Button(root, text=' screenshot ', command=CaptureScreen) b.pack()
root.mainloop()
The operation effect is as follows :
Method 3 : Direct use ImageDraw Image generation .
from tkinter import * from PIL import Image, ImageDraw width = 400 height =
300 center = height//2 white = (222, 222, 220) green = (0,128,0) root = Tk() cv
= Canvas(root, width=width, height=height, bg='white') cv.pack() image1 =
Image.new("RGB", (width, height), white) draw = ImageDraw.Draw(image1)
cv.create_line([0, center, width, center], fill='green') draw.line([0, center,
width, center], green) filename = "third.jpg" image1.save(filename)
root.mainloop()
The operation effect is as follows :
well , This blog is finished , Actually, it's mainly right canvas Take a screenshot , Then save it as a picture , The basic principle is relatively simple , This function will be applied in my subsequent drawing software , Coming soon ! Thank you for your support !
Technology