I have the following code I wrote on windows 7:

canvas_width = 150
canvas_height = 150

brush_size = 3
color = "black"

def paint(event):
        global brush_size
        global color
        x1 = event.x - brush_size
        x2 = event.x + brush_size
        y1 = event.y - brush_size
        y2 = event.y + brush_size
        w.create_oval(x1, y1, x2, y2,
                      fill = color,
                      outline=color)

    def getter(widget):
        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("img1.jpg")

    root = Tk()
    w = Canvas(root,
               width=canvas_width,
               height=canvas_height,
               bg="white")
    w.bind("<B1-Motion>", paint)

    save_btn = Button(text="Save", width=10, command=getter(w))

    w.grid(row=2, column=0,
           columnspan=7,
           padx=5, pady=5,
           sticky=E+W+S+N)
    w.columnconfigure(6, weight=1)
    w.rowconfigure(2, weight=1)

    save_btn.grid(row=0, column=1)

    root.mainloop()

And it works fine on windows 7. But I ran the same code on macos Sierra and something went wrong. When I try to save the canvas, I get an image that starts from a window title bar. When I move the window to the center of screen and hit 'save' I get an image of the center of screen, not the window canvas. And when I try to save the canvas on windows 7 - I get an image of canvas no matter where I move the window. What the hell? How do I fix the code to save the canvas only? On mac?

That code doesn't run here on W10 either. Is there more code? That is, it looks to be an incomplete code dump.

from Tkinter import *
from PIL import ImageGrab

canvas_width = 150
canvas_height = 150

brush_size = 3
color = "black"

def paint(event):
    global brush_size
    global color
    x1 = event.x - brush_size
    x2 = event.x + brush_size
    y1 = event.y - brush_size
    y2 = event.y + brush_size
    w.create_oval(x1, y1, x2, y2,
                  fill=color,
                  outline=color)

def getter(widget):
    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("img1.jpg")

root = Tk()
root.title("Paint")

w = Canvas(root,
           width=canvas_width,
           height=canvas_height,
           bg="blue")
w.bind("<B1-Motion>", paint)

clear_btn = Button(text="Clear", width=10, command=lambda: w.delete("all"))
save_btn = Button(text="Save", width=10, command=lambda: getter(w))

w.grid(row=2, column=0,
       columnspan=7,
       padx=5, pady=5,
       sticky=E+W+S+N)

w.columnconfigure(6, weight=1)
w.rowconfigure(2, weight=1)

clear_btn.grid(row=0, column=2)
save_btn.grid(row=0, column=1)

root.mainloop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.