使用Python、Tkinter和;皮尔

使用Python、Tkinter和;皮尔,python,user-interface,tkinter,python-imaging-library,Python,User Interface,Tkinter,Python Imaging Library,所以,我正在参加一个简单的在线课程项目,用python制作一个图像库。关键是要创建3个按钮,一个是Next,一个是Previous,一个是Quit。到目前为止,quit按钮可以工作,next加载一个新的图像,但是在另一个窗口中,我对使用Tkinter进行python和GUI编程非常陌生,所以这是begineers课程的一个重要部分 到目前为止,我的代码看起来是这样的,一切都正常。但是我需要帮助如何创建“上一步”和“下一步”按钮,到目前为止,我已经使用了新语句,但它会在不同的窗口中打开。我只想显示

所以,我正在参加一个简单的在线课程项目,用python制作一个图像库。关键是要创建3个按钮,一个是Next,一个是Previous,一个是Quit。到目前为止,quit按钮可以工作,next加载一个新的图像,但是在另一个窗口中,我对使用Tkinter进行python和GUI编程非常陌生,所以这是begineers课程的一个重要部分

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

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()

通过设置图像项更改图像:
Label['image']=photoimage\u 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()
导入图像
导入ImageTk
进口Tkinter
image_list=['1.jpg','2.jpg','5.jpg']
text_list=['apple'、'bird'、'cat']
电流=0
def移动(增量):
全局当前图像列表

如果不是(0我的UI不是很好。但是我的逻辑工作得很好。我测试得很好。你可以更改UI。它的工作原理是,首先我们需要浏览文件,当我们单击“打开”时,它会显示图像,并且它会创建一个在所选图像文件夹中的图像列表。我只提到了“.png”和“.jpg”文件。如果你想添加更多,你可以添加它路径_func()


听起来很有趣,这是一门公开的课程吗?不是,通过斯德哥尔摩大学,这是一门python多媒体编程的在线课程。我想这对全世界的学生都是可行的,但对瑞典公民来说是免费的,它把所有的东西都放在一个窗口中,这个窗口还包含一个
PhotoImage
小部件,并且有一个
mainloop()
调用。当按下下一个或上一个按钮时,执行相应的功能或方法,用另一个包含相应图像的图像小部件替换此图像小部件。您的意思是在主窗口下放置程序的所有组件?例如,我要显示的bot myImage?并且不使用wind部分?缩进示例代码中的缩进是混乱的。由于缩进在python中非常重要,我们很难确切知道您的程序在做什么。如果我希望添加适用于每个图片的文本,请使用一个简单的文本框。然后我是否应该使用与每个图片连接的文本列表来代替图像列表@falsetru@andrejcurcic,将代码更新为disp在图片下方放置文本。我爱你!已经被困了4天了。谢谢!在Python 3+中使用global是反模式的(以及导入*),此外,列出的代码不是基于作者的示例,而是看起来像一个不同的项目
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()