简体   繁体   English

tkinter - 如何在按下快捷键时停止用户输入

[英]tkinter - how to stop user typing when shortcut key is pressed

I've created a textbox with tkinter.我用 tkinter 创建了一个文本框。 The following code stops a newline being created when Ctrl+Return is pressed.以下代码在按下 Ctrl+Return 时停止创建换行符。

textbox = Text()
textbox.pack()
textbox.bind("<Control-Return>", lambda event: "break")

However, I also want to call a function when the user types Ctrl+Return.但是,当用户键入 Ctrl+Return 时,我还想调用 function。 The following calls the function, but then creates a newline:下面调用 function,然后创建一个换行符:

textbox = Text()
textbox.pack()
textbox.bind("<Control-Return>", lambda event: "break")
textbox.bind("<Control-Return>", lambda event: my_func())

If I swap the third and fourth line, there is no newline, but the function isn't called either.如果我交换第三行和第四行,则没有换行符,但也不会调用 function。

Is there a way of both preventing a newline and calling the function?有没有办法既防止换行又调用 function?

Create a proper function, then it's easy to do whatever you want.创建一个合适的 function,然后就可以轻松为所欲为。

def handle_control_return(event):
    my_func()
    return "break"

textbox.bind("<Control-Return>", handle_control_return)

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

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