使用此代码,我可以在全屏模式下显示图像。
from tkinter import *
from PIL import Image, ImageTk
def showPIL(pilImage):
root = Tk()
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.overrideredirect(1)
root.geometry("%dx%d+0+0" % (w, h))
root.focus_set()
root.bind("<Escape>", lambda e: (e.widget.withdraw(), e.widget.destroy()))
canvas = Canvas(root, width=w, height=h)
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.ANTIALIAS)
image = ImageTk.PhotoImage(pilImage)
imagesprite = canvas.create_image(w / 2, h / 2, image=image)
root.mainloop()
img1 = Image.open("img1.png")
img2 = Image.open('img2.png')
while True:
n = int(input('Numero: '))
if n == 1:
showPIL(img1)
continue
elif n == 2:
showPIL(img2)
continue
else:
break
但是我有一个问题...打开的图像不是作为屏幕上的主要对象出现的,有时它是在一些打开的窗口或程序后面......如何离开主屏幕?
你可以把这个root.attributes("-topmost",True(放在那个下面 root.bind. @StevoMitric
这解决了我的问题。