如何制作tkinter应用程序的屏幕截图?

2024-04-19 07:04:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要对下面的tkinter应用程序的内容做一个截图。我在Windows 7(或Windows 8)上。

from Tkinter import *

def test(x):    
    #print "I'm in event:", x
    if x == 1:            # if event on entry e1 
        print 'e1 event' # do some thing
    elif x == 2:            # also if event on entry e2    
        print 'e2 event'  # do some thing else
    else: 
        print 'no event' 

def test1(x):
    test(1)

def test2(x):
    test(2)


root=Tk()
root.minsize(500,500)
e1=Entry(root)
e1.pack()

e2=Entry(root)
e2.pack()

e1.bind( "<FocusOut>", test1) 
e2.bind( "<FocusOut>", test2)   
button=Button(root, text='print').pack(side=BOTTOM)

root.mainloop()

Tags: testeventifonwindowsdefsomeroot
1条回答
网友
1楼 · 发布于 2024-04-19 07:04:16

因为你提到你在Windows上。您可以按照此答案中的指示使用Win32 API。希望这有帮助。


但实际上Pyscreenshot应该是你想要的。

以下面的代码为例:

from pyscreenshot import grab

im = grab(bbox=(100, 200, 300, 400))
im.show()

如您所见,您可以使用bbox来拍摄位于坐标(100,200)处、宽度为300、高度为400的屏幕快照。

同样关于打印签出Printing using win32api。我希望这些能有帮助。

使用PIL,您可以调整大小:

from PIL import Image
from pyscreenshot import grab

img = grab(bbox=(100, 200, 300, 400))

# to keep the aspect ratio
w = 300
h = 400
maxheight = 600
maxwidth = 800
ratio = min(maxwidth/width, maxheight/height)
# correct image size is not #oldsize * ratio#

# img.resize(...) returns a resized image and does not effect img unless
# you assign the return value
img = img.resize((h * ratio, width * ratio), Image.ANTIALIAS)

我建议您更改程序,以便在打印之前调整图像的大小

相关问题 更多 >