简体   繁体   English

如何通过按下 Tkinter 中两个图像的下一个和上一个按钮来更改两个并排图像

[英]How to change two side by sides images by pressing next and previous buttons for both images in Tkinter

I have a two separate lists of multiple images files and their respective names.我有两个单独的多个图像文件列表及其各自的名称。 An image will be displayed with its respective name below it.图像将显示在其下方及其各自的名称。 I have created two buttons to go to 'next' and 'previous' images.我创建了两个按钮 go 到“下一个”和“上一个”图像。 What I want is that, on clicking of 'next' button, the program should show next image present in the list, and on clicking 'previous' button, it should show 'previous' image in the list with its respective name.我想要的是,在单击“下一个”按钮时,程序应该显示列表中的下一个图像,而在单击“上一个”按钮时,它应该在列表中显示“上一个”图像及其各自的名称。 For simplicity, I have only shown one slection box.为简单起见,我只展示了一个选择框。 But actually, there will be a same selection box parallel to it.但实际上,会有一个相同的选择框与之平行。 ie creating a situation like "Seletion 1 VS Selection 2".即创造一种情况,如“选择 1 VS 选择 2”。 Such situation usually occurs when we want to select two teams for a match in a computer game, for example.例如,当我们想要 select 两支球队在电脑游戏中进行一场比赛时,通常会出现这种情况。 So kindly, guide me how can this be applied to select two things.好心,请指导我如何将其应用于 select 两件事。 But I am sorry to say,I haven't tried anything myself because I have literally zero idea how to do this.但我很遗憾地说,我自己没有尝试过任何东西,因为我对如何做到这一点几乎一无所知。

from tkinter import *

root = Tk()
root.geometry('360x240')

all_images = ['image1', 'image2', 'image3', 'image4']   #List of all Images
all_images_names = ['name1', 'name2', 'name3', 'name4'] #List of names of all images

myFrame = Frame(root)
myFrame.pack()

image = Label(myFrame, text = 'Image will be displayed here', font = ('', 12, 'bold'), bg = 'light blue')
image.pack()

lbutton = Button(myFrame, text = '<', fg = 'red', font = ('', 11, 'bold'))
lbutton.pack(side = LEFT)

image_name = Label(myFrame, text = 'Image Name', font = ('', 10,'bold'))
image_name.pack(side = LEFT)

rbutton = Button(myFrame, text = ">", fg = 'red', font = ('', 11, 'bold'))
rbutton.pack(side = LEFT)

root.mainloop()

You have to add functionality to both of the buttons left and right.您必须向左右两个按钮添加功能。 Thats done assigning to the 'command=' variable of the Button widget a function of your choice.这就是将您选择的 function 分配给 Button 小部件的“command=”变量。

Below is an example regarding the core functionality.以下是有关核心功能的示例。 I have stored the images as name, src tuples, since those values come in pairs.我将图像存储为名称、src 元组,因为这些值成对出现。 In case you want to avoid the "magic numbers" for choosing the name / src, a slower named tuple or a dict might be an option too.如果您想避免选择名称 / src 的“幻数”,较慢的命名元组或字典也可能是一种选择。

import tkinter as tk

def main():

    images = [
        ('image1','src1'),
        ('image2','src2'),
        ('image3','src3'),
    ]
    
    app = App(images)
    app.mainloop()


class App(tk.Tk):
    def __init__(self, images):
        super().__init__()
        self.geometry("360x240")
        self.cur_image = 0
        self.images=images

        self.image = tk.Label(
            self,
            text=self.images[self.cur_image][1],
            font=('', 12, 'bold'),
            bg='light blue'
        )
        self.image.pack()


        self.left = tk.Button(
            self,
            text="<",
            fg='red',
            font=('', 11, 'bold'),
            command=self.left
        )
        self.left.pack(side=tk.LEFT)

        self.image_name=tk.Label(
            self,
            text=self.images[self.cur_image][0],
            font=('', 10,'bold')
            )
        self.image_name.pack(side=tk.LEFT)

        self.right = tk.Button(
            self,
            text=">",
            fg='red',
            font=('', 11, 'bold'),
            command=self.right
        )
        self.right.pack(side=tk.LEFT)

    def left(self):
        self.cur_image = (self.cur_image - 1) % len(self.images)
        self.update_image()

    def right(self):
        self.cur_image = (self.cur_image + 1) % len(self.images)
        self.update_image()

    def update_image(self):
        self.image_name.config(text=self.images[self.cur_image][0])
        self.image.config(text=self.images[self.cur_image][1])


if __name__ == '__main__':
    main()

Now lets place the functionality in a frame, and create 2 of those frames.现在让我们将功能放在一个框架中,并创建其中的 2 个框架。 One for each of the galleries:每个画廊一个:

import tkinter as tk

def main():

    images = [
        ('image1','src1'),
        ('image2','src2'),
        ('image3','src3'),
    ]

    app = App(images)
    app.mainloop()


class App(tk.Tk):
    def __init__(self, images):
        super().__init__()
        self.geometry("360x240")

        self.images=images

        self.left = ImageSlider(master=self, images=self.images)
        self.left.grid(column=0, row=0)
        self.right = ImageSlider(master=self, images=self.images)
        self.right.grid(column=1, row=0)


class ImageSlider(tk.Frame):
    def __init__(self, master, images):
        super().__init__(master)
        self.master = master
        self.images = images
        self.cur_image = 0

        self.image = tk.Label(
            self,
            text=self.images[self.cur_image][1],
            font=('', 12, 'bold'),
            bg='light blue'
        )
        self.image.pack()


        self.left = tk.Button(
            self,
            text="<",
            fg='red',
            font=('', 11, 'bold'),
            command=self.left
        )
        self.left.pack(side=tk.LEFT)

        self.image_name=tk.Label(
            self,
            text=self.images[self.cur_image][0],
            font=('', 10,'bold')
            )
        self.image_name.pack(side=tk.LEFT)

        self.right = tk.Button(
            self,
            text=">",
            fg='red',
            font=('', 11, 'bold'),
            command=self.right
        )
        self.right.pack(side=tk.LEFT)

    def left(self):
        self.cur_image = (self.cur_image - 1) % len(self.images)
        self.update_image()

    def right(self):
        self.cur_image = (self.cur_image + 1) % len(self.images)
        self.update_image()

    def update_image(self):
        self.image_name.config(text=self.images[self.cur_image][0])
        self.image.config(text=self.images[self.cur_image][1])


if __name__ == '__main__':
    main()

In order to replace the src - label by the actual image, please check: How to add an image in Tkinter?为了用实际图片替换src - label,请查看: How to add an image in Tkinter?

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

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