Tkinter:菜单未显示

时间:2015-04-22 15:33:41

标签: python python-2.7 tkinter

我运行此代码的目的只是显示一个菜单栏。

有一个简单的菜单栏,其中创建了3个子菜单,但没有执行除关闭窗口的Exit之外的任何内容:

from Tkinter import *
import tkMessageBox
import numpy as np
import ttk
import tkFont
from PIL import ImageTk, Image
from tkColorChooser import askcolor
from tkFileDialog import askopenfilename
class MyGui(Frame):

   def __init__(self,master):
       Frame.__init__(self,master)
       self.master=master
       self.themenus() # menu initialization within the constructor

   def themenus(self):
       self.menubar=Menu(self.master)

       self.filemenu=Menu(self.menubar,tearoff=0)
       self.filemenu.add_command(label="Open Image")
       self.filemenu.add_command(label="Save Image")
       self.filemenu.add_command(label="Exit",command=self.master.quit)

       self.menubar.add_cascade(label="File",menu=self.filemenu)

if __name__=="__main__":
   root=Tk()
   root.wm_title("Test")
   mg=MyGui(root)
   root.mainloop()

我收到此错误:

libdc1394 error: Failed to initialize libdc1394

如何解决这个问题?

修改

我通过删除毕竟没有使用的原始导入来解决问题:

import tkMessageBox
import numpy as np
import ttk
import tkFont
from PIL import ImageTk, Image
from tkColorChooser import askcolor
from tkFileDialog import askopenfilename

现在,没有触发错误,但我没有看到显示的菜单。为什么?

1 个答案:

答案 0 :(得分:1)

我通过在themenus()函数末尾添加此行来解决我的问题:

self.master.config(menu=self.menubar)