简体   繁体   English

Tkinter中的Tcl错误“无效的命令名称”

[英]Tcl Error in Tkinter “Invalid command name”

I'm trying to build an IA with a graphic interface with Tkinter, but when i'm running my code, write something into the User space and press Enter, i got this error : 我正在尝试使用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"

This is my code 这是我的代码

GUI : 界面:

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)

And this is my mainscript 这是我的原稿

Mainscript : 稿件:

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

Thank you in advance. 先感谢您。 Ilan 宜兰

The problem lies in the fact that you are constructing your classes incorrectly. 问题在于您错误地构造了类。 Look at this code: 看下面的代码:

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

You are inheriting from Frame , but are calling the constructor of LabelFrame . 您从Frame继承,但是正在调用LabelFrame的构造LabelFrame This is not causing your problem, but it is incorrect and should be fixed. 这不会引起您的问题,但它是不正确的,应予以解决。 You need to either inherit from LabelFrame , or call the Frame constructor. 您需要从LabelFrame继承,或调用Frame构造函数。

Also, take a look at this code: 另外,看看下面的代码:

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

In the above code, you inherit from TextFrame and correctly call the constructor for TextFrame , but you also call the constructor for LabelFrame . 在上面的代码,你继承TextFrame和正确地呼吁构造TextFrame ,但你呼吁构造LabelFrame That call to LabelFrame.__init__ is what is causing your invalid command name error. LabelFrame.__init__调用是导致invalid command name错误的原因。 You need to remove that line of code. 您需要删除该行代码。

However, it's not clear if you really intend to inherit from TextFrame , or if you intend to inherit from LabelFrame . 但是,目前还不清楚,如果你真的打算要继承TextFrame ,或者如果你打算继承LabelFrame Whichever one you inherit from, you need to call that class's __init__ method. 无论从哪个继承,都需要调用该类的__init__方法。

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

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