ValueError: could not convert string to float:
Tofloor
poster avatar
gentoo
deepin
2013-04-14 22:40
Author
  1. from Tkinter import *
  2. from ast import *
  3. function = lambda x : x ** 2
  4. class App:
  5.     def areaPiece(self,mark,step):
  6.         lengthUp = function(mark)
  7.         lengthDown = function(mark + step)
  8.         height = step
  9.         area = (lengthUp + lengthDown) * height / 2
  10.     def calculator(self,x,x0):
  11.         y = function(x)
  12.         y0 = function(x0)
  13.         y1 = y - y0
  14.         area0 = 0
  15.         mark0 = 0
  16.         step = x / 10000
  17.         while mark0 < x0:
  18.             area0 += areaPiece(mark0,step)
  19.             mark0 += step
  20.         mark1 = x0
  21.         areaRight = 0
  22.         while mark1 < x:
  23.             areaRight += areaPiece(mark1,step)
  24.             mark1 += step
  25.         areaAllRight = (x - x0) * y
  26.         area1 = areaAllRight - areaRight
  27.         delta = area0 * y1 + area1 * y0
  28.     def callback(self):
  29.         
  30.         delta = calculator(x,x0)
  31.         Rstring.set(delta)
  32.         return area
  33.     def __init__(self,master):
  34.         Xlabel = Label(master,
  35.                 text = "please input x:")
  36.         Xlabel.pack(side = LEFT)
  37.         
  38.         Xtext = StringVar()
  39.         Xentry = Entry(master,
  40.                 textvariable = Xtext)
  41.         Xentry.pack(side = LEFT)
  42.         
  43.         X0label = Label(master,
  44.                 text = "please input x0:")
  45.         X0label.pack(side = LEFT)
  46.         
  47.         X0entry = Entry(master)
  48.         X0entry.pack(side = LEFT)
  49.         
  50.         Rstring = StringVar()
  51.         Rlabel = Label(master,
  52.                 textvariable = Rstring)
  53.         Rstring.set('RESULT')
  54.         Rlabel.pack(side = LEFT)
  55.         x = float(Xtext.get())
  56.         #x0 = int(X0entry['text'])
  57.         #x = literal_eval(Xentry.get())
  58.         #x0 = literal_eval(X0entry)
  59.         calButton = Button(master,
  60.                 command = lambda:self.calculator(x,x0),
  61.                 text = "CAL")
  62.         calButton.pack()
  63.         #calButton.bind('',lambda :callback())
  64.    
  65. mainWin = Tk()
  66. app = App(mainWin)
  67. mainWin.mainloop()
Copy the Code

怎么样才能将文本框中的字符串转换成浮点型数字进行计算??
Reply Favorite View the author
All Replies

No replies yet