帮助开发一个聊天 Python 应用程序
我目前正在用 python 制作一个聊天应用程序。我有两个单独的代码:一个用于服务器,一个用于客户端。服务器脚本正在获取连接的新客户端的登录数据,并在另一个线程中管理他必须接收和发送的消息。
客户端应用程序被制作成一个类并且运行良好,除了当脚本请求 UI 显示时,唯一的新窗口是一个空窗口:
def __init__(self, master):
self.nr=0
self.frameul=self.tbox=self.txt=self.scrollbar=self.button=self.roottk=[0]*20
self.OameniSiIduri={}
self.LoginUI(master)
self.framestate=""
def ChatUI(self, peer_id):
no=self.no
self.no+=1
self.PeoplesAndId[peer_id]=no
self.base[no]=Toplevel()
self.theframe[no] = Frame(self.base[no])
self.theframe[no].pack()
self.entry[no] = Entry(self.theframe[no], width=95)
self.tbox[no] = Text(self.theframe[no], state=DISABLED, wrap=WORD)
self.button[no] = Button(self.theframe[no], text="Send", fg="green", command=lambda x=self.entry[no].get(), y=peer_id, z=self.tbox[nr]: self.Sendmsg(x,y,z), width=10)
self.tbox[no].pack(side=TOP, fill=X)
self.button[no].pack(side=RIGHT)
self.entry[no].pack(side=LEFT)
.....
所有变量和函数都已声明。谁能告诉我这个问题的原因是什么?
I am currently making a chat application in python. I am having 2 separate codes: one for the server and one for the client. The server script is taking the login data of new clients that connects and in another thread manages the messages that he has to receive and send.
The client application is made into a class and works well, excepts that when the script requests the UI to show, the only new window is a empty one:
def __init__(self, master):
self.nr=0
self.frameul=self.tbox=self.txt=self.scrollbar=self.button=self.roottk=[0]*20
self.OameniSiIduri={}
self.LoginUI(master)
self.framestate=""
def ChatUI(self, peer_id):
no=self.no
self.no+=1
self.PeoplesAndId[peer_id]=no
self.base[no]=Toplevel()
self.theframe[no] = Frame(self.base[no])
self.theframe[no].pack()
self.entry[no] = Entry(self.theframe[no], width=95)
self.tbox[no] = Text(self.theframe[no], state=DISABLED, wrap=WORD)
self.button[no] = Button(self.theframe[no], text="Send", fg="green", command=lambda x=self.entry[no].get(), y=peer_id, z=self.tbox[nr]: self.Sendmsg(x,y,z), width=10)
self.tbox[no].pack(side=TOP, fill=X)
self.button[no].pack(side=RIGHT)
self.entry[no].pack(side=LEFT)
.....
All the vars and the functions are declared. Can anyone give me a hint about what can the cause of this problem is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过一整晚的搜索代码,我发现了自己的错误。显然,如果我使用这一行:
所有对象都指向相同的值。
Found myself the mistake after searching all the night through the code. Apparently if i use this line:
All the objects point to the same value.
我的猜测是,创建 UI 的代码抛出了您没有看到的错误。例如,您是否正确导入
DISABLED
和WORD
?如果没有,代码将在创建框架之后但在创建其他小部件之前失败,从而留下一个空的小部件。调试此问题的一种方法是为每个顶层和框架赋予不同的颜色。这可以让您看到哪些是可见的,哪些是不可见的——也许您正在看着窗户或框架,并认为它是一回事,但实际上它是另一回事。
My guess is, the code that is creating the UI is throwing an error that you are not seeing. For example, are you importing
DISABLED
andWORD
properly? If not, the code would fail after creating the frame but before creating the other widgets, leaving you with an empty widget.One way to debug this is to give each toplevel and frame a distinct color. This lets you see which are visible and which are not -- maybe you're looking at a window or frame and thinking it's one thing when it's something else.