为什么kivy TextInput再次打印相同的数字?

时间:2019-04-14 16:23:32

标签: python kivy

我想在按下按钮时获得TextInput的总数。

class Streak():
    def __init__(self, action, action_num, day, hour, minute):
        self.action = action
        self.action_num = action_num
        self.day = day
        self.hour = hour
        self.minute = minute

class MainApp(App):
    def build(self): # build() returns an instance
        return presentation

    def create(self):
        obj = self.root.get_screen('one')
        self.streak = Streak(obj.ids.action_entry.text, obj.ids.streak_entry.text,
                            obj.ids.day_entry.text, obj.ids.hour_entry.text,
                            obj.ids.minute_entry.text)

        total = ((self.streak.day * 86400) + (self.streak.hour * 3600) +
                (self.streak.minute * 60)) # convert into seconds

        print(total)

这是.kv代码:

        GridLayout:
            cols:3
            rows:2
            Label:
                text: "Day(s)"
                size_hint: None, None
                font_size: 18
            Label:
                text: "Hour(s)"
                size_hint: None, None
                font_size: 18
            Label:
                text: "Minute(s)"
                size_hint: None, None
                font_size: 18
            TextInput:
                id: day_entry
                input_filter: "int"
                multiline: False
                size_hint: None, None
                size: 200, 28
            TextInput:
                id: hour_entry
                input_filter: "int"
                multiline: False
                size_hint: None, None
                size: 200, 28
            TextInput:
                id: minute_entry
                input_filter: "int"
                multiline: False
                size_hint: None, None
                size: 200, 28
    AnchorLayout:
        anchor_x: "right"
        anchor_y: "bottom"
        Button:
            text: "Add"
            size: 50, 25
            size_hint: None, None
            font_size: 18
            on_press: app.create()

当我将数字输入到TextInput并按“添加”按钮时,程序冻结了一段时间,然后多次打印“日”输入,例如:

天:1,小时:0,分钟:1

控制台:11111111111111111111111111111111111111111111111111111111111111 ...

我该如何解决?

1 个答案:

答案 0 :(得分:0)

我认为问题在于,您永远不要将textinput内容转换为int-即使只允许输入数字字符,但文本本身仍然是字符串。

这意味着您的输出为"1" * 86400 + ...,即您看到的输出数量更大。

要解决此问题,在每种情况下都可以执行类似self.day = int(day)的操作。