我正在尝试使用tkinter制作GUI。在此GUI中,用户必须猜测混乱的短语。问题是,startgame
函数中显示的列表仅返回最后一个元素。而且,我不会每次都根据正确的猜测得到不同的混乱短语,而是不断获得相同的混乱短语,只是其混乱顺序改变了。
这是完整的代码。
#GUI app logic
import random
from tkinter import *
tv_shows = ['Game of Thrones','Friends','How I Met Your Mother','Breaking Bad','Narcos','Flash','Arrow','Big Bang Theory','Walking Dead','Agents Of Shield','Blue Planet 2',
'Legion','The Grand Tour','Band Of Brothers','Westworld','Sherlock','The Punisher', 'True Detective', 'Daredevil','Luke Cage','Jessica Jones','Iron Fist','Stranger Things',
'Rick and Morty', 'House of Cards', '13 Reasons Why','House MD', 'Castle','Doctor Who','Dexter','Suits']
score = 0
user_list=[]
time= 0
for i in range(1,11,1):
user_list.append(random.choice(tv_shows))
print(user_list)
def jumble(word):
jum=" "
while word:
pos=random.randrange(len(word))
jum +=word[pos]
word=word[:pos]+word[(pos + 1):]
return jum
def sen_jumble(w):
l=[]
for i in w:
new = jumble(i)
l.append(new)
l=" ".join(l)
return l
def start_game(event):
timetaken()
jumb_list = []
global score
for i in user_list:
r1 = sen_jumble(i.split())
jumb_list.append(r1)
guess.focus_set()
if time>0:
for i in range(0,10,1):
word.config(text = "Jumbled Word => " + str(jumb_list[i]))
if guess.get().lower() == user_list[i].lower():
score += 1
score_display.config(text = str(score))
guess.delete(0,END)
def timetaken():
global time
if time>=0:
time += 1
timeout.config(text = "Time : "+ str(time))
timeout.after(1000, timetaken)
main = Tk()
main.title("Guess What")
main.geometry("375x200")
rules = Label(main, text="Guess the correct Tv-show name for the jumbled one shown")
rules.pack()
word = Label(main)
word.pack()
score_display = Label(main)
score_display.pack()
timeout = Label(main)
timeout.pack()
guess = Entry(main)
main.bind('<Return>',start_game)
guess.pack()
guess.focus_set()
main.mainloop()
答案 0 :(得分:1)
您的代码存在问题,<RETURN>
已绑定到start_game()
,因此尽管您认为自己在猜测和答案中循环,但事实是,用户每次点击<RETURN>
根据他们的回答,start_game()
的新实例开始了!相反,我们需要从start_game()
事件(又名<RETURN>
)中拆分设置新单词(又名score_game()
)。
我已按照以下步骤重新编写了您的代码-为简单起见,我将计时代码完全抛弃了,因为它不属于您的问题:
from random import shuffle
from tkinter import *
JUMBLED, PLAINTEXT = 0, 1
tv_shows = [
'Game of Thrones', 'Friends', 'How I Met Your Mother', 'Breaking Bad', 'Narcos', 'Flash', 'Arrow', 'Big Bang Theory',
'Walking Dead', 'Agents Of Shield', 'Blue Planet 2', 'Legion', 'The Grand Tour', 'Band Of Brothers', 'Westworld'
'Sherlock', 'The Punisher', 'True Detective', 'Daredevil', 'Luke Cage', 'Jessica Jones', 'Iron Fist', 'Stranger Things',
'Rick and Morty', 'House of Cards', '13 Reasons Why', 'House MD', 'Castle', 'Doctor Who', 'Dexter', 'Suits'
]
def word_jumble(word):
letters = list(word)
shuffle(letters)
return "".join(letters)
def sentence_jumble(w):
words = w.split()
shuffle(words)
return " ".join(word_jumble(word) for word in words)
def start_game():
global jumble
guess.focus_set()
jumble = jumble_list.pop()
word.config(text="Jumbled Words => " + jumble[JUMBLED])
def score_game(event):
global score
if guess.get().lower() == jumble[PLAINTEXT].lower():
guess.delete(0, END)
score += 1
score_display.config(text=str(score))
if jumble_list:
score_display.after(500, start_game)
else:
main.unbind('<Return>')
shuffle(tv_shows)
jumble_list = [(sentence_jumble(title), title) for title in tv_shows]
score = 0
jumble = None # a tuple with (jumbled, plaintext)
main = Tk()
main.title("Guess What")
main.geometry("375x200")
Label(main, text="Guess the TV show name from the jumbled one shown").pack()
word = Label(main)
word.pack()
score_display = Label(main)
score_display.pack()
score_display.config(text=str(score))
guess = Entry(main)
guess.pack()
guess.focus_set()
main.bind('<Return>', score_game)
start_game()
main.mainloop()
您需要在游戏结束时重新添加。您可以通过截断jumble_list
来限制回合数。
答案 1 :(得分:0)
此部分:
for i in range(0,10,1):
word.config(text = "Jumbled Word => " + str(jumb_list[i]))
if guess.get().lower() == user_list[i].lower():
score += 1
score_display.config(text = str(score))
guess.delete(0,END)
您的for循环不会等待您完成猜测。它总是完成循环并配置word
小部件10次,直到到达最后一个。您可以摆脱for
循环并根据当前得分提高列表索引:
def start_game(event):
timetaken()
jumb_list = []
global score
for i in user_list:
r1 = sen_jumble(i.split())
jumb_list.append(r1)
guess.focus_set()
word.config(text = "Jumbled Word => " + str(jumb_list[score]))
if guess.get().lower() == user_list[score].lower():
score += 1
score_display.config(text = str(score))
try:
word.config(text="Jumbled Word => " + str(jumb_list[score]))
except IndexError:
word.config(text="You Win!")
score = 0
guess.delete(0,END)