【发布时间】: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