tkinter 屏幕截图,其中 mss 未按预期工作

问题描述 投票:0回答:1

本质上问题是:我想截取我的 tkinter 画布的屏幕截图。 不幸的是,我无法理解 PIL,所以请不要用 Pillow 或 ImageGrab 来气我。

from mss import mss

def test_shot(self):
    with mss() as sct:
        filename = sct.shot(mon=1, output="E:/project/screenshots/test_output.png")

所以这实际上截取了整个屏幕的屏幕截图,从某种意义上说,这是次优且生硬的,但它并没有截屏 tkinter gui(root:frame:canvas;labels,...) 而是截屏了我背后的内容tkinter gui(空闲和控制台中的代码)。

所以基本上我的问题是: 1 有没有办法截取整个画布或部分画布的屏幕截图? 第二,为什么屏幕截图显示除我的程序之外的所有内容? enter image description here

编辑: 这就是我出于测试目的调用该函数的方式: shortened code

python tkinter screenshot tkinter-canvas python-mss
1个回答
0
投票

您可以使用

.grab()
来截取部分屏幕:

from mss import mss, tools

...

def test_shot(self):
    with mss() as sct:
        # self.graphs is the canvas object
        monitor = {
            "top": self.graphs.winfo_rooty(),
            "left": self.graphs.winfo_rootx(),
            "width": self.graphs.winfo_width(),
            "height": self.graphs.winfo_height(),
            "mon": 1
        }
        # grab the image of the specified region
        img = sct.grab(monitor)
        # save the image to file
        tools.to_png(img.rgb, img.size, output="test_output.png")
© www.soinside.com 2019 - 2024. All rights reserved.