python-3.x - 如何在具有功能的tkinter中显示多张图片?
问题描述
我想在我的 gui 应用程序中显示许多图片。但是根据这里我需要保存这些图片作为我班级的参考。但我很困惑我应该如何处理很多图片?
这不会在打开的窗口上显示任何图像
from tkinter import *
import tkinter
import os
from PIL import Image, ImageTk
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__)) + "/"
class GUI():
def __init__(self,file_list):
self.file_list = file_list
self.root=Tk()
self.canvas = Canvas(self.root, width=480, height=800)
self.canvas.pack()
def image_opener(self, filename):
opened_image = Image.open(CURRENT_DIR + "images/gui/" + filename)
photo_image = ImageTk.PhotoImage(opened_image)
return photo_image
def show_image1(self):
img = self.image_opener(files["img1"])
self.canvas.create_image(0, 0, image=img, anchor = NW, state = NORMAL)
def show_image2(self):
img = self.image_opener(files["img2"])
self.canvas.create_image(0, 0, image=img, anchor = NW, state = NORMAL)
def show_screens(self):
self.show_image1()
self.show_image2()
self.root.mainloop()
files = {
"img1":"image1.png",
"img2":"image2.png",
"img3":"image3.png",
"img4":"image4.png"
}
gui = GUI(files)
gui.show_screens()
当我把它改成这个时,它会在窗口中显示图片,但是有没有什么优雅的方法可以处理很多图片(可能是 75 张左右)?
from tkinter import *
import tkinter
import os
from PIL import Image, ImageTk
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__)) + "/"
class GUI():
def __init__(self,file_list):
self.file_list = file_list
self.root=Tk()
self.canvas = Canvas(self.root, width=480, height=800)
self.canvas.pack()
self.opened_image1 = Image.open(CURRENT_DIR + "images/gui/image1")
self.photo_image1 = ImageTk.PhotoImage(self.opened_image1)
self.opened_image2 = Image.open(CURRENT_DIR + "images/gui/image2")
self.photo_image2 = ImageTk.PhotoImage(self.opened_image2)
def show_image1(self):
self.canvas.create_image(0, 0, image=self.photo_image1, anchor = NW, state = NORMAL)
def show_image2(self):
self.canvas.create_image(0, 0, image=self.photo_image2, anchor = NW, state = NORMAL)
def show_screens(self):
self.show_image1()
self.show_image2()
self.root.mainloop()
files = {
"img1":"image1.png",
"img2":"image2.png",
"img3":"image3.png",
"img4":"image4.png"
}
gui = GUI(files)
gui.show_screens()
解决方案
推荐阅读
- dictionary - 将键值对添加到 Julia 语言字典中的现有键中
- go - net.Conn.Write 返回什么错误
- c# - 值不能为空,这个错误已经得到了,虽然代码在本地运行,但是在 GCP(Ubuntu)上它给出了这个错误
- powershell - 如何动态地将 PSCustomObjects 添加到列表中
- java - BigInteger modInverse 不可逆错误
- ruby - RGeo / GeoJSON / LinearRing 环测试失败 / EPSG 3857 与 EPSG 的问题:4326
- delphi - Jedi Project 和 Delphi 11 不通过 GetIt 安装
- mysql - 我可以将数据从 MySQL Ubuntu 8.0 恢复到 Windows 吗?
- javascript - 在 Javascript 中为 plotly 获取 API 数据
- movesense - 确定传感器何时磨损的最省电方法