Kivy 中 -: 'str' 和 'int' 不支持的操作数类型

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

我需要计算ruffier指数,但我遇到了这个问题。我该如何解决它?

def ruffier_index(P1, P2, P3):
    global index
    intermediate = 4*(P1+P2+P3)
    index = (intermediate-200)/10

    return index

bpm = TextInput(multiline = False, size_hint=(None, None), width = 100,  height = 30, pos_hint = {'center_x' : 0.4, 'center_y' : 0.3}, input_filter = 'int')
        self.bpm1 = bpm.text

我将此变量转换为 int,但遇到了另一个问题

self.bpm1 = bpm.text

invalid literal for int() with base 10: ''

python kivy textinput
1个回答
0
投票

您正在尝试将空字符串转换为整数,这是不可能的。发生这种情况是因为

bpm.text
属性为空,直到用户在
TextInput
小部件中输入某个值。要解决此问题,您需要在将
bpm.text
属性转换为整数之前检查它是否不为空。例如,您可以使用如下所示的 try- except 块:

try:
    self.bpm1 = int(bpm.text)
except ValueError:
    # Handle the case when bpm.text is empty or not a valid integer
    print("Please enter a valid integer for bpm")
© www.soinside.com 2019 - 2024. All rights reserved.