2013-05-01 66 views
0

我使用pack_forget()使按鈕不可見。但是當我隨後創建一個標籤時,它會出現在不可見按鈕的下方。我怎樣才能避免這種位移?隱形按鈕(使用pack_forget)仍佔用空間

下面的代碼示例演示了該問題:

from tkinter import * 

class Application(Frame): 
    def secondwidget(self): 
     self.b.pack_forget() 
     self.l = Label(text="Lowered Label :(") 
     self.l.pack() 

    def firstwidget(self): 
     self.b = Button(self) 
     self.b["text"] = "Button" 
     self.b["command"] = self.secondwidget 
     self.b.pack() 

    def __init__(self, master=None): 
     Frame.__init__(self, master) 
     self.pack() 
     self.firstwidget() 

root = Tk() 
app = Application(master=root) 
app.mainloop() 
root.destroy() 

編輯:我使用Python 3.2

回答

2

你忘了父母的標籤:

self.l = Label(text="Lowered Label :(") 
print(self.l.winfo_parent() == str(self)) 
>>> False 

比較:

self.b = Button(self) 
print(self.b.winfo_parent() == str(self)) 
>>> True 
+0

我如何設置標籤的父級? – 2013-05-01 19:34:27

+0

'Label(self,...)' - 第一個參數是新窗口小部件的父窗口。 'self.b = Button(self)' – kalgasnik 2013-05-01 19:37:45

+0

非常感謝! – 2013-05-01 19:44:11