简体   繁体   中英

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. 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 . When starting the code - press Single-player and then Spela to start the game.

The button labled Testa is the one I would like to bind to the enter key.

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(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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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