如何在Tkinter中显示图像?

1 投票
1 回答
4835 浏览
提问于 2025-04-17 21:01

这是我现在的程序:

from tkinter import *
from tkinter import messagebox
from collections import deque


class App():

    def __init__(self, *images):
        self.root = Tk()
        self.root.title("Disease")
        self.root.bind("<Button-1>", self.click_event)

        self.image_dict = {image: PhotoImage(file=image) for image in images}
        self.image_queue = deque(images)

        start_image = self.image_dict[self.image_queue[0]]
        self.label = Label(self.root, image=start_image)
        self.label.image = start_image
        self.label.pack()

    def change_image(self):
        self.image_queue.rotate(-1)
        next_image = self.image_queue[0]
        self.label.configure(image=self.image_dict[next_image])
        self.label.image = self.image_dict[next_image]

    def click_event(self, event):
        print ("clicked at", event.x, event.y )
        if (8 < event.x < 241) and (150 < event.y < 232):
            messagebox.showinfo("Description", "- Also known as the 'Stone Man Syndrome', it's a rare disease that affects the connective tissue.\n\n- Whenever tissue is damaged, instead of the body healing and repairing the wounds, it grows bone in its place.\n\n- Sometimes the body will even begin spontaneously growing excess bone throughout the body for no reason, leading to extremely limited movement.")

        if (255 < event.x < 494) and (150 < event.y < 232):
            messagebox.showinfo("Causes", "- FOP is caused by mutation of certain genes and chromosomes, more specifically the ACVR1 protein, something that is used to help control the functions of cells.\n\n- There is no known cure. The only thing sufferers can do is have surgery to remove the excess bone, but that ends up growing back.")

        if (8 < event.x < 241) and (245 < event.y < 317):
            messagebox.showinfo("People Affected", "- At the moment there are only around 700 people around the world who have been confirmed for having FOP. Nobody knows how many people have been affected with it in the past.")

        if (255 < event.x < 494) and (245 < event.y < 317):
            messagebox.showinfo("Pictures", "")

        if (8 < event.x < 494) and (327 < event.y < 388):
            messagebox.showinfo("Sources", "http://en.wikipedia.org/wiki/Fibrodysplasia_ossificans_progressiva\n\nhttp://ghr.nlm.nih.gov/condition/fibrodysplasia-ossificans-progressiva\n\nhttp://www.ifopa.org/fop-fact-sheet.html")

if __name__ == "__main__":
    app = App('1.gif')
    app.root.mainloop()

这个程序运行后会产生这样的效果:

在这里输入图片描述

一切都很好,程序运行得很顺利。现在我只需要找到一种方法,让用户点击“图片”框时能显示图片,但我不太确定该怎么做。我知道我需要用到其他东西,而不是消息框,但我不知道具体是什么。有没有人能帮帮我?

1 个回答

6

这里有一个简单的顶层窗口小部件,你可以用它在新窗口中显示一张图片。

from Tkinter import *

top = Toplevel()
diagrams = PhotoImage(file='your image')
logolbl= Label(top, image = diagrams)
logolbl.grid()

mainloop()

撰写回答