Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to erase everything from the tkinter text widget?

Tags:

python

tkinter

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)

like image 839
Andrey Avatar asked Mar 16 '11 07:03

Andrey


People also ask

How do you clear a textbox in Python?

Type something inside the textbox, and then, click the “Delete” button. It will erase the content inside the textbox.

How do you delete everything in tkinter?

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.

How do you clear a label in Python?

If we want to delete a label that is defined in a tkinter application, then we have to use the destroy() method.

How do you clear the entry widget after a button is pressed in tkinter?

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.


1 Answers

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.

like image 166
Bryan Oakley Avatar answered Oct 05 '22 00:10

Bryan Oakley