Reputation: 33
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 :
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
Upvotes: 0
Views: 2133
Reputation: 386335
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
. 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.
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
. That call to LabelFrame.__init__
is what is causing your invalid command name
error. 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
. Whichever one you inherit from, you need to call that class's __init__
method.
Upvotes: 1