开发者社区> 问答> 正文

如何使tkinter幻灯片缩放图像适合屏幕?

我正在尝试使用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

展开
收起
kun坤 2019-12-27 10:18:41 690 0
1 条回答
写回答
取消 提交回答
  • 你可以使用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()
    
    2019-12-27 10:18:49
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
3D动画的菜谱式灯光与云渲染 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载

相关实验场景

更多