0

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 😊.

4
  • " I have tried different solutions, but I haven't found one that works quite yet." Which ones have you tried? May as well name them, so we don't waste your time recommending them again.
    – Kevin
    May 15, 2018 at 12:03
  • Can't find the posts I used as a reference, I haven't worked in this for the past few months. Even if the same solutions are suggested, I might gain som insight into why they didn't work. May 15, 2018 at 12:09
  • Please don't link to code on another site. Take the time to create a minimal reproducible example and include it in your question. May 15, 2018 at 12:10
  • Hi @M.Otterman, I have written a solution for you. If you find it useful please upvote it and/or mark it as solution. Thanks! :)
    – Pitto
    Jun 20, 2018 at 7:52

1 Answer 1

3

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()
1
  • Hi @M. Otterman, did my solution help you? If so please upvote it and/or markit as solution. Thanks!
    – Pitto
    Feb 19, 2019 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.