Kivy-将KV与python逻辑结合

时间:2018-09-26 15:44:48

标签: python kivy

自昨天以来,我一直在为自己的申请书而苦苦挣扎,但我已经走到了尽头。

我是一名新手程序员,所以我决定要用Kivy创建应用程序,并用Python编写逻辑。应用程序将收集通过REST API连接到网络的设备的状态。

Rest api运作良好,并且我拥有所需的所有信息。此外,我在项目的图形方面也已完成。现在,我想将这两件事合而为一。这是问题所在。

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition
from kivy.config import Config
from os import listdir
from kivy.uix.label import Label
from kivy.properties import ObjectProperty
from rest import sniffer


# Window config

Config.set('graphics', 'width', '800')
Config.set('graphics', 'height', '500')
Config.set('graphics', 'resizable', False)
Config.write()

# Variables

mir_list = []
mir_dict = {'MiR_S161': '10.173.175.117',
        'MiR_S162': '10.173.175.121',
        'MiR_S319': '10.173.175.120',
        'MiR_S325': '10.173.175.119',
        'MiR_S326': '10.173.175.116',
        'MiR_S327': '10.173.175.118'
        }

# Kivy

kv_path = './kv/'
for kv in listdir(kv_path):
    Builder.load_file(kv_path+kv)


# Declare screens

class MainScreen(Screen):
    pass

class MiRScreen161(Screen):
pass

class MiRScreen162(Screen):
    pass

class MiRScreen319(Screen):
    pass

class MiRScreen325(Screen):
    pass

class MiRScreen326(Screen):
    pass

class MiRScreen327(Screen):
    pass

# Create the screen manager

sm = ScreenManager(transition = SlideTransition())
sm.add_widget(MainScreen(name='main'))
sm.add_widget(MiRScreen161(name='MiR_S161'))
sm.add_widget(MiRScreen162(name='MiR_S162'))
sm.add_widget(MiRScreen319(name='MiR_S319'))
sm.add_widget(MiRScreen325(name='MiR_S325'))
sm.add_widget(MiRScreen326(name='MiR_S326'))
sm.add_widget(MiRScreen327(name='MiR_S327'))

class MainApp(App):
    def build(self):
        self.title = 'MiR Sniffer'
        return sm



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

这是我的主要代码-它引用了我之前编写的具有REST处理功能的其他.py脚本。

<MainScreen>:
oft = oftbutton
GridLayout:
    cols: 2
    FloatLayout:
        Image:
            size: (400,200)
            source: 'mir-200.png'
            pos_hint: {'left':1, 'top':0.98}
            size_hint: None, None
            allow_stretch: True
            keep_ratio: False
        Label:
            halign: 'left'
            font_size: 70
            size_hint: (None,None)
            size: (140, 10)
            text: 'MiR Sniffer'
            pos: (105,70)
        Label:
            halign: 'left'
            font_size: 15
            size_hint: (None,None)
            size: (140, 10)
            text: 'Igor Czyrski \ngithub.com/czyrski'
            pos: (0,20)

    StackLayout:
        orientation: 'bt-rl'
        Button:
            id: oftbutton
            text: 'MiR_S161'
            size: 200, 75
            size_hint: None, None
            on_release: root.manager.current = 'MiR_S161'
        Button:
            text: 'MiR_S162'
            size: 200, 75
            size_hint: None, None
            on_release: root.manager.current = 'MiR_S162'
        Button:
            text: 'MiR_S319'
            size: 200, 75
            size_hint: None, None
            on_release: root.manager.current = 'MiR_S319'
        Button:
            text: 'MiR_S325'
            size: 200, 75
            size_hint: None, None
            on_release: root.manager.current = 'MiR_S325'
        Button:
            text: 'MiR_S326'
            size: 200, 75
            size_hint: None, None
            on_release: root.manager.current = 'MiR_S326'
        Button:
            text: 'MiR_S327'
            size: 200, 75
            size_hint: None, None
            on_release: root.manager.current = 'MiR_S327'

那是kv文件。

现在,我需要每3分钟获取一次REST api函数的回调,并在Buttons文本位置显示输出。

我被困住了-正如我所说的,我不是一个熟练的程序员。有人可以看一下并尝试让我走上正确的路吗?

0 个答案:

没有答案