繁体   English   中英

尝试使用 Tkinter (Python3) 制作图像幻灯片

[英]Trying to make image slideshow with Tkinter (Python3)

我试图用 Tkinter 和 Python3 制作一个图像幻灯片程序。 没有错误,但没有显示我选择的目录中的图像。 我使用的其他库是:PIL、random 和 glob。 对你的帮助表示感谢。

我的系统:

Ubuntu 20.04 LTS

这是代码:

import tkinter as Tk
from PIL import Image, ImageTk
import random
import glob

class gui:
    def __init__(self, mainwin):
        self.counter = 0
        self.mainwin = mainwin
        self.mainwin.title("Our Photos")
        self.colour()
        self.mainwin.configure(bg = "yellow")
        self.Frame = Tk.Frame(mainwin)
        self.img = Tk.Label(self.Frame)
        self.Frame.place(relheight = 0.85, relwidth = 0.9, relx = 0.05, rely = 0.05 )
        self.img.pack()
        self.pic()
    def colour(self):
        self.colours  =['gray47','gray48']

        c = random.choice(self.colours)
        self.mainwin.configure(bg = c)
        root.after(4000, self.colour)

    def pic(self):
        for name in glob.glob(r"/home/maheswar/Pictures/*"):
            self.pic_list = []
            val = name
            self.pic_list.append(val)
        
        if self.counter == len(self.pic_list) - 1:
            self.counter = 0
        else:
            self.counter == self.counter + 1
        
        self.file = self.pic_list[self.counter]
        self.load = Image.open(self.file)
        self.pic_width = self.load.size[0]
        self.pic_height = self.load.size[1]
        self.real_aspect = self.pic_width/self.pic_height
        self.calc_width = int(self.real_aspect * 800)  
        self.load2 = self.load.resize((self.calc_width, 800))
        self.render = ImageTk.PhotoImage(self.load2)
        self.img.config(image = self.render)
        self.img.image = self.render
        root.after(2000, self.pic) 
  


root = Tk.Tk()
myprog = gui(root)
root.geometry("1000x1000")
root.mainloop()

我发现了两个错误——你可能会看到你是否会使用print()来调试代码

首先:您在循环内创建列表self.pic_list = []以便替换以前的内容,这样您只能获得一个列表。 但是你不需要这个循环,而是直接

self.pic_list = glob.glob(r"/home/maheswar/Pictures/*")

第二:你需要=而不是==self.counter = self.counter + 1甚至更简单

self.counter += 1

完整的工作代码,改动很小。

import tkinter as Tk
from PIL import Image, ImageTk
import random
import glob

class GUI:  # PEP8: `CamelCaseNames` for classes
    
    def __init__(self, mainwin):
        self.mainwin = mainwin
        self.mainwin.title("Our Photos")
        self.mainwin.configure(bg="yellow")  # PEP8: inside `()` use `=` without spaces

        self.counter = 0
        
        self.frame = Tk.Frame(mainwin)  # PEP8: `lower_case_names` for variables
        self.frame.place(relheight=0.85, relwidth=0.9, relx=0.05, rely=0.05)

        self.img = Tk.Label(self.frame)
        self.img.pack()

        self.pic_list = glob.glob("/home/maheswar/Pictures/*")  # no need prefix `r`
        self.colours = ['gray47', 'gray48']  # PEP8: space after `,`

        self.colour()
        self.pic()
        
    def colour(self):
        selected = random.choice(self.colours)
        self.mainwin.configure(bg=selected)
        root.after(4000, self.colour)

    def pic(self):

        filename = self.pic_list[self.counter]
        image = Image.open(filename)
        
        real_aspect = image.size[0]/image.size[1]
        width = int(real_aspect * 800)  
        
        image = image.resize((width, 800))
        
        self.photo = ImageTk.PhotoImage(image)
        self.img.config(image=self.photo)
        #self.img.image = self.render  no need if you use `self.` to keep PhotoImage

        self.counter += 1
        
        if self.counter >= len(self.pic_list):
            self.counter = 0

        root.after(2000, self.pic) 
  
# --- main ---

root = Tk.Tk()
myprog = GUI(root)
root.geometry("1000x1000")
root.mainloop()

PEP 8——Python 代码风格指南

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM