我在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()
它在Windows 7上工作正常。但我在macos Sierra上运行相同的代码,出了点问题。当我尝试保存画布时,我得到一个从窗口标题栏开始的图像。当我将窗口移动到屏幕中心并点击“保存”时,我得到一个屏幕中心的图像,而不是窗口画布。当我尝试在Windows 7上保存画布时 - 无论我在哪里移动窗口,我都会得到一个画布图像。我勒个去?如何修复代码以仅保存画布?在mac?