其實,我有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