2013-03-05 82 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