2013-03-05 81 views
0

好吧,所以我有一個帶有編輯和查看按鈕的基本窗口。正如我的代碼所示,編輯和查看都返回一條消息「這個按鈕是無用的」。我在類「main_window」下創建了這些。我創建了另一個類「edit_window」,我希望在點擊EDIT按鈕時調用它。本質上,點擊編輯按鈕應該改變顯示新的窗口與按鈕添加和刪除。這是我的代碼到目前爲止......下一個合乎邏輯的步驟是什麼?很難理解python中的Tkinter主循環()

from Tkinter import * 
#import the Tkinter module and it's methods 
#create a class for our program 

class main_window: 
    def __init__(self, master): 
     frame = Frame(master) 
     frame.pack(padx=15,pady=100) 

     self.edit = Button(frame, text="EDIT", command=self.edit) 
     self.edit.pack(side=LEFT, padx=10, pady=10) 

     self.view = Button(frame, text="VIEW", command=self.view) 
     self.view.pack(side=RIGHT, padx=10, pady=10) 

    def edit(self): 
     print "this button is useless" 

    def view(self): 
     print "this button is useless" 

class edit_window: 
    def __init__(self, master): 
     frame = Frame(master) 
     frame.pack(padx=15, pady=100) 

     self.add = Button(frame, text="ADD", command=self.add) 
     self.add.pack() 

     self.remove = Button(frame, text="REMOVE", command=self.remove) 
     self.remove.pack() 

    def add(self): 
     print "this button is useless" 

    def remove(self): 
     print "this button is useless" 


top = Tk() 
top.geometry("500x500") 
top.title('The Movie Machine') 
#Code that defines the widgets 

main = main_window(top) 


#Then enter the main loop 
top.mainloop() 
+0

你對主流程有什麼不瞭解?對我來說,你似乎在尋找[Toplevel](http://effbot.org/tkinterbook/toplevel.htm),通過它你可以使edit_window成爲一個單獨的窗口。 – Junuxx 2013-03-05 23:46:44

+1

我可能沒有解釋得很好。我希望主窗口根據您點擊的按鈕進行更新。所以我不想打開一個新窗口,如果我點擊編輯,我想現有的窗口顯示編輯和查看---它應該更新顯示添加刪除與它以前顯示編輯查看相同的方式。合理?所以我假設Toplevel只是用ADD REMOVE打開一個新窗口。 – ordanj 2013-03-06 03:13:17

回答

0

只需創建一個Toplevel而是採用了Frame

class MainWindow: 
    #... 
    def edit(self): 
     EditWindow() 

class EditWindow(Toplevel): 
    def __init__(self): 
     Toplevel.__init__(self) 
     self.add = Button(self, text="ADD", command=self.add) 
     self.remove = Button(self, text="REMOVE", command=self.remove) 
     self.add.pack() 
     self.remove.pack() 

我根據CapWords約定(見PEP 8)改變了類名。這不是強制性的,但我建議你在你所有的Python項目中使用它來保持統一的風格。

+0

欣賞答案!不過,我試圖讓程序在單個窗口中運行。 Toplevel窗口很煩人。有沒有辦法在刪除按鈕編輯視圖時添加按鈕添加刪除?要更新窗口,可以這麼說嗎? – ordanj 2013-03-06 03:23:03

+0

更新:我意識到我可以隱藏根窗口和force_focus Toplevel窗口,所以看起來窗口不會改變。謝謝您的幫助! – ordanj 2013-03-06 05:45:52