我有一个使用Tkinter的聊天客户端。在linux上运行良好,但在Windows上给我一个错误。
Error: TclError: out of stack space (infinite loop?)
我有使用Tkinter构建的GUI。我已经构建了一个名为GUI的类,它继承了threading.thread
类。我有一个到服务器的套接字连接。当我在文本字段中按Enter键时,将激活sent
方法。在我启动GUI对象的主循环之前,recv
方法在另一个线程上运行。
这是我的GUI类:
from __future__ import division
from Tkinter import *
from utils.sound import playsound
import threading
import config.config
import os
# TKinter GUI class
class GUI(threading.Thread):
def __init__(self, client_handler):
self._base = None
self._message_box = None
self._scrollbar = None
self._message_txt = None
self._menu_bar = None
self._play_sound = True
self._locked = True
self._lock = threading.Lock()
self._client_handler = client_handler
threading.Thread.__init__(self)
# start looping
def loop(self):
self.start()
# start doing work
def run(self):
# main Form
self._base = Tk()
self._base.title(config.config.WINDOW_TITLE)
self._base.geometry("450x480")
self._base.resizable(width=FALSE, height=FALSE)
self._base.protocol("WM_DELETE_WINDOW", self.on_close) # exit event
# menu bar
self._menu_bar = Menu(self._base)
self._menu_bar.add_command(label="Disable sound", command=lambda : self.control_sound(self._menu_bar))
self._base.config(menu=self._menu_bar)
# log box
self._message_box = Text(self._base, bd=0, bg="white", height="8", width="50", font="Arial")
self._message_box.config(state=DISABLED)
# scrollbar to chat window
self._scrollbar = Scrollbar(self._base, command=self._message_box.yview)
self._message_box['yscrollcommand'] = self._scrollbar.set
# box to enter message
self._message_txt = Text(self._base, bd=0, bg="white", width="29", height="5", font="Arial")
self._message_txt.bind("<Return>", self.enter_pressed)
self._message_txt.bind("<KeyRelease-Return>", self.enter_released)
# place widgets on form
self._scrollbar.place(x=430, y=6, height=386)
self._message_box.place(x=6, y=6, height=386, width=425)
self._message_txt.place(x=6, y=401, height=70, width=437)
# loop
self.set_locked(False)
# update ui
#self.update_ui()
self._base.mainloop()
# ------------------------------------------------
# Called by different thread
# This is the method which throws the error !!!
# show ---- line in message box
# ------------------------------------------------
def new_line(self):
self._message_box.config(state=NORMAL) # breaks on this line
self._message_box.insert(END, '-----------------------------------------------------------------------------------\n')
self._message_box.yview(END)
self._message_box.config(state=DISABLED)
我试图让GUI类只继承object
(实际上没有)。通过这种方式,它不会在Windows上引发错误,但GUI正在冻结。
任何建议都表示赞赏。