Python 为什么索引器:列表索引超出范围。当列表索引不能超出范围时?
我一直在制作一个骰子游戏,当我到达将图像分配给每个Tkinter标签以便骰子显示的部分时,我开始出现以下错误:Python 为什么索引器:列表索引超出范围。当列表索引不能超出范围时?,python,python-2.7,tkinter,Python,Python 2.7,Tkinter,我一直在制作一个骰子游戏,当我到达将图像分配给每个Tkinter标签以便骰子显示的部分时,我开始出现以下错误: Exception in Tkinter callback Traceback (most recent call last): File "C:\Python27\lib\lib-tk\Tkinter.py", line 1486, in __call__ return self.func(*args) File "C:/Users/Logan/Desktop/Dic
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1486, in __call__
return self.func(*args)
File "C:/Users/Logan/Desktop/Dice/dicelab2.py", line 56, in rollDice
self.dice[i].roll()
File "C:/Users/Logan/Desktop/Dice/dicelab2.py", line 35, in roll
self.display.config(image=Photolist[self.value-1])
IndexError: list index out of range
现在列表中有6个条目,我从self调用它。价值Self.value应该是1到6之间的随机整数。在我调用它之前,我减去1,所以它应该在照片列表的索引内。还有什么可能导致此错误?
这是我的密码:
import Tkinter
import random
Photolist=[]
class Die:
def __init__(self,frame):
self.value = random.randint(1,6)
print self.value
photo1=Tkinter.PhotoImage(file="1.gif")
photo2=Tkinter.PhotoImage(file="2.gif")
photo3=Tkinter.PhotoImage(file="3.gif")
photo4=Tkinter.PhotoImage(file="4.gif")
photo5=Tkinter.PhotoImage(file="5.gif")
photo6=Tkinter.PhotoImage(file="6.gif")
Photolist=[photo1,photo2,photo3,photo4,photo5,photo6]
self.display = Tkinter.Label(frame,
image=Photolist[self.value-1],
font=('Courier New',30),
relief='ridge',
borderwidth=5,
bg='white')
self.display.pack(side='left')
def roll(self):
self.value = random.randint(1,6)
print self.value
self.display.config(image=Photolist[self.value-1])
class DiceRoller:
def __init__(self):
gameWin = Tkinter.Tk()
gameWin.title('Dice Roller')
diceFrame = Tkinter.Frame(gameWin)
self.dice = [ ]
for i in range(5):
self.dice.append(Die(diceFrame))
rollBtn = Tkinter.Button(gameWin,
text='Roll Again',
command=self.rollDice,
font=('Courier New',30))
diceFrame.pack()
rollBtn.pack(side='bottom')
gameWin.mainloop()
def rollDice(self):
for i in range(5):
self.dice[i].roll()
dice = DiceRoller()
dice
在您的代码中,您有
Photolist=[]
所以您试图访问空列表,而不是类中的列表,我将其更改为一个属性,这样您现在就不会有问题了天哪!我怎么会错过呢??非常感谢。不用担心,randint是一个例外,它比正常值更容易出错,通常范围的上限不包含在内。我尝试了你的建议,将范围更改为“random.randint(1,5)”,但我仍然得到相同的错误。发布整个回溯时+1。太多人没有。你有没有试过检查实际值,看看你的假设是否正确?
import Tkinter
import random
# PhotoList = [] you are accessing this
class Die(object):
def __init__(self,frame):
self.value = random.randint(1,6)
print self.value
photo1=Tkinter.PhotoImage(file="1.gif")
photo2=Tkinter.PhotoImage(file="2.gif")
photo3=Tkinter.PhotoImage(file="3.gif")
photo4=Tkinter.PhotoImage(file="4.gif")
photo5=Tkinter.PhotoImage(file="5.gif")
photo6=Tkinter.PhotoImage(file="6.gif")
# make it an attribute
self.Photolist=[photo1,photo2,photo3,photo4,photo5,photo6]
self.display = Tkinter.Label(frame,
image=self.Photolist[self.value-1],
font=('Courier New',30),
relief='ridge',
borderwidth=5,
bg='white')
self.display.pack(side='left')
def roll(self):
self.value = random.randint(1,6)
print self.value
self.display.config(image=self.Photolist[self.value-1])