Tkinter游戏:玩家名称不会显示

2024-05-16 01:04:57 发布

您现在位置:Python中文网/ 问答频道 /正文

程序无法显示玩家的姓名,玩家单击“提交”按钮后,应捕获该姓名

我可能可以用Entry小部件来做,但选择文本小部件可以更容易地更改高度和宽度

我做错了什么

# 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


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()
    getting_input = name_input.get("1.0", "end-1c")
    submit_button = Button(questioner, text="Submit", command=lambda: greeting_user(getting_input)).pack()
    
    
# Being greeted.
def greeting_user(name):
    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 {}, the game will start in 5 seconds.")


begin()

questioner.mainloop()

Tags: thenameyougameinputtkinterbestart
1条回答
网友
1楼 · 发布于 2024-05-16 01:04:57

您在代码中错误地创建了变量。您应该在函数本身中获取文本框的值。选中此项:

# 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


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))).pack()
    
    
# Being greeted.
def greeting_user(name):
    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 {}, the game will start in 5 seconds.")


begin()

questioner.mainloop()

相关问题 更多 >