我对猕猴桃继承有一些疑问。因此,我有ScreenManager根小部件,我想在我的屏幕小部件中访问它的值。想法是在我的ScreenManager中创建一个对象列表,并将其值继承给子窗口小部件。
以下代码:
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import StringProperty, ListProperty, DictProperty
from kivy.lang import Builder
from kivy.clock import Clock
from os import listdir
import requests
import json
kv_path = './kv/'
for kv in listdir(kv_path):
Builder.load_file(kv_path + kv)
class MirSniffer(ScreenManager):
mir_list = ListProperty()
mir_dict = DictProperty({'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'
})
class MiR():
"""Defining a robot class"""
error = 'Not accessible!'
mode = 'Not accessible!'
mission = 'Not accessible!'
battery_per = 'Not accessible!'
status = 'Not accessible!'
def __init__(self, name, ip):
self.name = name
self.ip = ip
def status_check(self):
url = f"http://{self.ip}/api/v2.0.0/status"
headers = {
'Content-Type': "application/json",
'authorization': "Basic YWRtaW46OGM2OTc2ZTViNTQxMDQxNWJkZTkwOGJkNGRlZTE1ZGZiMTY3YTljODczZmM0YmI4YTgxZjZmMmFiNDQ4YTkxOA==",
'Cache-Control': "no-cache",
'Postman-Token': "bfcd7159-1d12-5b7f-468f-d5e096709cbd"
}
response = requests.request("GET", url, headers=headers, timeout=2)
text = json.loads(response.text) # str to dict
self.error = text["errors"][0]["description"]
self.battery_per = text.get("battery_percentage")
self.mission = text.get("mission_text")
self.mode = text.get("mode_text")
self.status = text.get("state_text")
def creating_robots_list(mir_list, mir_dict):
"""Creating list of robots. Called once"""
for key in mir_dict.keys():
robot = MiR(key, mir_dict.get(key))
mir_list.append(robot)
def __init__(self, **kwargs):
super(MainScreen, self).__init__(**kwargs)
Clock.schedule_interval(self.creating_robots_list(self.mir_list,
self.mir_dict), 10)
class MainScreen(Screen):
status_mir161 = StringProperty('0')
def __init__(self, **kwargs):
super(MainScreen, self).__init__(**kwargs)
Clock.schedule_interval(self.change_robot_status, 5)
def change_robot_status(self, *args):
test_string = self.status_mir161 + '1'
self.status_mir161 = test_string
class Mir161(Screen):
pass
class MainApp(App):
def build(self):
return MirSniffer()
if __name__ == '__main__':
MainApp().run()
如果我想根据mir_list更改self.status_mir161-它的语法是什么?
即使在父窗口小部件中创建对象列表,我也不确定。
答案 0 :(得分:0)
我没有得到您的KV文件,但是如果您只想访问您定义的ScreenManager
的值,那么首先在MainScreen
上获得孩子的父母:>
parent = self.parent
然后,如果该父母是ScreenManager
,则可以执行以下操作:
target_dict = parent.mir_dict
我没有测试过,但是尝试一下,我们可以从那里拿走