如何使tkinter幻灯片缩放图像适合屏幕?-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

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

kun坤 2019-12-27 10:18:41 102

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

编解码 Python
分享到
取消 提交回答
全部回答(1)
  • kun坤
    2019-12-27 10:18:49

    你可以使用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()
    
    0 0
人工智能
使用钉钉扫一扫加入圈子
+ 订阅

了解行业+人工智能最先进的技术和实践,参与行业+人工智能实践项目

推荐文章
相似问题
推荐课程