Tkinter中的游戏:玩家名称未显示

时间:2020-06-25 03:21:29

标签: python tkinter text get widget

程序无法显示播放器的名称,一旦播放器单击“提交”按钮,就应该捕获该名称。

我可能可以使用Entry小部件来做到这一点,但是选择了Text,可以更轻松地更改高度和宽度。

我在做什么错了?

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

1 个答案:

答案 0 :(得分:0)

您在代码中错误地创建了变量。您应该在函数本身中获取文本框的值。检查一下:

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