我正在尝试使用Tkinter做一个图像幻灯片放映,但我遇到了一个问题,任何图像不一样的本机分辨率我的显示器(或更少)将被切断。 我现在的代码:
import tkinter as tk
from itertools import cycle
from PIL import ImageTk, Image
import random
with open('list.txt', 'r') as f: #sparces the list file into something the code can understand
lines = f.read().strip('[]')
images = [i.strip("\" ") for i in lines.split(',')]
var1 = input("random?")
if var1 == "yes" or var1 == "Yes":
#runs the randomized version of the code
random.shuffle(images)
photos = cycle(ImageTk.PhotoImage(Image.open(image)) for image in images)
def slideShow():
img = next(photos)
displayCanvas.config(image=img)
root.after(1200, slideShow)
root = tk.Tk()
root.overrideredirect(True)
width = root.winfo_screenwidth()
height = root.winfo_screenwidth()
root.geometry('%dx%d' % (1920, 1080))
displayCanvas = tk.Label(root)
displayCanvas.pack()
root.after(1000, lambda: slideShow())
root.mainloop()
elif var1 == "no" or var1 == "No":
#runs the alphabetical version of the code
photos = cycle(ImageTk.PhotoImage(Image.open(image)) for image in images)
def slideShow():
img = next(photos)
displayCanvas.config(image=img)
root.after(1200, slideShow)
root = tk.Tk()
root.overrideredirect(True)
width = root.winfo_screenwidth()
height = root.winfo_screenwidth()
root.geometry('%dx%d' % (1920, 1080))
displayCanvas = tk.Label(root)
displayCanvas.pack()
root.after(1000, lambda: slideShow())
root.mainloop()
else:
print("you gotta answer with yes,Yes,no,No,")
exit
我如何使它的图像缩放到适当的显示在屏幕上? 问题来源StackOverflow 地址:/questions/59383540/how-to-make-tkinter-slideshow-scale-images-to-fit-on-screen
你可以使用PIL image. resize()函数来改变图像的大小。下面是一个修改后的代码基础上你的:
with open('list.txt', 'r') as f: #sparces the list file into something the code can understand
lines = f.read().strip('[]')
images = [i.strip('"') for i in lines.split(',')]
var1 = input("Random ? ")
if var1.lower() in ("yes", "y"):
random.shuffle(images)
root = tk.Tk()
root.attributes('-fullscreen', 1) # make the root window fullscreen
root.config(cursor="none") # hide the mouse cursor
# get the screen size
scr_width = root.winfo_screenwidth()
scr_height = root.winfo_screenheight()
print("Preparing images ...")
photos = []
for image in images:
img = Image.open(image)
if img.width > scr_width or img.height > scr_height:
# only resize image bigger than the screen
ratio = min(scr_width/img.width, scr_height/img.height)
img = img.resize((int(img.width*ratio), int(img.height*ratio)))
photos.append(ImageTk.PhotoImage(img))
slides = cycle(photos)
def slideShow():
displayCanvas.config(image=next(slides))
root.after(1200, slideShow)
displayCanvas = tk.Label(root)
displayCanvas.pack(expand=1, fill=tk.BOTH)
root.bind('<Escape>', lambda e: root.destroy()) # allow Esc key to terminate the slide show
slideShow() # start the slide show
root.focus_force()
root.mainloop()
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。