How to do a screenshot of a tkinter application?

前端 未结 1 1001
清歌不尽
清歌不尽 2020-12-18 12:27

I need to do a screenshot of the content of the tkinter application below. I am on Windows 7 (or 8).

from Tkinter import *

def test(x):    
    #print \"I\'         


        
相关标签:
1条回答
  • 2020-12-18 13:01

    Since you mentioned that you are on Windows. You can use the Win32 API as directed in this answer Fastest way to take a screenshot with python on windows. Hope this helps.


    But actually Pyscreenshot should be what you are looking for.

    Take the following code for example:

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

    As you can see you can use bbox to take screenshot that is at co-ordinates (100, 200) and has a width of 300 and a height of 400.

    Also as regards the printing check out Printing using win32api. I hope these help.

    Using PIL you can do a resize:

    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)
    

    I would advise changing your program so that you can resize the image before printing

    0 讨论(0)
提交回复
热议问题