调用tkinter输入框值时,不能将Float对象解释为整数

问题描述 投票:0回答:1

我正在构建一个计算器,它接收各种变量的统计分布并将它们相乘。我有问题从我的输入框中返回值以用于我的计算。需要输入计算器的所有6个数字都是浮点变量。

我略微缩写并修改了代码,以便更容易理解错误。

我的思维过程就是这样。

  1. 我最初使用StringVar()赋值定义将从输入框调用的所有变量
  2. 然后我设置输入框以获取在步骤1中设置的输入变量
  3. 我通过使用variablename.get()方法将输入的项调用到计算函数中,并将它们转换为浮点变量
  4. 我使用numpy random.choice函数从提供的输入生成离散分布。

第4步是我遇到问题的地方。我收到一个类型错误,指出“'浮动对象'不能被解释为整数”。我不知道Python在这个脚本中试图解释整数的位置。我以为我把所有变量都抛到了浮动状态。

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

如果我能澄清任何事情,请告诉我,我会尽我所能!

感谢您阅读本文。

python python-3.x numpy tkinter tkinter-entry
1个回答
0
投票

问题在于以下这一行:

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

如果您看到traceback错误并阅读numpy文档,则错误非常明显。

在这种情况下sizedumbint参数必须是整数,但是你已经通过了float。所以把它变成int并且你应该没问题。

事实上,替换下面的行使程序工作:

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

© www.soinside.com 2019 - 2024. All rights reserved.