【问题标题】:Image in corner of tkinter window in python 3python 3中tkinter窗口角落的图像
【发布时间】:2013-04-08 16:24:54
【问题描述】:

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

【问题讨论】:

    标签: python image window tkinter


    【解决方案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()
    

    【讨论】:

    • 能否请您为此编写一些简单的程序,我尝试了一些方法但没有成功?
    • @TheQuiteStupidMan:我添加了一个例子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-26
    • 2019-04-30
    • 2014-10-30
    • 1970-01-01
    • 1970-01-01
    • 2014-07-17
    相关资源
    最近更新 更多