我想使用Tkinter的GUI,并已实现菜单栏。我查看了一些教程并为它编写了一些代码,但菜单栏似乎从未出现 - 只是具有白色背景的空白帧。这不仅仅发生在我的代码上;在将上述教程之一的代码复制并粘贴到新脚本中时,会出现相同的行为。蟒蛇Tkinter菜单栏不显示
我会很感激,如果任何人可以摆脱任何光线导致这一点。我的系统是OS X 10.5,Python 2.7,Tk 8.4。下面是本教程不会出现工作代码:
#!/usr/local/bin/python2.7
from Tkinter import *
from ttk import *
class App(Frame):
def __init__(self):
Frame.__init__(self)
self.master.geometry('400x300')
self.master.title(__file__)
self.pack()
self.menu = Menu(tearoff=False)
self.master.config(menu = self.menu)
fm = self.file_menu = None
fm = Menu(self.menu, tearoff=False)
self.menu.add_cascade(label='File', menu = fm)
fm.add_command(label='Say Hello', command = self.say_hello)
fm.add_separator()
fm.add_command(label='Quit', command = self.quit)
self.mainloop()
def say_hello(self, *e):
self.label = Label(self.master, text='Hello there!')
self.label.pack(anchor=CENTER, fill=NONE, expand=YES, side=LEFT)
if __name__ == '__main__':
App()
,我的代码是在这里:
from Tkinter import *
class App(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
parent.title("Cluedo Solver 1.0")
menubar = Menu(root)
menubar.add_command(label="File")
menubar.add_command(label="Quit", command=root.quit())
root.config(menu=menubar)
root=Tk()
root.geometry("300x250+300+300")
app=App(root)
root.mainloop()
你的例子从来没有创建self.master,这是你的实际代码? – 2013-03-12 23:09:06
感谢您的回复!这实际上不是我的代码,它是我使用的其中一个教程中的其他人的代码,但他们都遇到同样的问题,所以我认为将它粘贴更好。我会编辑我的帖子以包含我的。 – user2163043 2013-03-13 00:05:43
你也是对的,我没有注意到关于self.master的信息 - 猜猜它写得太急了!如果我放一个root = Tk(),把root作为App的__init__的参数(并将其馈入最后一行),立即将self.master = root放入__init__并放入root。mainloop()在脚本的末尾,我仍然找到相同的结果... – user2163043 2013-03-13 00:12:59