Hangman cannot update image one by one

主宰稳场 提交于 2021-01-29 07:04:54

问题


from tkinter import *
from tkinter import messagebox
import random


class homewindow(object):
    def __init__(self,win):
        self.words = ['rainbow','geography','testimonial','science','effort','amusing']
        self.hidden_wd = random.choice(self.words)
        self.words.remove(self.hidden_wd)
        self.photo_list = [PhotoImage(file='1.gif'),
                      PhotoImage(file='2.gif'),PhotoImage(file='3.gif'),
                      PhotoImage(file='4.gif'),PhotoImage(file='5.gif'),
                      PhotoImage(file='6.gif'),PhotoImage(file='7.gif'),
                      PhotoImage(file='8.gif'),PhotoImage(file='9.gif'),PhotoImage(file='10.gif')]

        self.hidden_wd = self.hidden_wd.strip()
        print(self.hidden_wd)

        self.guess_list = []
        self.times = 9
        self.wrong = 0
        
        self.win = win
        self.win.title('Hangman')
        self.win.geometry('600x400')
        self.lb1 = Label(win,image=self.photo_list[0])
        self.lb1.image = self.photo_list[0]
        self.lb1.grid(row=0,rowspan=3,column=0)
        self.lb2 = Label(win,text='_'*len(self.hidden_wd))
        self.lb2.grid(row=0,column=1,columnspan=3)
        self.lb3 = Label(win,text='You have '+ str(self.times) + ' left')
        self.lb3.grid(row=4,column=1,columnspan=3)
        self.e = Label(win,text='Enter letter: ')
        self.e.grid(row=1,column=1,columnspan=3)
        self.display_word()
        self.display_guess()
        self.guess_input()

    def letter_guess(self,letter):
        self.data = self.entry.get()
        self.guess_list += self.data
        if self.data not in self.hidden_wd:
            self.times -= 1
            if self.times == 0:
                messagebox.showwarning('Lose','Game Over')
        self.display_word()
        self.display_guess()

    def display_word(self):
        self.guessed = ''
        for i in self.hidden_wd.lower():
            if i in self.guess_list:
                self.guessed += i
            elif i not in self.guess_list:
                self.guessed += '*'
                self.wrong += 1
        self.lb1.configure(image=self.photo_list[self.wrong])
        self.lb2.configure(text=self.guessed)
        if '*' not in self.guessed:
            messagebox.showinfo('Hangmaner','Congraulations')

    def display_guess(self):
        self.wrong_guess = []
        for i in self.guess_list:
            if i not in self.hidden_wd.lower():
                self.wrong_guess += i
        Label(self.win,text=self.wrong_guess).grid(row=5,column=1)
                
                
    def guess_input(self):
        self.var = StringVar()
        self.entry = Entry(self.win,textvariable=self.var)
        self.entry.bind('<Return>',self.letter_guess)
        self.entry.grid(row=2,column=1,columnspan=3)
        
if __name__ == '__main__':
    HangMan = Tk()
    hm = homewindow(HangMan)
    HangMan.mainloop()
        

Error:
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python38\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "E:\Python\Python Projects Fun\hangman\hangman_code.py", line 49, in letter_guess
    self.display_word()
  File "E:\Python\Python Projects Fun\hangman\hangman_code.py", line 60, in display_word
    self.lb1.configure(image=self.photo_list[self.wrong])
IndexError: list index out of range

I have set self.wrong = 0 at the beginning and my aim is to update the image to the next one if guessing the wrong letter. I tried so many times and this error message still comes out. I already set if the player guess wrongly then self.wrong += 1. Then the variable self.wrong should be an integer so I don't understand why self.photo_list[self.wrong] will have index error? I have already indicated the self.photo_list[index] and the index is an integer. Can anyone help me to fix the above error?


回答1:


If self.wrong is the number of wrong guesses, then it should be updated inside letter_guess and not in display_word():

def letter_guess(self,letter):
    self.data = self.entry.get()
    self.guess_list += self.data
    if self.data not in self.hidden_wd:
        self.wrong += 1  # increment self.wrong
        self.times -= 1
        if self.times == 0:
            messagebox.showwarning('Lose','Game Over')
    self.display_word()
    self.display_guess()

def display_word(self):
    self.guessed = ''
    for i in self.hidden_wd.lower():
        if i in self.guess_list:
            self.guessed += i
        else:
            self.guessed += '*'
            # removed self.wrong += 1
    self.lb1.configure(image=self.photo_list[self.wrong])
    self.lb2.configure(text=self.guessed)
    if '*' not in self.guessed:
        messagebox.showinfo('Hangmaner','Congraulations')


来源:https://stackoverflow.com/questions/63168956/hangman-cannot-update-image-one-by-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!