我正在尝试启动一个跟随Class menu in Tkinter Gui的Tkinter应用程序以获得整洁,但也为此添加了此功能 . 按钮栏,RadioButton栏等 . 类似于:
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.grid(row=0, column=0) ???
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(parent, text="1st Button", command=clickTest)
secondButton = Button(parent, text="2nd Button", command=clickTest)
if __name__ == "__main__":
app = App()
app.mainloop()
但我不知道如何让所有这些都出现在同一个窗口中 . 当然,代码原样不起作用 . 任何建议表示赞赏 . 谢谢!
1 回答
是用
pack()
做的 . 我相信它也可以用grid()
来完成,但我对它并不熟悉 .另一件事是,您应该将父按钮设置为Frame,如下所示:
在这里,我用
self
改变了parent
.self
是Frame本身,而不是整个顶层窗口 . 并且通过pack()
函数,我将按钮打包在其父节点中,在本例中为Frame .然后用
buttonBar.pack()
我将buttonBar打包到顶层窗口 . 您也可以在此处和Frame中使用网格 .