在tkinter窗口的角落的图象在Python 3

时间:2013-04-08 16:24:54

标签: python image window tkinter

是否可以在tkinter窗口中放置一个小图像。在窗口的右下角,如果是这样的话怎么办?

1 个答案:

答案 0 :(得分:0)

您可以创建标签,在该标签中放置图像,然后使用place将其精确放置在您想要的位置。例如,您可以使用1.0的相对x和y以及“se”的锚点将其放在右下角。

这是一个人为的例子:

import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)

        # a simple label, just to show there's something in the frame
        label = tk.Label(self, text="Example of using place")
        label.pack(side="top", fill="both", expand=True)

        # we'll place this image in every corner...
        self.image = tk.PhotoImage(data='''
            R0lGODlhEAAQALMAAAAAAP//AP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
            AAAAAAAAAAAA\nAAAAACH5BAEAAAIALAAAAAAQABAAQAQ3UMgpAKC4hm13uJnWgR
            TgceZJllw4pd2Xpagq0WfeYrD7\n2i5Yb+aJyVhFHAmnazE/z4tlSq0KIgA7\n
        ''')

        # ... by creating four label widgets ...
        self.nw = tk.Label(self, image=self.image)
        self.ne = tk.Label(self, image=self.image)
        self.sw = tk.Label(self, image=self.image)
        self.se = tk.Label(self, image=self.image)

        # ... and using place as the geometry manager
        self.nw.place(relx=0.0, rely=0.0, anchor="nw")
        self.ne.place(relx=1.0, rely=0.0, anchor="ne")
        self.sw.place(relx=0.0, rely=1.0, anchor="sw")
        self.se.place(relx=1.0, rely=1.0, anchor="se")

if __name__ == "__main__":
    root = tk.Tk()
    root.wm_geometry("400x400")
    Example(root).pack(side="top", fill="both", expand=True)
    root.mainloop()