Python 我想添加一个按钮,它可以加载下一个图像文件并显示到画布上 我想添加一个按钮,它可以加载下一个图像文件并显示到画布上。这是我到目前为止所做的代码。有人能帮忙添加下一步按钮吗?
以下脚本允许您查看选定文件夹中的所有图像:Python 我想添加一个按钮,它可以加载下一个图像文件并显示到画布上 我想添加一个按钮,它可以加载下一个图像文件并显示到画布上。这是我到目前为止所做的代码。有人能帮忙添加下一步按钮吗?,python,image,function,canvas,tkinter,Python,Image,Function,Canvas,Tkinter,以下脚本允许您查看选定文件夹中的所有图像: import os import glob from tkinter import filedialog import tkinter as tk from PIL import ImageTk, Image class ImageViewer(object): def __init__(self): self.root = tk.Tk() self.root.state('zoomed')
import os
import glob
from tkinter import filedialog
import tkinter as tk
from PIL import ImageTk, Image
class ImageViewer(object):
def __init__(self):
self.root = tk.Tk()
self.root.state('zoomed')
self.width = self.root.winfo_screenwidth()
self.height = self.root.winfo_screenheight()
self.images = None
img, wh, ht = self.open_file()
self.canvas = tk.Canvas(self.root, width=wh, height=ht, bg='black')
self.canvas.pack(expand=tk.YES)
self.image_on_canvas = self.canvas.create_image(self.width/2, self.height/2, anchor=tk.CENTER, image=img)
b = tk.Button(self.root, text='next', command=self.next_image)
b.place(x=50, y=50)
self.root.mainloop()
def open_file(self):
openfile = filedialog.askopenfilename(initialdir='/', title='Select image', filetypes=(('jpeg files', '*.jpg'), ('all files', '*.*')))
self.images = glob.glob(os.path.dirname(openfile) + '/*.jpg')
self.root.title(openfile)
img = Image.open(openfile)
img.thumbnail((self.width, self.height), Image.ANTIALIAS)
img = ImageTk.PhotoImage(img)
return img, self.width, self.height
def next_image(self):
if not self.images:
img, wh, ht = self.open_file()
else:
image = self.images.pop(0)
self.root.title(image)
img = Image.open(image)
img.thumbnail((self.width, self.height), Image.ANTIALIAS)
img = ImageTk.PhotoImage(img)
self.canvas.itemconfigure(self.image_on_canvas, image=img)
self.canvas.config(width=self.width, height=self.height)
try:
self.canvas.wait_visibility()
except tk.TclError:
pass
ImageViewer()
您有什么问题/错误?对不起,解释不充分。当我运行代码时,应该同时弹出两个窗口。一个是python库中的画布窗口,另一个是选择图像文件的文件对话框窗口。若我从对话框窗口中选择一个图像文件,那个么该图像应该显示在画布中。并且应该在画布中创建一个按钮。这是我想要的。如果我点击该按钮,图像将自动更改为下一个图像。每当我再次单击该按钮时,就会执行此任务。
图像应自动更改为下一个图像
–什么是下一个图像
?同一文件夹中会有另一组图像文件。例如,如果同一文件夹中有10个图像文件,并且如果我选择其中一个文件,然后单击按钮,那么下一个图像应该显示在同一文件夹中。我非常感谢您的时间和代码。真的很棒。我希望有一天能像你一样。这是很棒的代码。我会用你的来教我。
import os
import glob
from tkinter import filedialog
import tkinter as tk
from PIL import ImageTk, Image
class ImageViewer(object):
def __init__(self):
self.root = tk.Tk()
self.root.state('zoomed')
self.width = self.root.winfo_screenwidth()
self.height = self.root.winfo_screenheight()
self.images = None
img, wh, ht = self.open_file()
self.canvas = tk.Canvas(self.root, width=wh, height=ht, bg='black')
self.canvas.pack(expand=tk.YES)
self.image_on_canvas = self.canvas.create_image(self.width/2, self.height/2, anchor=tk.CENTER, image=img)
b = tk.Button(self.root, text='next', command=self.next_image)
b.place(x=50, y=50)
self.root.mainloop()
def open_file(self):
openfile = filedialog.askopenfilename(initialdir='/', title='Select image', filetypes=(('jpeg files', '*.jpg'), ('all files', '*.*')))
self.images = glob.glob(os.path.dirname(openfile) + '/*.jpg')
self.root.title(openfile)
img = Image.open(openfile)
img.thumbnail((self.width, self.height), Image.ANTIALIAS)
img = ImageTk.PhotoImage(img)
return img, self.width, self.height
def next_image(self):
if not self.images:
img, wh, ht = self.open_file()
else:
image = self.images.pop(0)
self.root.title(image)
img = Image.open(image)
img.thumbnail((self.width, self.height), Image.ANTIALIAS)
img = ImageTk.PhotoImage(img)
self.canvas.itemconfigure(self.image_on_canvas, image=img)
self.canvas.config(width=self.width, height=self.height)
try:
self.canvas.wait_visibility()
except tk.TclError:
pass
ImageViewer()