-4
__author__ = "Jack Ashton"
from tkinter import *
import random

window = Tk()

canvas = Canvas(window, width=800, height=600, background="dark cyan")
canvas.pack_propagate(False)
placeholder = StringVar

#--------------------------------------------------------------------
#images
title_image = PhotoImage(file="title_image.png")
button_bg_image = PhotoImage(file="button_bg_image.png")
game_bg_image = PhotoImage(file="Untitled-3.png")
sprite_bg_image = PhotoImage(file="Untitled-4.png")
knight = None
dragon = None
#--------------------------------------------------------------------

def start(canvas):
    canvas.pack()
    main_menu()
    window.mainloop()

def main_menu():
    #--- globals ---#
    global title_image
    global button_bg_image
    global difficulty

    #--- images ---#
    title = canvas.create_image(402, 152, image=title_image)
    button_bg = canvas.create_image(402, 452, image=button_bg_image)

    #--- label ---#
    instruction_label = Label(text="To start the game choose a difficulty!")
    instruction_label.pack(side=TOP)

    #--- buttons ---#
    #- difficulty buttons -#
    difficulty_button_1 = Button(canvas, text="Easy", command=lambda: play_game("Easy", button_list))
    difficulty_button_1.place(width=100, x=100, y=450)
    difficulty_button_2 = Button(canvas, text="Medium", command=lambda: play_game("Medium", button_list))
    difficulty_button_2.place(width=100, x=275, y=450)
    difficulty_button_3 = Button(canvas, text="Hard", command=lambda: play_game("Hard", button_list))
    difficulty_button_3.place(width=100, x=425, y=450)
    difficulty_button_4 = Button(canvas, text="Insta-Death", command=lambda: play_game("Insta-Death", button_list))
    difficulty_button_4.place(width=100, x=600, y=450)

    #--- list of buttons ---#
    button_list = [instruction_label, difficulty_button_1, difficulty_button_2, difficulty_button_3, difficulty_button_4]
    #--- end of function ---#

def play_game(difficulty, button_list):
    #--- globals ---#
    global game_bg_image
    global sprite_bg_image
    global word_create
    global user_input

    clear_canvas(button_list) #removes start menu before the game is started

    #--- images ---#
    top_half_bg = canvas.create_image(402, 152, image=game_bg_image)
    bottom_half_bg = canvas.create_image(402, 452, image=sprite_bg_image)

    #--- labels and entry_field ---#
    labels = display_labels()
    user_input = create_entry_field()

    #--- get words ---#
    word_list = get_words() #stores a list of all the words from words.txt
    print("word list: ", word_list)
    word_create = (random.choice(word_list))
    create_text = canvas.create_text(415, 125, text=word_create, fill="black", font=("Arial", 20))

    #--- check word ---#
    window.bind("<Return>", check_word)
    #--- end of function ---#

def clear_canvas(button_list):
    canvas.delete(ALL)
    for list_item in button_list:
        list_item.destroy()
    #--- end of function ---#

def display_labels():
    level = 1
    display_level = Label(canvas, text="Level: {}".format(level), fg="white", bg="black")
    display_level.place(width=100, x=10, y=10)

    time = 1
    display_time = Label(canvas, text="Time: {}".format(time), fg="white", bg="black")

    wpm = 1
    display_wpm = Label(canvas, text="WPM: {}".format(wpm), fg="white", bg="black")

    points = 1
    display_points = Label(canvas, text="Points: {}".format(points), fg="white", bg="black")

    accuracy = 1
    display_accuracy = Label(canvas, text="Accuracy: {}".format(accuracy), fg="white", bg="black")

    label_list = [display_time, display_wpm, display_points, display_accuracy] #list of labels
    y_value = -15
    for list_item in label_list: #places labels on canvas at equal distance apart
        y_value += 25
        list_item.place(width=100, x=690, y=y_value)
    #--- end of function ---#

def create_entry_field():
    entry_field = Entry(canvas, textvariable=placeholder)
    entry_field.place(x=350, y=200)
    entry_field.focus_set()
    entered_text = entry_field.get()
    return entered_text
    #--- end of function ---#

def get_words():
    word_list = [] #stores a list of all the words from words.txt
    f = open("words.txt")
    for word in f.readlines(): #appends all words from words.txt to the word list for use in the program
        word_list.append(word.replace("\n", ""))
    return word_list

def check_word(event):
    global user_input
    global word_create

    if user_input == word_create:
        print("yup")
    else:
        print("wrong")

def end_menu():
    #--- end of function ---#
    pass

start(canvas)

When I enter the text into the entry field, even though it is the same as the word created, it still doesn't recognize this.

This program is supposed to be a typing game for a school project, it is supposed to get the text from a file and then display it on screen, when text is entered into the entry field it is supposed to check it, if correct the word changes, if false you must rewrite your word and submit it again. I cannot get it to check if the word is correct.

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

You are calling entry_field.get() immediately after creating it. You must wait to call that function until the user has a chance to type. In this case, you should call it fro inside check_word.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685