Tkinter - 绑定 crtl + Enter 在文本框中创建新行(文本)

发布于 2025-01-11 12:41:50 字数 209 浏览 2 评论 0原文

我已经做到了,当有人按 Enter 时,文本框中的文本将被删除,数据将传输到另一个方法,但是,我还想绑定 ctrl+Enter 以在文本框中创建一个新行,例如 Zoom 的聊天和其他一些平台的聊天,但是,我不知道如何通过 Tkinter 执行此操作,也不知道如何获取数据,并将新行替换为 Enter 在文本框中创建的“\n”,因为似乎新行中的新行文本框实际上不是每当我将其视为字符串时,都会使用“\n”。

I already made that when somebody presses Enter the text in the textbox will be deleted and data will be transferred to another method, however, I want to bind also ctrl+Enter to make a new line in the textbox, like Zoom's chat and some other platforms' chat, however, I don't know how to do so over Tkinter, and how to take the data, and replace the new line with "\n" that the Enter created in textbox since it seems that the new line in the textbox isn't actually "\n" whenever I take it as a string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帥小哥 2025-01-18 12:41:50

要绑定 control-Enter 以插入文字换行符并且不执行默认操作,您可以按照以下示例执行操作。请注意,该事件是,并且该函数返回字符串“break”以防止其他处理程序处理该事件。


def insert_newline(event):
    event.widget.insert("insert", "\n")
    return "break"

text = tk.Text(...)
text.bind("<Control-Return>", insert_newline)

To bind control-Enter to insert a literal newline and to not do the default action, you could do it like in the following example. Note that the event is <Control-Return>, and the function returns the string "break" to prevent other handlers from handling the event.


def insert_newline(event):
    event.widget.insert("insert", "\n")
    return "break"

text = tk.Text(...)
text.bind("<Control-Return>", insert_newline)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文