简体   繁体   English

有什么方法可以将Enter键绑定到Python中的Tkinter.Toplevel()窗口?

[英]Are there any ways to bind the enter key to a Tkinter.Toplevel() window in Python?

I have a tk window opening another. 我有一个打开另一个窗口的窗口。 This secondary window is used as my input for a program. 此辅助窗口用作我对程序的输入。 I would like to read the results each time the cartage return key is pressed. 我想在每次按回车键时读取结果。 I read somewhere that the method I am trying to use (below) only works for root .Tk() windows. 我在某处读到,我尝试使用的方法(下)仅适用于root .Tk()窗口。

input_window.bind('<Return>',lambda: function_to_save_data (args) ) 

Is there a way to get around this, or an alternative way to do such a thing? 有没有解决这个问题的方法,或者有替代方法可以做到这一点? (I have tried this, and it fails to work, and does not bug out, thus the question above) (我已经尝试过了,但是它行不通,并且没有出错,因此出现了上面的问题)

Bind works for any window, there are no special cases. 绑定适用于任何窗口,没有特殊情况。

The problem you are experiencing is likely due to the fact that top level windows may not get keyboard focus. 您遇到的问题很可能是由于顶层窗口可能无法获得键盘焦点。 When you press a key, it is the window with focus that processes the event. 当您按下一个键时,将是具有事件焦点的窗口。

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

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