简体   繁体   English

如何使用Tkinter在Python中将Enter键连接到按钮

[英]How to connect Enter key to button in Python with Tkinter

I am developing a hangman program in Python using Tkinter, and I want to bind the enter key to my submit button I created in Tkinter. 我正在使用Tkinter在Python中开发一个hangman程序,我想将Enter键绑定到我在Tkinter中创建的Submit按钮。 I have tried different solutions, but I haven't found one that works quite yet. 我尝试了不同的解决方案,但还没有找到一种可行的解决方案。

To download the complete code and a txt file with words used in the program (I haven't packaged it yet) - use this link . 要下载完整的代码和带有程序中使用的单词的txt文件(我尚未打包),请使用此链接 When starting the code - press Single-player and then Spela to start the game. 启动代码时-按单人游戏 ,然后按Spela开始游戏。

The button labled Testa is the one I would like to bind to the enter key. 按钮标签的Testa是我要绑定到Enter键的按钮。

The code is commented in Swedish, but the Testa -button is a part of the function spel_multiplayer() and is created with the following code. 该代码用瑞典语注释,但是Testa -button是spel_multiplayer()函数的一部分,并使用以下代码创建。

testa = Button(root, text="Testa",font=("Helvetica neue",12), command=vinstkontroll) # skapar knapp för att testa gissning
testa.pack(fill=X,padx=10) # skapar knapp för att testa gissning

Any help is appreciated 😊. 任何帮助表示赞赏😊。

Try using this solution as starter for your implementation. 尝试将此解决方案用作实现的入门。

import tkinter as tk

tk = tk.Tk()
tk.geometry("100x100")

def func(event):
    print("Ah-ha! You've hit return!")

tk.bind('<Return>', func)

tk.mainloop()

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

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