Python 在主对象中全屏显示的代码(tkinter 和 PIL)
Python Code to Display fullscreen in main object (tkinter and PIL)
使用这段代码,我可以在全屏模式下显示图像。
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
但是我有一个问题...
打开的图像不会作为屏幕上的主要对象出现,有时它在某些打开的 window 或程序后面......我如何离开作为主屏幕?
You can put this root.attributes("-topmost", True) under that
root.bind. @StevoMitric
解决了我的问题。
使用这段代码,我可以在全屏模式下显示图像。
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
但是我有一个问题... 打开的图像不会作为屏幕上的主要对象出现,有时它在某些打开的 window 或程序后面......我如何离开作为主屏幕?
You can put this root.attributes("-topmost", True) under that root.bind. @StevoMitric
解决了我的问题。