TypeError:' float'对象没有属性' __ getitem __'。 (蟒蛇)

时间:2015-03-25 13:46:43

标签: python tkinter

import Tkinter
import tkMessageBox
from Tkinter import *

CanvasHeight = 500
CanvasWidth = 600

画布宽度和高度设置为变量最大值的10倍。     IsGraphHidden = 0

MainWindow = Tkinter.Tk()

这是所有滑块的窗口,定义为“MainWindow”供以后使用。

Strength = DoubleVar() 
Multiple = DoubleVar()
Time = DoubleVar()

由于Tkinter插件,所有变量都设置为DoubleVar。 它需要它自己的特殊浮点数,整数和字符串才能工作。 可以使用VARIABLE.get()

将它们作为普通变量访问
coords = []
lastcoords = [0,0] 

这是我们用来继续这一行而不仅仅是从屏幕的一角画出一堆线条。

Plot = DoubleVar()

StrengthScale = Scale( MainWindow, variable = Strength, orient = HORIZONTAL,label="Strength")

MultipleScale = Scale( MainWindow, variable = Multiple, from_ = float(0), to = float(1), resolution = float(0.01), orient = HORIZONTAL, label="Multiple")

TimeScale = Scale( MainWindow, variable = Time, orient = HORIZONTAL, from_ = int(0), to = int(120), label="Time")

这些是按钮的程序,以及代码的其余部分。

def Calculate():
  answer = float(Strength.get())*float(Multiple.get())
  tkMessageBox.showinfo("Answer:", answer)

def PrepPlot():
 global IsGraphHidden
 global coords
 global lastcoords
 lastcoords0 = lastcoords[0]
 lastcoords1 = lastcoords[1]
 coords.append(lastcoords0)
 coords.append(lastcoords1)
 coords.append(Time.get()*5)
 coords.append(Strength.get()*Multiple.get()*5)
 lastcoords = Time.get()*5
 lastcoords = Strength.get()*Multiple.get()*5

if IsGraphHidden == 0:
 Graph = Canvas(MainWindow, width = CanvasWidth, height = CanvasHeight, bg = "white")
 Graph.create_line(coords, fill = "black")
 Graph.grid(row=5, column=1)
else:
 Graph.destroy()
 Graph.delete("all")
 Graph.create_line(coords, fill = "black")
 Graph.grid(row=5,column=1)
 IsGraphHidden = 1

def DisplayPoints():
 PointWindow = Tkinter.Tk()
 Text = Label(PointWindow, textvariable = "Hi there", relief=RAISED)
 Text.pack()
 PointWindow.mainloop() #Work in progress, nothin' to see here.

Button = Tkinter.Button(MainWindow, text= "Calculate",command = Calculate)
PrepButton = Tkinter.Button(MainWindow, text = "Plot", command = PrepPlot) #The text is the text on the button.
DisplayButton = Tkinter.Button(MainWindow, text = "Display Plots", command = DisplayPoints)

MultipleScale.grid(row=0,column=0)
StrengthScale.grid(row=1,column=0)
TimeScale.grid(row=1,column=2)
PrepButton.grid(row=2,column=1)
Button.grid(row=4,column=1)
DisplayButton.grid(row=3,column=1)

MainWindow.mainloop()

我需要一些浮动对象getitem错误的帮助,我正在为曼彻斯特大学的工作经验做这个代码......

1 个答案:

答案 0 :(得分:4)

您将lastcoords列表替换为浮点值:

lastcoords = Time.get()*5
lastcoords = Strength.get()*Multiple.get()*5

这样下次就行了:

lastcoords0 = lastcoords[0]

引发异常,因为您无法对浮点值使用订阅。

你想要设置一个新列表:

lastcoords = [Time.get() * 5, Strength.get() * Multiple.get() * 5]