__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)
当我在输入字段中输入文本时,即使它与创建的单词相同,它仍然无法识别。
这个程序应该是学校项目的打字游戏,它应该从文件中获取文本,然后在屏幕上显示,当文本输入到输入字段时,它应该检查它,如果纠正单词更改,如果为false,则必须重写单词并再次提交。我无法检查这个词是否正确。
答案 0 :(得分:0)
您在创建后立即致电entry_field.get()
。您必须等待调用该函数,直到用户有机会输入。在这种情况下,您应该在check_word
内召唤它。