【问题标题】:Tcl Error in Tkinter "Invalid command name"Tkinter 中的 Tcl 错误“无效的命令名称”
【发布时间】:2016-01-07 02:51:08
【问题描述】:

我正在尝试使用 Tkinter 构建一个带有图形界面的 IA,但是当我运行我的代码时,在用户空间中写入一些内容并按 Enter,我得到了这个错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python33\lib\tkinter\__init__.py", line 1482, in __call__
    return self.func(*args)
  File "C:\Users\Ilan\Desktop\Python\ProjetIa\IA 2.0\GUI.pyw", line 53, in Enter_pressed
    self.text.config(state='normal')
  File "C:\Python33\lib\tkinter\__init__.py", line 1270, in configure
    return self._configure('configure', cnf, kw)
  File "C:\Python33\lib\tkinter\__init__.py", line 1261, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name ".56241624.56241736"

这是我的代码

图形界面:

from tkinter import *
from tkinter import font

input_get = ""
ia_answers = ""

class TextFrame(Frame):
   def __init__(self, window, **kwargs):
    """Init the main top frame who contains the discussion"""
    LabelFrame.__init__(self, window, text="Discussion",borderwidth = 15, height = 200, width = 200)
    self.pack(fill=BOTH, side=TOP, expand = True)

    # Font options
    self.printopt = font.Font(family = "Times")

    # Create the Text widget, who contains the discussion, it is append by the Enter_pressed function
    self.text = Text(self, state='disabled', bg ="grey")
    self.text.pack(fill = BOTH, expand = True, side = "left")

    # Tag for the justify options on the text
    self.text.tag_configure("right", justify="right", font=self.printopt)
    self.text.tag_configure("left", justify="left", font=self.printopt)

    # Put the Scrollbar and his options
    self.scr = Scrollbar(self)
    self.scr.config(command=self.text.yview)
    self.text.config(yscrollcommand=self.scr.set)
    self.scr.pack(side="right", fill="y", expand=False)

class InputFrame(TextFrame):
  def __init__(self, window, **kwargs):
    """Init the main bottom frame who contains the user input box """

    # Import the parent class (Textframe)modules
    TextFrame.__init__(self,window)
    LabelFrame.__init__(self, window, text="User :", borderwidth=4)
    self.pack(fill=BOTH, side=BOTTOM)

    # Create the input box for the user
    self.input_user = StringVar()
    self.input_field = Entry(self)
    self.input_field.pack(fill=BOTH, side=BOTTOM)

    def Enter_pressed(event):
     """Took the current string in the Entry field.""" 
     # Take the globals variables
     global input_get
     global ia_answers
     # Take the input of the user in the input box
     self.input_get = self.input_field.get()
     self.input_field.delete(0, 1000)
     # Put the text in state normal then add the variables finally put it in disabled mode
     self.text.config(state='normal')
     self.text.insert("end", "\n"+input_get+"\n", "left")
     self.text.insert("end", "\n"+ia_answers+"\n", "right")
     self.text.config(state='disabled')
     # Scroll automatically to the bottom of the window
     self.text.yview(END)
    self.input_field.bind("<Return>", Enter_pressed)

这是我的主脚本

主脚本:

import GUI
from GUI import *
from tkinter import *
window = Tk()
TextFrame(window)
InputFrame(window)
window.mainloop()

提前谢谢你。 宜兰

【问题讨论】:

  • TextFrame.__init__ 内你必须调用Frame.__init__,而不是LabelFrame.__init__,因为你定义了class TextFrame(Frame)。在InputFrame.__init__ 内部,您只需调用TextFrame.__init__,因为您声明了class InputFrame(TextFrame)
  • 我试过了,但它看起来不像应该的,我有两个框架和一个输入框分开。
  • 你的课程看起来很奇怪——这一切都是__init__。如果您在其他班级需要LabelFrame,可以致电LabelFrame(...)
  • Enter_pressed(event) 部分InputFrame 类?
  • 我知道,但这是我发现的旧代码,是的,它是 InputFrame 的一部分

标签: python user-interface tkinter


【解决方案1】:

问题在于您错误地构造了类。看这段代码:

class TextFrame(Frame):
   def __init__(self, window, **kwargs):
    ...
    LabelFrame.__init__(self, window, text="Discussion",borderwidth = 15, height = 200, width = 200)
    ...

您继承自Frame,但正在调用LabelFrame 的构造函数。这不会导致您的问题,但它是不正确的,应该修复。您需要从LabelFrame 继承,或者调用Frame 构造函数。

另外,看看这段代码:

class InputFrame(TextFrame):
  def __init__(self, window, **kwargs):
    ...
    TextFrame.__init__(self,window)
    LabelFrame.__init__(self, window, text="User :", borderwidth=4)
    ...

在上面的代码中,您从TextFrame 继承并正确调用了TextFrame 的构造函数,但您调用了LabelFrame 的构造函数。对LabelFrame.__init__ 的调用是导致您的invalid command name 错误的原因。您需要删除该行代码。

但是,尚不清楚您是否真的打算从TextFrame 继承,或者您是否打算从LabelFrame 继承。无论您从哪个继承,您都需要调用该类的 __init__ 方法。

【讨论】:

  • 感谢您的回答,但是当我按下 Enter 时,我得到了一个 属性错误:'InputFrame' 对象没有属性'text'。我尝试了多种方法,但似乎不起作用,答案肯定就在我面前,但我看不到。
  • @IlanRossler:这是一个完全不同的问题。错误应该是不言自明的。您的 InputText 类显然没有名为 text 的属性。
  • :那么,我怎样才能访问其他类中的文本小部件?如果我写了class InputFrame(TextFrame) 为什么我不能访问它?
  • 即使我写了TextFrame.text.config(state='normal'),它也会说AttributeError: type object 'TextFrame' has no attribute 'text'
  • @IlanRossler:不要一直在问题上扔代码。想想错误告诉你什么。哪些对象有text 属性?您有权访问这些对象吗?这不是 tkinter 问题,只是 python 变量范围的正常问题。
猜你喜欢
  • 2013-07-07
  • 2017-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-14
  • 1970-01-01
相关资源
最近更新 更多