使用Python图形模块:有没有办法将当前窗口保存为图像?

使用Python图形模块:有没有办法将当前窗口保存为图像?,python,graphics,Python,Graphics,我正在使用python模块。我试图做的是将当前窗口保存为图像。在模块中,有一个将“图像”保存为图像的选项(image.save())。但这并没有什么帮助,因为它只保存了一个已经加载的图像。或者,如果你像我一样加载一个空白图像,希望它能改变这种情况,惊喜,惊喜:你保存了一个空白图像。这是我的密码: from graphics import * w = 300 h = 300 anchorpoint=Point(150,150) height=300 width=300 image=Imag

我正在使用python模块。我试图做的是将当前窗口保存为图像。在模块中,有一个将“图像”保存为图像的选项(image.save())。但这并没有什么帮助,因为它只保存了一个已经加载的图像。或者,如果你像我一样加载一个空白图像,希望它能改变这种情况,惊喜,惊喜:你保存了一个空白图像。这是我的密码:

from graphics import *


w = 300
h = 300

anchorpoint=Point(150,150)
height=300
width=300

image=Image(anchorpoint, height, width) #creates a blank image in the background

win = GraphWin("Red Circle", w, h)
# circle needs center x, y coordinates and radius
center = Point(150, 150)
radius = 80
circle = Circle(center, radius)
circle.setFill('red')
circle.setWidth(2)
circle.draw(win)
point= circle.getCenter()

print point
pointx= point.getX()
pointy= point.getY()
print pointx
print pointy

findPixel=image.getPixel(150,150)
print findPixel
image.save("blank.gif")

# wait, click mouse to go on/exit
win.getMouse()
win.close()

#######that's it#####
我的问题是:如何将屏幕上的内容保存为“blank.gif”
谢谢

您正在绘制的对象基于Tkinter。我不相信您实际上是在基础图像上绘制,而是简单地使用“图形”库创建Tkinter对象。我也不相信你能把一个Tkinter保存到一个“gif”文件中,尽管你肯定能用postscript格式保存它们,然后把它们转换成gif格式

为此,您需要python的
PIL

如果所有对象实际上都是TKinter对象,则只需保存这些对象即可

首先替换这一行代码:

image.save("blank.gif")
以下是:

# saves the current TKinter object in postscript format
win.postscript(file="image.eps", colormode='color')

# Convert from eps format to gif format using PIL
from PIL import Image as NewImage
img = NewImage.open("image.eps")
img.save("blank.gif", "gif")
如果您需要其他信息,请查看-这是我得到建议代码的地方

我肯定有比save/convert更优雅的解决方案,但因为我对TKinter了解不多,所以这是我找到的唯一方法

希望有帮助