自动在顶部重新强制 Tkinter 画布全屏图像(Windows 10、Python)

Automatically Re-Forcing Tkinter Canvas Fullscreen Image on Top (Windows 10, Python)

提问人:javery 提问时间:9/16/2023 更新时间:9/16/2023 访问量:23

问:

目标是以无边框、全屏显示图像,无论计算机上发生什么,都能保持其焦点。该程序在我们的大厅中显示客户偶尔看到的图像,因此即使发生奇怪的事情,也要将焦点集中在图像上。到目前为止,计算机更新消息和随机文件系统错误已经弹出并位于其之上。不幸的是,没有人可以检查它以手动将焦点恢复。无论如何,这将是一个糟糕的解决方案。

因此,问题在于程序在显示图像时卡在行上运行,因此我不确定如何运行任何其他代码来检查和处理显示图像后出现在顶部的其他窗口。popup.mainloop()

这是我到目前为止所拥有的,它只是全屏打开图像。 我尝试在之前和之后添加一个循环,但都没有奏效。 我也看到了这篇文章,但无法构建一些重新聚焦窗口的东西。popup.lift()popup.mainloop()

import pyautogui as pa
import datetime
import tkinter
from PIL import Image, ImageTk

def showPIL(pilImage):
    popup = tkinter.Tk()
    w, h = popup.winfo_screenwidth(), popup.winfo_screenheight()
    popup.attributes('-fullscreen',True) 
    popup.geometry("%dx%d+0+0" % (w, h))
    popup.focus_set()

    # Want to be able to exit on command
    popup.bind("<Escape>", lambda e: (e.widget.withdraw(), e.widget.exit()))
    canvas = tkinter.Canvas(popup, width=w, height=h, highlightthickness=0)
    canvas.pack()
    canvas.configure(background='black')

    imgWidth, imgHeight = pilImage.size
    if imgWidth != w or imgHeight != h:
        ratio = min(w/imgWidth, h/imgHeight)
        imgWidth = int(imgWidth*ratio)
        imgHeight = int(imgHeight*ratio)
        pilImage = pilImage.resize((imgWidth, imgHeight))

    image = ImageTk.PhotoImage(pilImage)
    canvas.create_image(w/2, h/2, image=image)
    popup.mainloop()


if __name__ == '__main__':
    # program won't terminate if mouse goes into the corner.
    pa.FAILSAFE = False

    pilImage = Image.open(r"C:\directory\to\your\image.png")

    pa.moveTo(pa.size().width, 1000)  # Move mouse to side of screen
    
    # five_pm = datetime.time(hour=17, minute=0)
    # while datetime.datetime.now().time() < five_pm:
    showPIL(pilImage)
python-3.x tkinter windows-10 python-imaging-library tkinter-canvas

评论

1赞 TheLizzard 9/16/2023
你有,但你确定它不应该是或?e.widget.exit()e.widget.quit()e.widget.destroy()

答:

0赞 JRiggles 9/16/2023 #1

这可以通过设置来修复,使您的窗口保持在其他所有内容之上。popup.attributes('-topmost', True)

评论

0赞 TheLizzard 9/16/2023
请注意,这不会阻止其他也使用您的程序的程序出现在您的程序上。此外,窗口上的任务菜单等内容仍可能出现在程序上。topmost
0赞 JRiggles 9/18/2023
@TheLizzard 是的,这些都是好点!