简体   繁体   English

TclError:在 python3.7 上使用 tkinter 的未知选项“-menu”

[英]TclError: Unkown option '-menu' using tkinter on python3.7

Learning how to use tkinter and I'm getting an error I don't understanding it, as it doesn't really make sense to me.学习如何使用 tkinter,但我遇到了一个我不理解的错误,因为它对我来说没有任何意义。 It states that the option "-menu" isn't recognized, but... I don't use it?它指出无法识别选项“-menu”,但是......我不使用它?

Thanks for reading and possibly helping!感谢阅读并可能提供帮助!

import tkinter as tk


LARGE_FONT = ('Verdana', 12)


class CofBTC(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)

        container.pack(side='top', fill='both', expand=True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        frame = startPage(container,self)

        self.frames[startPage] = frame

        frame.grid(row=0, column=0, sticky='nsew')

        self.showFrame(startPage)

    def showFrame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

class startPage(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent, controller)
        label = tk.Label(self, text = 'Start Page', font = LARGE_FONT)
        label.pack(pady=10, padx=10)

root= tk.Tk('TestTkinter')

app = CofBTC()
app.mainloop()

Edit: Was asked for the full error message, so here it is (I don't really understand it, so for some reason I didn't think about the fact that some people actually do understand these lol):编辑:被要求提供完整的错误信息,所以在这里(我不太明白,所以出于某种原因我没有想到有些人确实理解这些 lol 的事实):

Traceback (most recent call last):

  File "<ipython-input-4-9a8fe12ef0a8>", line 1, in <module>
    runfile('C:/Users/danburnier/Desktop/Music21/sanstitre4.py', wdir='C:/Users/danburnier/Desktop/Music21')

  File "C:\Users\danburnier\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "C:\Users\danburnier\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/danburnier/Desktop/Music21/sanstitre4.py", line 52, in <module>
    app = CofBTC()

  File "C:/Users/danburnier/Desktop/Music21/sanstitre4.py", line 29, in __init__
    frame = startPage(container,self)

  File "C:/Users/danburnier/Desktop/Music21/sanstitre4.py", line 46, in __init__
    tk.Frame.__init__(self, parent, controller)

  File "C:\Users\danburnier\AppData\Local\Continuum\anaconda3\lib\tkinter\__init__.py", line 2744, in __init__
    Widget.__init__(self, master, 'frame', cnf, {}, extra)

  File "C:\Users\danburnier\AppData\Local\Continuum\anaconda3\lib\tkinter\__init__.py", line 2299, in __init__
    (widgetName, self._w) + extra + self._options(cnf))

TclError: unknown option "-menu"`

Problem is because you send controller to Frame问题是因为您将controller发送到Frame

tk.Frame.__init__(self, parent, controller)

and it doesn't know what to do with this unexpected element它不知道如何处理这个意想不到的元素

You need你需要

tk.Frame.__init__(self, parent)

BTW: If you inherit from tk.Tk - class CofBTC(tk.Tk) then you don't need root= tk.Tk('TestTkinter') because tkinter should use only one window create with Tk()顺便说一句:如果你继承自tk.Tk - class CofBTC(tk.Tk)那么你不需要root= tk.Tk('TestTkinter')因为tkinter应该只使用一个用Tk()创建的窗口


In Python 3 you can use super()在 Python 3 中你可以使用super()

    #tk.Tk.__init__(self, *args, **kwargs)
    super().__init__(*args, **kwargs)

and

    #tk.Frame.__init__(self, parent)
    super().__init__(parent)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM