其实,我有2个类似的问题:有没有办法从另一个二级类访问非根类?
- 可以访问类FaceNameTxtIn,捕捉TextInput的文本,从没有使用全局变量(例如,通过IDS,self.parent类TrainerBtn .. 。我试过没有成功)?
如何从TrainerBtn函数on_release访问TrainerBoxLayout的trained_faces_value.text?从根类的程序是:
self.root.ids.TrainerBoxLayout.ids.trained_faces_value.text
,程序运行正常,所以我已经改变了在self.parent self.root(然后self.parent.parent)但过程失败并返回 '处理已结束,退出代码1'。
(我已经搜查了很多网上没有找到与我的问题匹配的解决方案)
FaceRec.py
Kivy库
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.button import Button
from kivy.uix.textinput import TextInput
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.core.window import Window
主屏幕类
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 TrainerBoxLayout(BoxLayout):
pass
class TrainerBtn(Button):
def on_release(self):
global face_name
face_name = face_name.replace(' ','')
...
class FaceNameTxtIn(TextInput):
def on_text(self,instance,value):
global face_name
face_name = value
class RecognizerBtn(Button):
def on_release(self):
...
点
主要类
class FaceRecApp(App):
...
def build(self):
self.root = HomeScreen()
Logger.info('FaceRec.py: FaceRec.kv loaded')
self.settings_cls = MySettingsWithSidebar
Logger.info('FaceRec.py: MySettingsWithSidebar loaded')
...
return self.root
...
侧边栏的设置
class MySettingsWithSidebar(SettingsWithSidebar):
...
执行
if __name__ == '__main__':
FaceRecApp().run()
FaceRec.kv
#: import WipeTransition kivy.uix.screenmanager.WipeTransition
<ScreenManagement>:
transition: WipeTransition()
HomeScreen:
<HomeActionBar>:
id: HomeActionBar
background_color: 0.9,0.9,0.9,0.5
pos_hint: {'top':1}
size_hint_x: 1
ActionView:
use_separator: True
ActionPrevious:
title: 'Home'
with_previous: False
ActionOverflow:
ActionButton:
text: 'Logs'
on_release:
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: 45
<StatusBoxLayout>:
orientation: 'horizontal'
Label:
id: status
text: 'Status: '
Label:
id: status_value
text: 'Error'
color: 1,0,0,1
<ErrorsBoxLayout>:
id: ErrorsBoxLayout
orientation: 'horizontal'
Label:
id: errors
text: 'Errors No: '
Label:
id: errors_value
text: '...'
<TrainerBoxLayout>:
id: TrainerBoxLayout
orientation: 'horizontal'
Label:
id: trained_faces
text: 'Trained faces: '
Label:
id: trained_faces_value
text: '...'
<TrainerBtn>:
id: TrainerBtn
text: 'Press to run the Face Trainer'
font_size: 25
size_hint: 0.5,1.4
<FaceNameTxtIn>:
id: FaceNameTxtIn
hint_text: 'Insert your name and then press the button below'
font_size: 19
size_hint: 0.5,1.4
multiline: False
<RecognizerBtn>:
id: RecognizerBtn
text: 'Press to run the Face Recognizer'
font_size: 25
size_hint: 0.5,1.4
<HomeScreen>:
id: HomeScreen
name: 'HomeScreen'
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: 'face_pointed.png'
HomeActionBar:
id: HomeActionBar
BoxLayout:
orientation: 'vertical'
spacing: 70
TitleLabel:
id: TitleLabel
BoxLayout:
orientation: 'vertical'
spacing: 20
StatusBoxLayout:
id: StatusBoxLayout
ErrorsBoxLayout:
id: ErrorsBoxLayout
TrainerBoxLayout:
id: TrainerBoxLayout
FaceNameTxtIn:
id: FaceNameTxtIn
BoxLayout:
orientation: 'horizontal'
TrainerBtn:
id: TrainerBtn
RecognizerBtn:
id: RecognizerBtn
Label:
text: 'Press q to quit the video'
font_size: 20
感谢您的宝贵支持。
你可能有更好的运气越来越要是你把你的例子到你的问题快速回答。看看这个https://stackoverflow.com/help/mcve – EL3PHANTEN