我正在尝试用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"
这是我的代码
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)
这是我的主语
标题:
import GUI
from GUI import *
from tkinter import *
window = Tk()
TextFrame(window)
InputFrame(window)
window.mainloop()
提前谢谢你。 伊兰
答案 0 :(得分: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__
方法。