Search code examples
pythonpython-3.xnumpytkintertkinter-entry

Float object cannot be interpreted as an integer when calling a tkinter entry box value


I am building out a calculator that takes in various statistical distributions of variables and multiplies them together. I am having issues returning values from my entry boxes to be used in my calculations. All 6 numbers that need to be entered into the calculator are float variables.

I have abbreviated and altered the code slightly to make it easier to understand the error.

My thought process is this.

  1. I initially define all variable that will be called from the entry box using StringVar() assignments
  2. I then set up the entry boxes to take the input variables set up in step 1
  3. I call the entered items into the calculation function by by using the variablename.get() method and cast them as float variables
  4. I use the numpy random.choice function to generate a discrete distribution from the inputs provided.

step 4 is where I am having issues. I am getting a type error stating that "'float object' cannot be interpreted as an integer". I don't know where Python is trying to interpret an integer at in this script. I thought I had cast all of my variables to float.

from tkinter import *
import numpy as np

window = Tk()
window.geometry('1100x700')
window.title('Discrete Gas Calculator')


def example():
    #area variables
    dalv = float(alv.get())
    dabv = float(abv.get())
    dahv = float(ahv.get())
    dalp = float(alp.get())
    dabp = float(abp.get())
    dahp = float(ahp.get())
    dnumbint = float(numbint.get())

    problemex= np.random.choice([dalv,dabv,dahv], dnumbint, p=[dalp,dabp,dahp] )
    print(problemex)


#Number of iterations label
numbitlabel = Label(text = 'Number of Iterations', fg = 'black', bg = 'white', width = 17 ).grid(row = 9, column = 0)
#y labels
arealabel = Label(text = 'Area', fg = 'black', bg = 'white', width = 17 ).grid(row = 1, column = 0)
#x labels
unitslabel = Label(text = 'Units', fg = 'black', bg = 'white', width = 17 ).grid(row = 0, column = 1)
lvlabel = Label(text = 'Low Value', fg = 'black', bg = 'white', width = 17 ).grid(row = 0, column = 2)
bvlabel = Label(text = 'Base Value', fg = 'black', bg = 'white', width = 17 ).grid(row = 0, column = 3)
hvlabel = Label(text = 'High Value', fg = 'black', bg = 'white', width = 17 ).grid(row = 0, column = 4)
lplabel = Label(text = 'Low Probability', fg = 'black', bg = 'white', width = 17 ).grid(row = 0, column = 5)
bplabel = Label(text = 'Base Probability', fg = 'black', bg = 'white', width = 17 ).grid(row = 0, column = 6)
hplabel = Label(text = 'High Probability', fg = 'black', bg = 'white', width = 17 ).grid(row = 0, column = 7)


calcbutton1 = Button(text='Calculate Volumes', width=15, height = 2,fg = 'black', bg = 'white', command = example).place(x = 200, y = 250)

#initially define variables that will be called in entry box
alv = StringVar()
abv = StringVar()
ahv = StringVar()
alp = StringVar()
abp = StringVar()
ahp = StringVar()


#number of iterations variable
numbint = StringVar()


#this section contains the entry box creation for each variable

#area entry boxes
alventry = Entry(textvariable = alv).grid(row = 1, column = 2)
abventry = Entry(textvariable = abv).grid(row = 1, column = 3)
ahventry = Entry(textvariable = ahv).grid(row = 1, column = 4)
alpventry = Entry(textvariable = alp).grid(row = 1, column = 5)
abplventry = Entry(textvariable = abp).grid(row = 1, column = 6)
ahpventry = Entry(textvariable = ahp).grid(row = 1, column = 7)

#number of iterations entry box
numbintentry = Entry(textvariable = numbint).grid(row = 9, column = 1)

window.mainloop()

Please let me know if I can clarify anything and I will do my best!

Thanks for reading this.


Solution

  • The issue is with this line below:

    problemex= np.random.choice([dalv,dabv,dahv], dnumbint, p=[dalp,dabp,dahp] )

    If you see the traceback error and read the numpy documentation , the error is quite obvious.

    The size parameter in this case dumbint must be an integer but you have passed a float. So typecast it into int and you should be fine.

    In fact replacing with the line below makes the program work:

    problemex= np.random.choice([dalv,dabv,dahv], int(dnumbint), p=[dalp,dabp,dahp] )