python - 使用按钮时 tuplex 超出范围

标签 python tkinter

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

File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1413, in call

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。

--------------------更新--------------------

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

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

它运行得很好,所以也许我的情节有问题?

最佳答案

您的第一个问题可以通过在 la.Graph(grids) 代码行中使用grids 的大小来回答。也许它只是一个包含单个项目的元组? (或者无论如何少于三个,因为 [2] 给出了错误。)

编辑: 我相信你的第二个问题需要to=100我误读了你想要在0和1之间。你可能由于您将值设置为 40,因此只能看到 0 或 1。

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

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

yay tkinter

关于python - 使用按钮时 tuplex 超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9617003/

相关文章:

python - 针对远程服务器运行 Flask 测试

python - 在 Python 中查找文本中所有出现的整数

Python Tkinter 长途电话

python - 将 sqlite3 与 Tkinter 链接

python - 从 Treeview 中选择自动将字符串数字转换为整数

python - Win10/Pyinstaller ImportError : numpy. core.multiarray导入失败

python - Pandas 在回溯窗口中找到每个用户的第一个订单的最小日期

python - Tkinter:在循环中更新标签

python - 无法将字符串从python发送到arduino

python - tkinter 标签不尊重 justify 选项?