在主对象(tkinter和PIL)中全屏显示的Python代码
有了这段代码,我可以在全屏模式下显示图像在主对象(tkinter和PIL)中全屏显示的Python代码,python,windows,tkinter,python-imaging-library,fullscreen,Python,Windows,Tkinter,Python Imaging Library,Fullscreen,有了这段代码,我可以在全屏模式下显示图像 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))
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
从tkinter导入*
从PIL导入图像,ImageTk
def showPIL(pilImage):
root=Tk()
w、 h=root.winfo_屏幕宽度(),root.winfo_屏幕高度()
root.overrideredirect(1)
根几何体(“%dx%d+0+0”%(w,h))
root.focus_set()
root.bind(“,lambda e:(e.widget.draw(),e.widget.destroy())
画布=画布(根,宽度=w,高度=h)
canvas.pack()
canvas.configure(background='black')
imgWidth,imgHeight=pilImage.size
如果imgWidth>w或imgHeight>h:
比值=最小值(宽/内径,高/内径)
imgWidth=int(imgWidth*比率)
imgHeight=int(imgHeight*比率)
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')
尽管如此:
n=int(输入('Numero:'))
如果n==1:
showPIL(img1)
持续
elif n==2:
showPIL(img2)
持续
其他:
打破
但是我有个问题。。。
打开的图像不是屏幕上的主要对象,有时它位于某个打开的窗口或程序后面。。。如何离开主屏幕
您可以将这个root.attributes(“-topmost”,True)放在下面
root.bind@茎尖
这就解决了我的问题。将按钮中的
e.widget.quit()
更改为e.widget.destroy()
,这样您只需退出mainloop.thaaanks@Stevomitrich如何打开此窗口并将图像作为屏幕上的优先级?有时它会在其他程序@stevomitric后面打开。您可以将此root.attributes(“-top”,True)
放在该root.bind
下。干得好@茎尖