Oliver H Gray
Oliver H Gray

Reputation: 105

Python - formatted chat client

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?

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)

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

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

Upvotes: 0

Views: 2313

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386285

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

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

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

Upvotes: 3

Related Questions