当Tkinter窗口不在顶层时,如何对其进行快照?

时间:2020-04-07 06:37:18

标签: python tkinter screenshot

我正在尝试使用Tkinter制作一个窗口的屏幕快照,但是当该窗口不在顶层时,我只会看到屏幕上显示的内容。 我宁愿拥有该窗口本身的屏幕截图,即使它上面还有其他窗口也是如此。

这是一个最小的例子。我只是做了一个使用ImageGrab保存屏幕截图的功能。 单击File.screenshot或单击hello按钮时调用的函数。该功能使用了2秒的暂停,就像我有时间在Tkinter顶部放置另一个窗口一样。

我最后想要的是Tkinter窗口的图像,而不是屏幕的图像。例如,如果我在顶部放置另一个窗口,则会得到:enter image description here

   from tkinter import *
   from PIL import ImageGrab
   import time
   top = Tk()
   top.geometry("600x400")
   def helloCallBack():
          time.sleep(2)
          x = top.winfo_rootx()
          y = top.winfo_rooty()
          xx = x + top.winfo_width()
          yy = y + top.winfo_height()
          print(x,y,xx,yy)
          ImageGrab.grab(bbox=(x, y, xx, yy)).save("screenImage2.jpg")

   menuBar = Menu(top)
   menuFile = Menu(menuBar, tearoff=0)
   menuFile.add_command(label="Screenshot", command=helloCallBack)
   menuBar.add_cascade( label="File", menu=menuFile)
   top.config(menu = menuBar)
   B = Button(top, text = "Hello", command = helloCallBack)
   B.place(x = 50,y = 50)
   top.mainloop()

0 个答案:

没有答案
相关问题