简体   繁体   English

如何在Tkinter中显示图像?

[英]How to show an image in Tkinter?

Here is my current program: 这是我当前的程序:

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

Which produces this 哪个产生这个

在此处输入图片说明

And everything is good, everything runs fine, it's just that I need to find a way to have pictures appear when someone clicks on the "Pictures" box, which i'm not sure how to do. 一切都很好,一切运行良好,只是我需要找到一种方法,当有人单击“图片”框时显示图片,我不确定该怎么做。 I know that I need something other than the messagebox, but I don't know what. 我知道除了消息框外我还需要其他东西,但我不知道什么。 Any help? 有什么帮助吗?

Here is a simple Toplevel widget that you can use to display an image in a new window. 这是一个简单的顶级小部件,可用于在新窗口中显示图像。

from Tkinter import *

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

mainloop()

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

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