2013-04-08 45 views

回答

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

你能否为此写一些简单的程序,我试过了一些东西,但没有奏效? – TheQuiteStupidMan 2013-04-09 15:45:17

+0

@TheQuiteStupidMan:我已经添加了一个例子。 – 2013-04-09 16:23:42

+0

非常感谢你! – TheQuiteStupidMan 2013-04-10 14:42:39

-1
example_label = Label(window, text = 'example') 

example_label.pack(anchor = 'nw') 

'nw'代表西北,因此,例如,你可以使用:ne(东北),se(东南)。

+1

如果你解释代码的作用,以及它如何帮助解决问题,这将是一个更好的答案。 – Nae 2018-01-01 18:25:57

+0

这不一定会将图像放在窗口的角落。它很大程度上取决于其他小部件已放置在那里。你也不能用“se”代替“nw”,因为默认的位置是“top”。如果你想要在角落里,你必须指定'side ='bottom'',并且在底部或者右边不能包含任何其他的小部件。 – 2018-01-01 18:50:48