0

好的,所以我有一个带有 EDIT 和 VIEW 按钮的基本窗口。正如我的代码所代表的那样, EDIT 和 VIEW 都返回一条消息“这个按钮没用”。我在“main_window”类下创建了这些。我创建了另一个类“edit_window”,希望在单击 EDIT 按钮时调用它。本质上,单击编辑按钮应该更改显示带有按钮添加和删除的新窗口。到目前为止,这是我的代码......下一个合乎逻辑的步骤是什么?

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()
4

1 回答 1

0

只需创建 aToplevel而不是使用 a 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 项目中使用它以保持统一的样式。

于 2013-03-06T00:00:30.250 回答