需要帮助在PhotoImage .gif上放置按钮

时间:2014-02-01 21:20:55

标签: python tkinter gif

这是我的程序:

from tkinter import *
from collections import deque

class App():

    def __init__(self, *images):
        self.root = Tk()
        self.root.title("Skin")

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

        b = Button(self.root, text="Click here to see the diagram!", command=self.change_image)
        b.pack(fill=X)

        self.label = Label(self.root, image=self.image_dict["1.gif"])
        self.label.image = self.image_dict["1.gif"]
        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]

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

这样做是当你运行scipt时,会出现一个窗口,上面写着“1.gif”和一个按钮。单击按钮时,“1.gif”将更改为“2.gif”。 “1.gif”是一个空白图,“2.gif”是一个图表,其中的标签显示了图表的每个部分。

现在我的程序的下一个阶段,我需要一些方法在“2.gif”图表上的每个单词上添加多个隐形按钮或类似的东西,当你点击它时,我需要一个单独的窗口上面有文字。有没有办法将其实现到我当前的程序中?我不知道从哪里开始。谢谢!

1 个答案:

答案 0 :(得分:1)

我认为你最好使用Canvas来保存你的图像 而不是标签。然后,您可以在图表上放置“热点”并绑定 给他们的事件。例如。类似的东西:

from tkinter import *

class App():

    def __init__(self):
        self.root = Tk()

        self.messages = {}

        self.canvas = Canvas(self.root, bd=0, highlightthickness=0)
        self.image = PhotoImage(file='2.gif')
        self.canvas.create_image(0,0, image=self.image, anchor='nw')
        w,h = self.image.width(), self.image.height()
        self.canvas.config(width=w, height=h)
        self.canvas.pack()

        self.balloon = b = Toplevel(self.root)
        b.withdraw()
        b.wm_overrideredirect(1)
        self.msg = Label(b, bg='black', fg='yellow', bd=2, text='')
        self.msg.pack()

        self.set_up_hotspots()

    def set_up_hotspots(self):
        box1 = self.canvas.create_polygon(50,100,100,100,100,150,50,150,
                                               outline='blue', fill='')
        #note: outline can be '' also ;)
        self.messages[box1] = "The machine head consists of a "\
                              "Flynn mechanism,\na Demmel crank, "\
                              "and a heavy-duty frobulator. "

        box2 = self.canvas.create_polygon(150,100,200,100,200,150,150,150,
                                               outline='red', fill='')
        self.messages[box2] = "And now for something completely different"

        for item in [box1, box2]:
            for event, handler in [('<Enter>', self.on_hotspot_enter),
                                   ('<Leave>', self.on_hotspot_leave)]:
                self.canvas.tag_bind(item, event, handler)

    def on_hotspot_enter(self, event):
        if not self.balloon.winfo_ismapped():
            txt = self.messages[event.widget.find_withtag('current')[0]]
            self.msg.config(text=txt)
            x,y = event.x_root, event.y_root
            self.balloon.geometry('+%d+%d' %(x+16,y))
            self.balloon.deiconify()

    def on_hotspot_leave(self, event):
        if self.balloon.winfo_ismapped():
            self.balloon.withdraw()


if __name__ == "__main__":

    app = App()
    app.root.mainloop()