2013-11-24 70 views
3
from Tkinter import * 

class Window(Tk): 
    def __init__(self, parent): 
     Tk.__init__(self, parent) 
     self.parent = parent 
     self.initialize() 

    def initialize(self): 
     self.geometry("600x400+30+30") 
     wButton = Button(self, text='text', command = self.OnButtonClick()) 
     wButton.pack() 

    def OnButtonClick(self): 
     top = Toplevel() 
     top.title("title") 
     top.geometry("300x150+30+30") 
     topButton = Button(top, text="CLOSE", command = self.destroy) 
     topButton.pack() 


if __name__ == "__main__": 
    window = Window(None) 

    window.title("title") 

    window.mainloop() 

#  top.lift(aboveThis=self) 
    #self.configure(state=DISABLED) - unknown option "-state" 
    #ss = self.state() 
    #self["state"] = "disabled" - unknown option "-state" 
#ws = window.state() # >>> ws outputs: 'normal' 
    # varname.unbind("<Button-1>", OnButtonClick) 
    #self.unbind("<Button-1>", OnButtonClick) 
    #window.unbind("<Button-1>") 
###if window.OnButtonClick == True: 
### window.unbind("<Button-1>", OnButtonClick) 

Python的ver2.7.3代碼如上所述,當在IDLE ver2.7.3跑,使用 Tkver8.5:第一顯示較小的頂部= Toplevel的()窗口對於 秒,在顯示窗口=窗口(Tk)的一個實例之前,它就是 。這是在任何按鈕被點擊或任何東西之前。

上面的代碼下面的所有評論只是我自己的事情,我已經嘗試和想法嘗試下一個(idk - 也許沒有幫助的東西)的筆記。

如何將上面的代碼更改爲:將window = Window(Tk)的實例作爲父窗口,將top = Toplevel()窗口作爲子窗口。然後,當我運行程序時,只有父窗口應該顯示;然後當我點擊'wButton'時,子窗口應該出現在父窗口的頂部,父窗口被禁用 - 它的按鈕不可操作,並且用戶無法通過點擊將窗口提升到最前沿?Python的GUI代碼點擊一個按鈕以打開另一個窗口打破

+0

請驗證顯示代碼的正確性(縮進)。另外,儘量縮短問題的範圍,但要注意點。 –

+1

現在將代碼顯示在上方,就像我在* .py文件中輸入代碼一樣。感謝您修復我的格式錯誤。我猜想一些不正確的縮進之前就搞亂了,因爲當我把帖子放在* .html文件中時,它顯示正確。我會縮小非代碼,問題部分,然後編輯帖子。 –

回答

5

command僅預計函數名稱 - 沒有()和參數。

使用

wButton = Button(self, text='text', command = self.OnButtonClick) 

如果使用command = self.OnButtonClick()運行self.OnButtonClick()和結果分配給command。如果要動態創建command的功能,這可能非常有用。


爲了讓子窗口送花兒給人頂部的父窗口,你可以使用child.transient(parent)

在你的代碼應該是top.transient(self)

def OnButtonClick(self): 
    top = Toplevel() 
    top.title("title") 
    top.geometry("300x150+30+30") 

    top.transient(self) 

    topButton = Button(top, text="CLOSE", command = self.destroy) 
    topButton.pack() 

您可以使用.config(state='disabled').config(state='normal')禁用/啓用按鈕。

您可以在OnButtonClick()中禁用主窗口按鈕,但需要新的功能在子窗口關閉之前/之後啓用該按鈕。

from Tkinter import * 

class Window(Tk): 
    def __init__(self, parent): 
     Tk.__init__(self, parent) 
     self.parent = parent 
     self.initialize() 

    def initialize(self): 
     self.geometry("600x400+30+30") 
     self.wButton = Button(self, text='text', command = self.OnButtonClick) 
     self.wButton.pack() 

    def OnButtonClick(self): 
     self.top = Toplevel() 
     self.top.title("title") 
     self.top.geometry("300x150+30+30") 
     self.top.transient(self) 
     self.wButton.config(state='disabled') 

     self.topButton = Button(self.top, text="CLOSE", command = self.OnChildClose) 
     self.topButton.pack() 

    def OnChildClose(self): 
     self.wButton.config(state='normal') 
     self.top.destroy() 

if __name__ == "__main__": 
    window = Window(None) 

    window.title("title") 

    window.mainloop() 
0
謝謝,furas!

「command」只需要一個函數名稱 - 不帶「()」和參數。 感謝您在那裏捕捉我的錯誤。我認爲這就是「神祕地」改變了我之前得到的結果。我改變了「命令」選項,當我改回時,我犯了把「()」放進去的錯誤。所以,我猜想結果(頂端已經繪製)是我得到的。有關爲動態創建「命令」功能的見解確實非常有用。我會牢記這一點。

你的四個建議很好。哈,我記得試圖弄清楚如何改變窗口的狀態很長一段時間,但我永遠找不到任何示例代碼,所以即使在聯機查看和* .py後,我也沒有得到正確的語法模塊與源代碼。我非常感激向我展示解決方案。
相關問題