Python - 格式化聊天客户端 [英] Python - formatted chat client

查看:29
本文介绍了Python - 格式化聊天客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个聊天服务器和客户端,它使用基本的套接字连接将消息发送到服务器,然后服务器将它们发送给所有连接的用户.这一切都是在命令行中使用 print 完成的,因此下一步是使用 Tk (Python 2.7) 制作 GUI.

I have made a chat server and client which uses basic socket connections to send messages to a server which then sends them to all connected users. This was all done using print in the command line and so the next step was to make a GUI, using Tk (Python 2.7).

我现在遇到的问题是用包装在客户端显示收到的消息.

The problem I now have is displaying the received messages in the client with wrapping.

起初我尝试使用列表框,除了显然无法在列表框上换行的事实之外,它完美地工作.

At first I tried using a Listbox, which worked perfectly apart from the fact that there is apparently no way to wrap text on a Listbox.

其次,我尝试使用一个文本框,这完全是一场灾难,因为我找不到任何方法在不输入的情况下在其中打印文本.

Secondly I tried using a text box which was a total disaster since i couldn't find any methods for printing text in it without typing it out.

第三次我尝试了一个画布,它确实支持自动换行并且能够创建文本,但是我在换行后将文本打印在正确的位置以及滚动条不起作用时遇到了一些问题

Thirdly I tried a canvas, which does support word wrapping and has the ability to create text, however I had some problems with the text being printed in the right place after the wrapping as well as the scroll bar not working

.

那么这些问题中的任何一个都可以解决吗,或者我是否完全错误的树并且有一种非常简单的方法可以做到这一点?

So can any of those issues be solved, or am I barking up totally the wrong tree and there is a really easy way to do this?

这是我目前为 Canvas 编写的代码:

Here's the code i have at the moment for the Canvas:

self.chatspace.create_text(5, i, text = recv_data, anchor=NW, width=175)
i = i + (((len(recv_data) / 175) + 1) * 15)

我意识到 len() 返回字符串中的字符数,而不是以像素为单位的长度.

I realise that len() returns the number of characters in the string and not the length in pixels.

抱歉问了 3 个问题,我完全被难住了,我已经研究了 4 个小时了.

Sorry for asking 3 questions, i am totally stumped, I've been working on this for 4 hours.

推荐答案

您应该使用文本小部件.要将文本插入小部件,请使用 insert 方法:

You should use the text widget. To insert text into the widget, use the insert method:

The_widget.insert("end", "hello, world")

这几乎在所有 Tkinter 文档中都有记录.例如,参见 http://effbot.org/tkinterbook/text.htm#Tkinter.Text.insert-method

This is documented pretty much in all Tkinter documention. For example, see http://effbot.org/tkinterbook/text.htm#Tkinter.Text.insert-method

这篇关于Python - 格式化聊天客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆