0

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

4

1 回答 1

1

您可以创建一个标签,将图像放在该标签中,然后使用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()
于 2013-04-08T16:47:55.567 回答