3

所以,我正在进行一个简单的在线课程项目,使用 python 制作一个图片库。事情是创建 3 个按钮,一个是 Next、Previous 和 Quit。到目前为止,退出按钮有效,下一个加载新图像但在不同的窗口中,我对 python 和使用 Tkinter 进行 GUI 编程非常陌生,所以这是初学者课程的重要组成部分。

到目前为止,我的代码看起来像这样,一切正常。但是我在如何制作上一个和下一个按钮方面需要帮助,到目前为止我已经使用了 NEW 语句,但它在不同的窗口中打开。我只想显示 1 张图像,然后单击带有一些简单文本的下一张图像。

import Image
import ImageTk
import Tkinter

root = Tkinter.Tk();
text = Tkinter.Text(root, width=50, height=15);
myImage = ImageTk.PhotoImage(file='nesta.png');

def new():
wind = Tkinter.Toplevel()
wind.geometry('600x600')
imageFile2 = Image.open("signori.png")
image2 = ImageTk.PhotoImage(imageFile2)
panel2 = Tkinter.Label(wind , image=image2)
panel2.place(relx=0.0, rely=0.0)
wind.mainloop()

master = Tkinter.Tk()
master.geometry('600x600')

B = Tkinter.Button(master, text = 'Previous picture', command = new).pack()

B = Tkinter.Button(master, text = 'Quit', command = quit).pack()

B = Tkinter.Button(master, text = 'Next picture', command = new).pack()

master.mainloop()
4

2 回答 2

5

通过设置图像项更改图像:Label['image'] = photoimage_obj

import Image
import ImageTk
import Tkinter

image_list = ['1.jpg', '2.jpg', '5.jpg']
text_list = ['apple', 'bird', 'cat']
current = 0

def move(delta):
    global current, image_list
    if not (0 <= current + delta < len(image_list)):
        tkMessageBox.showinfo('End', 'No more image.')
        return
    current += delta
    image = Image.open(image_list[current])
    photo = ImageTk.PhotoImage(image)
    label['text'] = text_list[current]
    label['image'] = photo
    label.photo = photo


root = Tkinter.Tk()

label = Tkinter.Label(root, compound=Tkinter.TOP)
label.pack()

frame = Tkinter.Frame(root)
frame.pack()

Tkinter.Button(frame, text='Previous picture', command=lambda: move(-1)).pack(side=Tkinter.LEFT)
Tkinter.Button(frame, text='Next picture', command=lambda: move(+1)).pack(side=Tkinter.LEFT)
Tkinter.Button(frame, text='Quit', command=root.quit).pack(side=Tkinter.LEFT)

move(0)

root.mainloop()
于 2013-07-06T17:17:56.700 回答
-1

我的用户界面不太好。但我的逻辑运作良好,我测试得很好。你可以改变用户界面。它的工作原理是,首先我们需要浏览文件,当我们单击打开时,它会显示图像,并且它还会创建一个位于该选定图像文件夹中的图像列表。我只提到了“.png”和“.jpg”文件。如果你想添加更多,你可以在 path_func() 中添加它。

from tkinter import *
from PIL import Image, ImageTk
from tkinter import filedialog
import os

class ImageViewer:

    def __init__(self, root):
        
        self.root = root
        self.root.title("Photo Viewer")
        self.root.geometry("1360x750")
        self.root.config(bg = "lightblue")

        menus = Menu(self.root)
        self.root.config(menu = menus)

        file_menu = Menu(menus)
        menus.add_cascade(label = "File", menu = file_menu)
        
        file_menu.add_command(label = "Open", command = self.open_dialog)
        file_menu.add_separator()
        file_menu.add_command(label = "Previous", command = self.previous_img)
        file_menu.add_command(label = "Next", command = self.next_img)
        file_menu.add_separator()
        file_menu.add_command(label = "Exit", command = self.root.destroy)

        self.label = Label(self.root, text = "Open a image using open menu", font = ("Helvetica", 15), foreground = "#0000FF", background = "lightblue")
        self.label.grid(row = 0, column = 0, columnspan = 4)

        self.buttons()

    def path_func(self, path):
        l = []
        self.path = path.split('/')
        self.path.pop()

        self.path = '/'.join([str(x) for x in self.path])
        
        #print(self.path)
        
        for file in os.listdir(self.path):
            if file.endswith('.jpg') or file.endswith('.png'):
                l.append(file)
                #print(l)

        def join(file):
            os.chdir(self.path)
            #print(os.getcwd())
            cwd = os.getcwd().replace('\\', '/')
            #print(cwd)
            f = cwd + '/' + file
            #print(f)
            return f
        
        global file_list
        file_list = list(map(join, l))
        #print(file_list) 

    def open_dialog(self):
            global file_name
            file_name = filedialog.askopenfilename(initialdir = "C:/Users/elcot/Pictures", title = "Open file")
            #print(file_name)
            self.view_image(file_name)
            self.path_func(file_name)

            '''except:
            label = Label(self.root, text = "Select a file to open")
            label.grid(row = 4, column =1)'''

    def view_image(self, filename):
        try:
            self.label.destroy()
            global img
            img = Image.open(filename)
            img = img.resize((1360, 650))
            img = ImageTk.PhotoImage(img)
        
        #print(img)

            show_pic = Label(self.root, image = img)
            show_pic.grid(row = 1, column = 0, columnspan = 3)

        except:
            pass
        
    def buttons(self):
        open_button = Button(self.root, text = "Browse", command = self.open_dialog, background = "lightblue")
        open_button.grid(row = 1, column = 1)
        
        previous_button = Button(self.root, text = "Previous", command = self.previous_img, background = "lightblue", width = 25)
        previous_button.grid(row = 3, column = 0, pady = 10)

        empty = Label(self.root, text = "        ", background = "lightblue")
        empty.grid(row = 3, column = 1)
        
        next_button = Button(self.root, text = "Next", command = self.next_img, background = "lightblue", width = 25)
        next_button.grid(row = 3, column = 2)


    def previous_img(self):
        global file_name
        #print(file_list)
        index = file_list.index(file_name)
        #print(index)
        curr = file_list[index - 1]
        #print(curr)
        self.view_image(curr)
        file_name = curr


    def next_img(self):
        global file_name
        index = file_list.index(file_name)
        #print(index)
        if index == len(file_list) - 1:
            index = -1
            curr = file_list[index + 1]
            #print(curr)
            self.view_image(curr)
            file_name = curr
        else:
            curr = file_list[index + 1]
            #print(curr)
            self.view_image(curr)
            file_name = curr

 
if __name__ == "__main__":
    root = Tk()
    gallery = ImageViewer(root)
    root.mainloop()
于 2021-06-09T06:52:40.827 回答