Im working on a GUI for some chat programme. For user's input I have Text() widget, messages are sent via "Return" and after that I clean the Text(). But as hard as I tried I cant remove the last "\n" which Return button creates.
Here is my code for this part:
def Send(Event):
MSG_to_send=Tex2.get("1.0",END)
client.send(MSG_to_send)
Tex2.delete("1.0",END)
looking forward for offers)
Type something inside the textbox, and then, click the “Delete” button. It will erase the content inside the textbox.
While creating a canvas in tkinter, it will effectively eat some memory which needs to be cleared or deleted. In order to clear a canvas, we can use the delete() method. By specifying “all”, we can delete and clear all the canvas that are present in a tkinter frame.
If we want to delete a label that is defined in a tkinter application, then we have to use the destroy() method.
Tkinter Entry widgets are used to display a single line text that is generally taken in the form of user Input. We can clear the content of Entry widget by defining a method delete(0, END) which aims to clear all the content in the range.
Most likely your problem is that your binding is happening before the newline is inserted. You delete everything, but then the newline is inserted. This is due to the nature of how the text widget works -- widget bindings happen before class bindings, and class bindings are where user input is actually inserted into the widget.
The solution is likely to adjust your bindings to happen after the class bindings (for example, by binding to <KeyRelease>
or adjusting the bindtags). Without seeing how you are doing the binding, though, it's impossible for me to say for sure that this is your problem.
Another problem is that when you get the text (with Tex2.get("1.0",END)
), you are possibly getting more text than you expect. The tkinter text widget guarantees that there is always a newline following the last character in the widget. To get just what the user entered without this newline, use Tex2.get("1.0","end-1c")
. Optionally, you may want to strip all trailing whitespace from what is in the text widget before sending it to the client.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With