用pack()做的。我确信它也可以用grid()完成,但我对它不太熟悉。在from Tkinter import *
def clickTest():
print "Click!"
class App(Tk):
def __init__(self):
Tk.__init__(self)
menuBar = MenuBar(self)
buttonBar = ButtonBar(self)
self.config(menu=menuBar)
buttonBar.pack()
class MenuBar(Menu):
def __init__(self, parent):
Menu.__init__(self, parent)
fileMenu = Menu(self, tearoff=False)
self.add_cascade(label="File", menu=fileMenu)
fileMenu.add_command(label="Exit", command=clickTest)
class ButtonBar(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
firstButton = Button(self, text="1st Button", command=clickTest).pack()
secondButton = Button(self, text="2nd Button", command=clickTest).pack()
if __name__ == "__main__":
app = App()
app.mainloop()
另一件事是,您应该将buttons parent设置为Frame,如下所示:
^{pr2}$
这里我用parent改了self。self是框架本身,而不是整个顶层窗口。使用pack()函数,我将按钮打包到其父级中,在本例中是框架。在
然后用buttonBar.pack()将按钮栏打包到顶层窗口中。你也可以在这里和框架中使用网格。在