测试完毕,望采纳!
from tkinter import *
import random
import time
WORD = '''concern important because concert importance
excellent universe usually people chinese banana
republic without paying attention beautiful
mistake future arrogant stagnant'''.split()
def random_word():
global word
word=random.choice(WORD)
word_list=list(word)
random.shuffle(word_list)
return ''.join(word_list)
def guess_word(event):
global s
if event.keysym == 'Escape':
root.quit()
if event.keysym == 'BackSpace':
s=s[:-1]
elif event.keysym == 'Return':
judge_result()
elif event.keysym == 'space':
s+=' '
elif len(event.keysym)==1:
s+=event.keysym
lb_guess.config(text=s)
def judge_result():
global s
if s == word:
lb_res.config(text=f'真棒,你猜对了!用时{round(time.time()-t1)}秒')
lb_next.focus_set()
lb_next.config(text='Y')
else:
lb_res.config(text=f'猜错了,再试试吧')
s = ''
def to_continue(event):
if event.keysym == 'Escape':
root.quit()
elif event.keysym == 'Return':
guess_next(lb_next['text'])
elif event.keysym.upper() == 'Y' or event.keysym.upper() == 'N':
lb_next.config(text=event.keysym.upper())
def guess_next(a):
global t1
global s
if a == 'Y':
s = ''
lb_word.config(text=random_word())
lb_guess.focus_set()
lb_guess.config(text='')
lb_res.config(text='')
t1=time.time()
else:
root.quit()
s = ''
word = ''
root=Tk()
root.title("猜单词游戏")
root.geometry('400x250+300+200')
root.resizable(False,False)
Label(root,text='欢迎参加猜单词游戏',font=('Arial',12)).place(x=130,y=10)
Label(root,text='把字母组合成一个正确的单词',font=('Arial',12)).place(x=100,y=35)
Label(root,text='乱序后的单词:',font=('Arial',12)).place(x=50,y=85)
Label(root,text='请你猜:',font=('Arial',12)).place(x=50,y=135)
Label(root,text='是否继续(Y/N):',font=('Arial',12)).place(x=50,y=185)
lb_word=Label(root,text=random_word(),font=('Arial',12))
lb_word.place(x=170,y=85)
lb_guess=Label(root,text='请输入单词',fg='blue',font=('Arial',12))
lb_guess.bind('<Key>',guess_word)
lb_guess.focus_set()
lb_guess.place(x=120,y=135)
lb_res=Label(root,font=('Arial',12))
lb_res.place(x=50,y=160)
lb_next=Label(root,font=('Arial',12))
lb_next.bind('<Key>',to_continue)
lb_next.place(x=190,y=185)
t1 = time.time()
root.mainloop()