【问题标题】:Error in my code I have no clue whats wrong. Tkinter我的代码中的错误我不知道出了什么问题。特金特
【发布时间】:2019-04-27 15:31:50
【问题描述】:

我的代码中有这个错误,我在网上搜索了答案,但不知道如何修复。不知道为什么我的代码只有 165 行长时出现 2000+ 行错误?

这是错误:

Traceback(最近一次调用最后一次): 文件“/Applications/Wing101.app/Contents/Resources/src/debug/tserver/_sandbox.py”,第 156 行,在 init 中的文件“/Applications/Wing101.app/Contents/Resources/src/debug/tserver/_sandbox.py”,第 80 行 init 中的文件“/Applications/Wing101.app/Contents/Resources/src/debug/tserver/_sandbox.py”,第 38 行 init 中的文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py”,第 2591 行 Widget.init(self, master, 'label', cnf, kw) init 中的文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py”,第 2090 行 (widgetName, self._w) + extra + self._options(cnf)) _tkinter.TclError: 未知选项“-container”

有人能解释一下发生了什么吗?

编辑 1:(这是我的代码

from Tkinter import *
from Tkinter import Menu
import Tkinter as tk
import tkFont as tkfont
import os

class master(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
    def show(self):
        self.lift()

class DisplayPage(master):
    def __init__(self, *args, **kwargs):
        master.__init__(self, *args, **kwargs)
        # create all of the main containers

        frame_F = Frame(self, width=930, height=780)
        frame_G = Frame(self, width=465, height=170)
        frame_H = Frame(self, width=465, height=170)
        frame_I = Frame(self, width=465, height=170)
        frame_J = Frame(self, width=465, height=170)
        frame_K = Frame(self, width=465, height=85)

        # layout all of the main containers

        frame_F.grid(row=0, column=0, columnspan=2, rowspan=4)
        frame_G.grid(row=0, column=3)
        frame_H.grid(row=1, column=3)
        frame_I.grid(row=2, column=3)
        frame_J.grid(row=3, column=3)
        frame_K.grid(row=4, column=3)

        # create widget 1

        contentA = Frame(self, frame_G)

        namelbl5 = Label(self, contentA, text='Chosen Destination:', font =('Roboto Thin', 30), )

        # create widget 2

        contentB = Frame(self, frame_H)

        namelbl6 = Label(self, contentB, text='Distance From Destination:', font =('Roboto Thin', 30), )

        # create widget 3

        contentC = Frame(self, frame_I)

        namelbl7 = Label(self, contentC, text='Available Parking Spaces:', font=('Roboto Thin', 30), )

        # create widget 4

        contentD = Frame(self, frame_J)
        namelbl8 = Label(self, contentD, text='Price Per Hour:', font =('Roboto Thin', 30), )

        # next ok button

        contentE= Frame(self, frame_K)
        back = Button(self, contentE, text='Back', font =('Roboto Thin', 30))
        back.pack()

        # layout all widgets

        contentA.grid(column=0, row=0)
        contentB.grid(column=3, row=1)
        contentC.grid(column=3, row=2)
        contentD.grid(column=3, row=3)
        contentE.grid(column=3, row=4)

        namelbl5.grid(column=3, row=1)
        namelbl6.grid(column=3, row=3)
        namelbl7.grid(column=3, row=5)
        namelbl8.grid(column=3, row=7)
        back.grid(column=3, row=9)

class MainPage(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        p1 = DisplayPage(self)        

# create all of the main containers 
        frame_A = Frame(self, width=930, height=780)
        frame_B = Frame(self, width=465, height=280)
        frame_C = Frame(self, width=465, height=280)
        frame_D = Frame(self, width=465, height=140)
        frame_E = Frame(self, width=465, height=70)

        # layout all of the main containers

        frame_A.grid(row=0, column=0, columnspan=2, rowspan=3)
        frame_B.grid(row=0, column=3)
        frame_C.grid(row=1, column=3)
        frame_D.grid(row=2, column=3)
        frame_E.grid(row=3, column=3)

        # create widget 1

        content1 = Frame(self, frame_B)

        onevar = BooleanVar()
        twovar = BooleanVar()
        threevar = BooleanVar()
        onevar.set(False)
        twovar.set(False)
        threevar.set(False)

        namelbl = Label(self, content1, text='Filter System', font =('Roboto Thin', 30), )
        one = Checkbutton(self, content1, text='Closest', font =('Roboto Thin', 30), variable=onevar, onvalue=True)
        two = Checkbutton(self, content1, text='Cheapest', font =('Roboto Thin', 30), variable=twovar, onvalue=True)
        three = Checkbutton(self, content1, text='Most Available Space', font =('Roboto Thin', 30), variable=threevar, onvalue=True)

        # create widget 2

        content2 = Frame(self, frame_C)

        namelbl2 = Label(self, content2, text='Starting Location', font =('Roboto Thin', 30), )
        name = Entry(self, content2)
        namelbl3 = Label(self, content2, text='Destination', font=('Roboto Thin', 30), )
        name2 = Entry(self, content2)

        # create widget 3

        content3 = Frame(self, frame_D)

        namelbl4 = Label(self, content3, text='Chosen Car Park', font =('Roboto Thin', 30), )
        name3 = Entry(self, content3)

        # next ok button

        content4= Frame(self, frame_E)
        ok = tk.Button(self, content4, text="Locate",command=p1.lift)
        ok.pack()

        # layout all widgets

        content1.grid(column=0, row=0)
        content2.grid(column=3, row=1)
        content3.grid(column=3, row=2)
        content4.grid(column=3, row=3)

        namelbl.grid(column=3, row=1)
        name.grid(column=3, row=6)
        namelbl2.grid(column=3, row=5)
        name2.grid(column=3, row=8)
        namelbl3.grid(column=3, row=7)
        name3.grid(column=3, row=10)
        one.grid(column=3, row=2)
        two.grid(column=3, row=3)
        three.grid(column=3, row=4)
        ok.grid(column=3, row=11)
        namelbl4.grid(column=3, row=9)                   

if __name__ == "__main__":
    root = tk.Tk()
    main = MainPage(root)
    main.pack(side="top", fill="both", expand=True)
    menu = Menu(root)
    root.config(menu=menu)
    root.wm_title('MobilePark Simulator')
    root.wm_geometry("1300x830")
    new_item = Menu(menu)
    new_item.add_command(label='Save')
    menu.add_cascade(label='File', menu=new_item) 
    root.mainloop()  

【问题讨论】:

  • 您所说的错误是因为错误的根源是在您的代码使用的某个库中,而不是在您的代码本身中。确保您已升级到最新版本的 tkinter。 _sandbox.py 是您正在编辑的文件吗?
  • 如果需要我也可以添加代码,但是它非常长(而且我知道人们筛选会很痛苦)对造成的所有不便表示抱歉,感谢所有帮助!
  • 你应该发布一个最小的可验证代码示例,即尽可能多地删除不必要的代码,直到你得到仍然显示错误的最小版本
  • @GreenCloakGuy 不确定 _sandbox.py 是什么,但我的文件名为 MobilePark Simulator.py
  • 请尝试将此代码缩减为minimal reproducible example。我怀疑你需要几十个框架、标签和按钮来重现这个问题。

标签: python tkinter


【解决方案1】:

那么,大行号的段是:

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",
    line 2090, in init (widgetName, self._w) + extra + self._options(cnf)) _tkinter.TclError:
    unknown option "-container"

这意味着它所抱怨的不是您的代码,而是调用堆栈中的某些东西,在Tkinter 代码本身中。

话虽如此,通常是因为你在代码中做错了一些事情,比如传递了一个无效的参数或类似的东西。

我不确定这里的情况是否如此,因为整个调用堆栈似乎是由不是你的代码组成的。这意味着_sandbox.pyTkinter 之间可能不兼容。

鉴于沙箱似乎属于 Wing101(一个 IDE,因此很可能是一个沙箱,用于在 IDE 下运行时包含应用程序),我要做的第一件事通过尝试直接从命令行运行代码,将其排除在外。如果它在那里工作,几乎可以肯定你的 IDE 设置有问题。如果不是,那么您的代码正在执行的操作有问题。


我自己已经这样做了并且看到错误仍然存​​在,我不得不问您正在尝试如何处理以下内容:

# create widget 1
contentA = Frame(self, frame_G)
namelbl5 = Label(self, contentA, text='Chosen Destination:', font =('Roboto Thin',30), )

具体来说,标签构造函数中contentA 的存在似乎是将配置设置为一个大字典,其中包含大量不适合小部件的键:

{'bd': 0, 'bg': '#d9d9d9', 'container': 0, 'colormap': '', 'text': 'Chosen Destination:', 'font': ('Roboto Thin', 30), 'height': 170, 'cursor': '', 'width': 465, 'visual': '', 'highlightcolor': '#000000', 'relief': 'flat', 'background': '#d9d9d9', 'padx': <pixel object at 0x1d1ac40>, 'pady': <pixel object at 0x1d1ac70>, 'highlightthickness': 0, 'highlightbackground': '#d9d9d9', 'class': 'Frame', 'takefocus': '0', 'borderwidth': 0}

我会说(乍一看)你在构造函数中似乎有太多的父母。换句话说,selfcontentX 应该是父级,例如:

namelbl5 = Label(contentA, text='Chosen Destination:', font =('Roboto Thin',30), )

现在无论这是否是您的实际意图,我无法确定,但我可以保证删除其中一个或另一个可以解决您当前的问题。


我的建议是尽量简化控件的层次结构。当我怀疑你可以摆脱主框架和单个布局管理器时,你有三到四层。

【讨论】:

  • 请问我可以做些什么来解决这个错误?我也不确定 sandbox.py 来自哪里
  • 我在终端运行这个吗? ~/code/hw01/MobilePark 模拟器 A.py ?
  • 嗨,我刚刚使用 python MBSA.py 运行,它在终端中出现了同样的错误(MBSA 是我的文件名)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-08
  • 1970-01-01
  • 2012-05-19
相关资源
最近更新 更多