简体   繁体   中英

(Tkinter) same code - NO CHANGES - will work and then not work anymore when I run it? Binding enter key to function

This is the first time I see this, but I was able to get this to work and then after re-running the program, it wouldn't work anymore, and then it would, and then it wouldn't again, when I on purpose had not changed anything.

When I press the ENTER key, sometimes the player's name will show up, but sometimes I will get a weird message instead, which makes me believe it's getting data from the Textbox before the user had input his name. What am I doing wrong?

# The program will start by explaining the rules, which are:
# The user will be asked random questions.
# For every right answer, a piece of the building will be built.
# 10 pieces will be necessary to finish it.
# if the user provides 3 wrong answers, the game will be over.
# The maximum number of questions will be 13.

from tkinter import *
import tkinter.simpledialog
import tkinter.messagebox
import random

questioner = Tk()

questioner.title(" -- Build the BUILDING! -- ")


# Explanation of rules and choice to begin or quit.
def begin():
    rules_var = tkinter.messagebox.showinfo(" -- Build the BUILDING! -- ", """Game rules: You will be asked random questions, and
if you get more than 3 of them wrong, you will lose. If you are able to answer 10 questions correctly, you win!""")

    building_game = tkinter.messagebox.askyesno(" -- Build the BUILDING -- !", "Do you want to start the game?")

    if not building_game:
        questioner.destroy()
    else:
        second_stage()


# Entering name.
def second_stage():
    name_label = Label(questioner, text="What's your name?", font=('arial', 30, 'bold')).pack()
    name_input = Text(questioner, width=30, height=1, font=('courier', 18))
    name_input.pack()
    submit_button = Button(questioner, text="Submit", command=lambda: greeting_user(name_input.get("1.0", "end-1c"))).pack()
    questioner.bind('<Key-Return>', greeting_user)


# Being welcomed.
def greeting_user(name):
    welcome_msg = f"Hi {name}, the game will start in 5 seconds."
    welcome_length = 5000

    top = Toplevel()
    top.title('Welcome!')
    Message(top, text=welcome_msg, font=20, padx=50, pady=40).pack()
    top.after(welcome_length, top.destroy)



    # greeting = Label(questioner, text="Hi " + str(name) + "! The game will start in 5 seconds.")
    # greeting.pack()
    # greeting = tkinter.messagebox.showinfo(f"Get ready!", f"Hi {name}, the game will start in 5 seconds.")
    # time.sleep(3)
    # questioner.destroy()


begin()

questioner.mainloop()

You could pass the name with lambda , use it in the same way as how you use it in command,but don't forget to pass a default argument.:

def second_stage():
    name_label = Label(questioner, text="What's your name?", font=('arial', 30, 'bold')).pack()
    name_input = Text(questioner, width=30, height=1, font=('courier', 18))
    name_input.pack()
    submit_button = Button(questioner, text="Submit", command=lambda: greeting_user(name_input.get("1.0", "end-1c"))).pack()
    questioner.bind('<Key-Return>', lambda e: greeting_user(name_input.get("1.0", "end-1c"))) # e is the default argument

More details about the default argument.

You should use the lambda anonymous function in bind method (Like in case of command parameter of Button ).

Correct line:

questioner.bind('<Key-Return>', lambda x: greeting_user(name_input.get("1.0", "end-1c")))

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