没有用户输入显示

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

我正在努力使用简单的Python / Kivy程序。与我一直尝试遵循的所有示例不同,我的程序使用绝对零用户输入。它只是从第三方网站获取数据,运行一些计算,然后显示结果。结果每30分钟更新一次。

我还没有弄清楚如何正确地将数据从Python传递到Kivy。我无法掌握什么?

我将程序简化为以下内容:

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty
from kivy.clock import Clock

class GetData():
    red_flag = [1, 0, 0, 1]
    green_flag = [0, 1, 0, 1]

    def data_grab(self):
        '''
        In the real program, the raw data is pulled from 3rd party website
        Then calculations are performed on the raw data and then formatted
        I realize in this sample program that the data never changes
        '''
        se_days = ['4/3', '4/4']
        so_last_update = '4/5 1:31 PM'
        itd_status = [self.green_flag, self.red_flag]
        return se_days, so_last_update, itd_status

class TheBox(FloatLayout):
    day3 = ObjectProperty(None)
    day2 = ObjectProperty(None)
    lastupdate = ObjectProperty(None)
    inv1 = ObjectProperty(None)
    inv2 = ObjectProperty(None)

    def update_data(self, *args):
        d = GetData()
        data_list = d.data_grab()
        se_days = data_list[0]
        so_last_update = data_list[1]
        itd_status = data_list[2]

        self.day3.text = se_days[0]
        self.day2.text = se_days[1]
        self.lastupdate.text = 'Last Updated at: ' + so_last_update
        self.inv1.background_color = itd_status[0]
        self.inv2.background_color = itd_status[1]

class DisplayTestApp(App):

    def build(self):
        time_interval = 30   # minutes
        x = TheBox()
        x.update_data()
        Clock.schedule_interval(x.update_data, time_interval*60)
        return TheBox()

if __name__ == '__main__':
    DisplayTestApp().run()

带有Kivy文件:

#:kivy 1.11.1

<DayLabel@Label>:
    size_hint: .4, .1
    font_size: '24sp'
    color: 1, 1, 1, 1
    halign: 'right'

<InvLabel@Button>:
    size_hint: .2, .15
    font_size: '48sp'
    color: 1, 1, 1, 1
    halign: 'center'

<TheBox>:
    day3: day3
    day2: day2
    lastupdate: lastupdate
    inv1: inv1
    inv2: inv2
    FloatLayout:
        size_hint: 1, 1
        canvas.before:
            Color:
                rgba: 0, 0, 0, 1
            Rectangle:
                pos: self.pos
                size: self.size
        FloatLayout:
            size_hint: .35, .5
            pos_hint: {'center_x': .4, 'center_y': .5}
            DayLabel:
                id: day3
                pos_hint: {'right': .08, 'center_y': .9}
                text: ''
            DayLabel:
                id: day2
                pos_hint: {'right': .08, 'center_y': .7}
                text: ''
        FloatLayout:
            size_hint: .2, .6
            pos_hint: {'center_x': .5, 'center_y': .55}
            InvLabel:
                id: inv2
                pos_hint: {'center_x': .5, 'center_y': .35}
                background_normal: ''
                background_color: .5, .5, .5, 1
                text: '2'
            InvLabel:
                id: inv1
                pos_hint: {'center_x': .5, 'center_y': .2}
                background_normal: ''
                background_color: .5, .5, .5, 1
                text: '1'
        FloatLayout:
            size_hint: .8, .15
            pos_hint: {'center_x': .5, 'center_y': .15}
            Label:
                id: lastupdate
                size_hint: .9, .2
                pos_hint: {'center_x': .5, 'center_y': .3}
                font_size: '18sp'
                color: 1, 1, 1, 1
                halign: 'center'
                text: ''
python kivy kivy-language
1个回答
0
投票

唯一的问题是您正在更新不在GUI中的TheBox实例。

    return TheBox()

在您的build()方法中,正在创建TheBox的新实例,并将其作为GUI的根返回。

行:

    x = TheBox()
    x.update_data()
    Clock.schedule_interval(x.update_data, time_interval*60)

还将创建TheBox的实例,并调用其update_data()方法。但是该实例不会出现在您的GUI中。

简单的解决方法是替换:

return TheBox()

with:

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