为什么我不能打包这个Tkinter菜单小部件?

时间:2013-05-31 14:05:23

标签: python xml tkinter

我正在Tkinter中构建一个GUI作为我的应用程序的一部分,我编写了一个类“XMLMenu”,它继承了“Menu”,但是当我尝试打包它时,我收到了一个TclError。这是它的样子:

class XMLMenu(Menu):
    def __init__(self,master,text, **kw):
        Menu.__init__(self,master, kw)
        #Irrelevent code in which I read an XML file to build the menu
        self.pack(side=TOP,fill=X)

我收到以下错误:

Traceback (most recent call last):
  File "E:\Portable Python 2.7.3.1\App\lib\threading.py", line 551, in __bootstrap_inner
    self.run()
  File "E:\Portable Python 2.7.3.1\App\lib\threading.py", line 504, in run
    self.__target(*self.__args, **self.__kwargs)
  File "xlistr\gui.py", line 62, in create
    app=constructor(root)
  File "xlistr\gui.py", line 84, in __init__
    self.menu=xmlMenu_by_filename(self,"xlistr/data/feMenu.xml")
  File "xlistr\gui.py", line 23, in xmlMenu_by_filename
    return XMLMenu(master,read_file(filename))
  File "xlistr\gui.py", line 52, in __init__
    self.pack(side=TOP,fill=X)
  File "E:\Portable Python 2.7.3.1\App\lib\lib-tk\Tkinter.py", line 1806, in pack_configure
    + self._options(cnf, kw))
TclError: can't pack ".26194928.31816720": it's a top-level window

1 个答案:

答案 0 :(得分:3)

菜单不会与几何管理器一起放置,而是在相应的窗口中配置:

class XMLMenu(Menu):
    def __init__(self,master,text, **kw):
        Menu.__init__(self, master, kw)
        master.config(menu=self)
        # ...