简体   繁体   English

如何从tkinter文本小部件中删除所有内容?

[英]How to erase everything from the tkinter text widget?

Im working on a GUI for some chat programme. 我正在为一些聊天程序的GUI工作。 For user's input I have Text() widget, messages are sent via "Return" and after that I clean the Text(). 对于用户的输入我有Text()小部件,消息通过“Return”发送,之后我清理Text()。 But as hard as I tried I cant remove the last "\\n" which Return button creates. 但是,尽管我尝试了很难,但我无法删除Return按钮创建的最后一个“\\ n”。

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) 期待优惠)

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). 解决方案可能会类绑定之后调整绑定(例如,通过绑定到<KeyRelease>或调整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. 另一个问题是当你得到文本时(使用Tex2.get("1.0",END) ),你可能会获得比你预期的更多的文本。 The tkinter text widget guarantees that there is always a newline following the last character in the widget. tkinter文本小部件保证小部件中的最后一个字符后面始终有换行符。 To get just what the user entered without this newline, use Tex2.get("1.0","end-1c") . 要获得用户在没有此换行符的情况下输入的内容,请使用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. (可选)您可能希望从文本窗口小部件中删除所有尾随空格,然后再将其发送到客户端。

With a string you can remove last character with: 使用字符串,您可以删除最后一个字符:

msg = msg[:-2]

You could also remove all whitespace from the end of the string (including newlines) with: 您还可以使用以下命令从字符串末尾删除所有空格(包括换行符):

msg = msg.rstrip()

OK. 好。 After reading your comment, I think you should check this page: The Tkinter Text Widget 阅读完评论后,我认为您应该查看此页面: Tkinter Text Widget

where it is explained: 在哪里解释:

"If you insert or delete text before a mark, the mark is moved along with the other text. To remove a mark, you must use the *mark_unset* method. Deleting text around a mark doesn't remove the mark itself." “如果在标记之前插入或删除文本,则标记将与其他文本一起移动。要删除标记,必须使用 * mark_unset * 方法。删除标记周围的文本不会删除标记本身。”

----EDIT---- - - 编辑 - -

My mistake, disregard the above paragraph. 我的错误,无视上一段。 Marks have nothing to do with the newline. 标记与换行符无关。 As Bryan explains in his answer: The tkinter text widget guarantees that there is always a newline following the last character in the widget. 正如Bryan在他的回答中解释的那样: tkinter文本小部件保证小部件中的最后一个字符后面总是有换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM