如何在Tkinter中显示图像?

时间:2014-03-06 00:27:36

标签: python image tkinter messagebox

这是我目前的计划:

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

哪个产生了这个

enter image description here

一切都很好,一切都运行良好,只是当有人点击“图片”框时我需要找到一种方式来显示图片,我不知道该怎么做。我知道我需要除了消息框以外的其他东西,但我不知道是什么。有什么帮助吗?

1 个答案:

答案 0 :(得分:5)

这是一个简单的Toplevel小部件,可用于在新窗口中显示图像。

from Tkinter import *

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

mainloop()