使用按钮时,tuplex超出范围

时间:2012-03-08 11:34:20

标签: python tkinter

我有一个工作正常的程序但是当我尝试使用按钮时,当我运行它时,它给了我

  

文件“/usr/lib/python2.7/lib-tk/Tkinter.py”,第1413行,致电

return self.func(*args)   File "fire_buttons.py", line 193, in Plot

result=la.Graph(grids)   File "fire_buttons.py", line 181, in Graph

n=sc.shape(data)[2] IndexError: tuple index out of range

功能是:

 def Graph(self,data):
            """Make the plot"""
            plt.colormaps()
            n=sc.shape(data)[2]
            ims=[]
            for i in range(n):
                mydata=data[:,:,i]
                im=plt.imshow(mydata,cmap=plt.get_cmap('jet'))
                ims.append([im])
            return ims

按钮的一些代码:

def createWidgets(self):
        """Add widgets to main frame"""
        top_frame=Frame(self)
        self.Tree=StringVar()
        self.Tree_entry=Entry(top_frame,textvariable=self.Tree)
        self.label1=Label(top_frame,text="Probability of existence of a tree")
        self.Tree_entry.pack(side='right')
        self.label1.pack()
        top_frame.pack(side=TOP)
   ......
        bottom_frame=Frame(self)
        bottom_frame.pack(side=TOP)
        self.QUIT=Button(bottom_frame,text='Quit',command=self.quit)
        self.QUIT.pack(side=LEFT)
        self.operate=Button(bottom_frame,text='Run',command=self.Plot)
        self.operate.pack(side=LEFT)

def Plot(self):
        if (self.area.get().isdigit() and p.match(self.Tree.get())  and p.match(self.Burning.get())  and p.match(self.Lighting.get()) and p.match(self.ResistBurn.get()) and p.match(self.RegenerateTree.get()) and self.time_step.get().isdigit()):   
            la=myfire()
            grids=la.fire(int(self.area.get()),float(self.Tree.get()),float(self.Burning.get()),float(self.Lighting.get()),float(self.ResistBurn.get()),float(self.RegenerateTree.get()),int(self.time_step.get()))
            result=la.Graph(grids)
            fig=plt.gcf()
            ArtistAnimation(fig,result,interval=10,repeat=False)
            plt.show()

此外,如果我想使用幻灯片小部件,我试过像:

self.Tree_entry=Scale(top_frame,from_=0,to=1,orient=HORIZONTAL,length=1000, tickinterval=0.001)
self.Tree_entry.set(40)

我想要从0到1的值并增加0.0001.但这只是给我0和1。

-------------------- UPDATE --------------------

在没有按钮而不是Plot功能的版本中,我使用:

grids=fire(area,Tree,Burning,Lighting,ResistBurn,RegenerateTree,time_step)
result=Graph(grids)
fig=plt.gcf()
ani=ArtistAnimation(fig,result,interval=10,repeat=False)
plt.show()

它运行正常,所以也许我在剧情中有错误?

2 个答案:

答案 0 :(得分:2)

grids代码行中使用la.Graph(grids)之前,可以通过获取[2]的大小来回答您的第一个问题。也许它只是一个单项的元组? (或者不到三个,因为to=100正在给出错误。)

编辑: 您的第二个问题需要import tkinter as tk if __name__ == "__main__": root = tk.Tk() tree_entry = tk.Scale(root, from_=0, to=1, resolution=0.001) tree_entry.pack() tree_entry.set(0.5) root.mainloop() 我相信。我误读您想要介于0和1之间。您可能只看到0或1,因为您将值设置为40.

编辑:以下内容已经过测试。

{{1}}

yay tkinter

答案 1 :(得分:1)

您的错误就在这一行:

n=sc.shape(data)[2]

根据您提供的证据,这与按钮或小部件无关。你只是试图从元组中获取元素2,但元组没有那么多元素。您需要找到定义sc.shape(数据)的位置,并查看为什么它没有您认为的那么多项目。