刽子手无法一一更新图片 [英] Hangman cannot update image one by one

查看:21
本文介绍了刽子手无法一一更新图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我在开头设置了 self.wrong = 0,我的目标是在猜错字母时将图像更新到下一个.我试了很多次,这个错误信息仍然出现.如果玩家猜错了,我已经设置了 self.wrong += 1.那么变量 self.wrong 应该是一个整数,所以我不明白为什么 self.photo_list[self.wrong] 会有索引错误?我已经指出了 self.photo_list[index] 并且索引是一个整数.谁能帮我解决上述错误?

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?

推荐答案

如果 self.wrong 是猜错的次数,那么应该在 letter_guess 里面更新不在 display_word() 中:

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')

这篇关于刽子手无法一一更新图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆