如何使用python或kivy访问另一个类的标签/ id并避免下面的错误? 我在其他帖子中搜索过,我发现根本不可能引用另一个类...... Hower,我不知道有任何其他方法可以做到这一点。我希望你能帮助我。 提前致谢。
FaceRec.py
库
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.settings import SettingsWithSidebar
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.actionbar import ActionBar
from kivy.logger import Logger
from kivy.config import Config
设置屏幕尺寸
Config.set('graphics', 'width', '850')
Config.set('graphics', 'height', '850')
HomeScreen类
class ScreenManagement(ScreenManager):
pass
class HomeScreen(Screen):
pass
class HomeActionBar(ActionBar):
pass
class TitleLabel(Label):
pass
class StatusBoxLayout(BoxLayout):
pass
class ErrorsBoxLayout(BoxLayout):
pass
主要课程
class FaceRecApp(App):
def build(self):
root = HomeScreen()
Logger.info('FaceRec.py: FaceRec.kv loaded')
self.settings_cls = MySettingsWithSidebar
Logger.info('FaceRec.py: MySettingsWithSidebar loaded')
status = root.ids.StatusBoxLayout.status
status.font_size = float(self.config.get('General', 'font_size'))
label = root.ids.label
label.font_size = float(self.config.get('General', 'font_size'))
return root
def build_config(self, config):
config.setdefaults('General', {'text': 'Default Hello','font_size': 20})
def build_settings(self, settings):
settings.add_json_panel('General', self.config, 'Settings.json')
def on_config_change(self, config, section, key, value):
Logger.info("FaceRec.py: App.on_config_change: {0}, {1}, {2}, {3}".format(config, section, key, value))
if section == "General":
if key == "text":
self.root.ids.label.text = value
elif key == 'font_size':
self.root.ids.label.font_size = float(value)
self.root.ids.StatusBoxLayout.status = float(value)
def close_settings(self, settings):
Logger.info("FaceRec.py: App.close_settings: {0}".format(settings))
super(FaceRecApp, self).close_settings(settings)
补充工具栏设置
class MySettingsWithSidebar(SettingsWithSidebar):
def on_close(self):
Logger.info('FaceRec.py: MySettingsWithSidebar.on_close')
def on_config_change(self, config, section, key, value):
Logger.info(
"FaceRec.py: MySettingsWithSidebar.on_config_change: "
"{0}, {1}, {2}, {3}".format(config, section, key, value))
执行
if __name__ == '__main__':
FaceRecApp().run()
FaceRec.kv
ScreenManagement:
HomeScreen:
<HomeActionBar>:
id: HomeActionBar
pos_hint: {'bottom':0}
ActionView:
use_separator: True
ActionPrevious:
title: 'Home'
with_previous: False
ActionOverflow:
ActionButton:
text: 'Settings'
icon: 'settings.png'
background_down: 'settings.png'
on_release:
app.open_settings()
<TitleLabel>:
id: TitleLabel
text: '[b]FaceRec[/b] - [i]The Face Recognition Project[/i]'
color: 0.0,0.3,1,1
markup: True
font_size: 38
<StatusBoxLayout>:
orientation: 'horizontal'
#padding: 100
Label:
id: status
text: 'Status: '
font_size: 20
Label:
id: status_value
text: 'Error'
font_size: 20
color: 1,0,0,1
<ErrorsBoxLayout>:
id: ErrorsBoxLayout
orientation: 'horizontal'
#padding: 100
Label:
id: errors
text: 'Errors No: '
font_size: 20
Label:
id: errors_value
text: '...'
font_size: 20
<HomeScreen>:
id: HomeScreen
BoxLayout:
orientation: 'vertical'
HomeActionBar:
TitleLabel:
BoxLayout:
cols: 2
orientation: 'vertical'
StatusBoxLayout:
ErrorsBoxLayout:
Label:
id: label
text: 'Does it run?'
Settings.json
[
{
"type": "string",
"title": "Label caption",
"desc": "Choose the text that appears in the label",
"section": "General",
"key": "text"
},
{
"type": "numeric",
"title": "Label font size",
"desc": "Choose the font size the label",
"section": "General",
"key": "font_size"
}
]
我尝试访问 label 变量(FaceRecApp,build和on_config_change函数),程序运行正常。当我在设置中更改标签的字体大小时,已在HomeScreen中应用了更改。然后我添加了以下代码行:
在build()中:
status = root.ids.StatusBoxLayout.status
status.font_size = float(self.config.get('General', 'font_size'))
并在on_config_change()中:
self.root.ids.StatusBoxLayout.status = float(value)
结果出错了:
[INFO ] [Logger ] Record log in C:\***\.kivy\logs\kivy_17-04-17_109.txt
[INFO ] [Kivy ] v1.9.1
[INFO ] [Python ] v2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)]
[INFO ] [Factory ] 179 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_gif, img_sdl2, img_pil (img_ffpyplayer ignored)
[INFO ] [Text ] Provider: sdl2
[INFO ] [OSC ] using <thread> for socket
[INFO ] [Window ] Provider: sdl2
[INFO ] [GL ] GLEW initialization succeeded
[INFO ] [GL ] OpenGL version <4.5.0 NVIDIA 376.53>
[INFO ] [GL ] OpenGL vendor <NVIDIA Corporation>
[INFO ] [GL ] OpenGL renderer <GeForce ***/PCIe/SSE2>
[INFO ] [GL ] OpenGL parsed version: 4, 5
[INFO ] [GL ] Shading version <4.50 NVIDIA>
[INFO ] [GL ] Texture max size <16384>
[INFO ] [GL ] Texture max units <32>
[INFO ] [Window ] auto add sdl2 input provider
[INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
[INFO ] [FaceRec.py ] FaceRec.kv loaded
[INFO ] [FaceRec.py ] MySettingsWithSidebar loaded
Traceback (most recent call last):
File "C:/***/PycharmProjects/face_recognition_2/FaceRec.py", line 114, in <module>
FaceRecApp().run()
File "C:\Python27\lib\site-packages\kivy\app.py", line 802, in run
root = self.build()
File "C:/***/PycharmProjects/face_recognition_2/FaceRec.py", line 65, in build
status = root.ids.StatusBoxLayout.status
File "kivy\properties.pyx", line 757, in kivy.properties.ObservableDict.__getattr__ (kivy\properties.c:11882)
AttributeError: 'super' object has no attribute '__getattr__'
Process finished with exit code 1
我已经阅读过这篇文章(以及其他许多人......): How to access id/widget of different class from a kivy file (.kv)? 但是我的程序尚未运行。
再次感谢您的支持。
答案 0 :(得分:0)
您忘记在根类(HomeScreen)中为StatusBoxLayout指定一个ID。需要在根类中给出id,以便能够将其视为self.root.ids.StatusBoxLayout。
所以在HomeScreen中给它它的id:
<HomeScreen>:
id: HomeScreen
BoxLayout:
orientation: 'vertical'
HomeActionBar:
TitleLabel:
BoxLayout:
cols: 2
orientation: 'vertical'
StatusBoxLayout:
id: StatusBoxLayout
ErrorsBoxLayout:
id: ErrorsBoxLayout
当你在这里时,你也可以给ErrorsBoxLayout
一个id,或者你需要通过id访问的任何其他类。
现在,当您指定status = root.ids.StatusBoxLayout.status
时,请将其分配为:
status = self.root.ids.StatusBoxLayout.ids.status
您需要在self
之前添加App
,因为在self.root
课程的后面,您想获得或设置root
。但是您没有将ids
定义为类属性
第二个StatusBoxLayout
是因为状态是def build(self):
self.root = HomeScreen()
Logger.info('FaceRec.py: FaceRec.kv loaded')
self.settings_cls = MySettingsWithSidebar
Logger.info('FaceRec.py: MySettingsWithSidebar loaded')
status = self.root.ids.StatusBoxLayout.ids.status
status.font_size = float(self.config.get('General', 'font_size'))
label = self.root.ids.label
label.font_size = float(self.config.get('General', 'font_size'))
return self.root
的身份证
所以你的构建方法应该是这样的:
mSocket.emit("send message", "msg data", "abc1");