Как вызывать функцию, при переключении экрана менеджера экранов kivy
Задача: вызвать некоторую функцию, на определенном экране скринменеджера(например: меню), не задействуя при этом кнопки других экранов, создающих переход на нужный экран, вызывающих некоторую функцию(например print('hello')) Есть идея, чтобы на тех кнопках вызывать проверку текущего экрана (if screen_manager.current == 'screen'), но вот встал вопрос, чтобы вызвать на нужном экране нужную функцию, при его появлении и не создавать кучу кода на всех кнопках
<MainMenu>:
FloatLayout:
Image:
source: 'menu.png'
Button:
text: " "
background_normal: 'dalee.png'
size_hint: .15, .1
pos_hint:{'right':.5,'bottom':.1}
on_press:
root.manager.transition.direction = 'left'
root.manager.transition.duration = 1
root.manager.current = 'init'
FloatLayout:
halign: 'left'
valign:'middle'
Image:
source: 'ready.png'
pos_hint: {'center_x': 0.13, 'center_y': 0.8}
size_hint: .4, .4
Label:
id: loraMessage
text: '44 знака, после этого слеш эн'
bold: True
font_size:30
pos_hint: {'center_x': 0.58, 'center_y': 0.9}
BoxLayout:
orientation: 'horizontal'
padding: 10, 10
spacing: 10
Button:
id: commands
background_normal: 'menuCom1.png'
background_down: 'menuCom12.png'
size_hint: .2, .19
pos_hint: {'center_x':.1, 'center_y':.5}
on_press:
root.manager.current = 'commands'
BoxLayout:
orientation: 'horizontal'
padding: 10, 10
spacing: 10
Button:
id: settings
background_normal: 'menuSet1.png'
background_down: 'menuSet12.png'
size_hint: .2, .19
pos_hint: {'center_x':.1, 'center_y':.3}
on_press:
root.manager.current = 'settings'
BoxLayout:
orientation: 'horizontal'
padding: 10, 10
spacing: 10
Button:
id: exit
background_normal: 'menuExit1.png'
background_down: 'menuExit12.png'
size_hint: .2, .19
pos_hint: {'center_x':.1, 'center_y':.1}
on_press: self.root_window.close()
<ChooseLang>:
BoxLayout:
padding:160
Button:
text: "English"
font_size: 50
background_normal: 'en.png'
on_press:
root.english()
root.manager.transition.direction = 'left'
root.manager.transition.duration = 1
root.manager.current = 'init'
Button:
text: "Русский"
font_size: 50
background_normal: 'ru.png'
on_press:
root.russian()
root.manager.transition.direction = 'left'
root.manager.transition.duration = 1
root.manager.current = 'init'
<Init>:
username: username
age: age
FloatLayout:
Image:
source: 'init.png'
FloatLayout:
TextInput:
id: username
multiline: False
font_size: 33
hint_text:'Как к тебе обращаться?'
pos_hint: {'center_x': 0.38, 'center_y': 0.510}
size_hint: 0.30, 0.08
Label:
id: nameError
text: ' '
bold: True
font_size:30
pos_hint: {'center_x': 0.72, 'center_y': 0.510}
Label:
id: ageError
text: ' '
bold: True
font_size:30
pos_hint: {'center_x': 0.8, 'center_y': 0.410}
FloatLayout:
TextInput:
id: age
hint_text:'Сколько тебе лет?'
multiline: False
font_size: 33
pos_hint: {'center_x': 0.5, 'center_y': 0.415}
size_hint: 0.30, 0.08
Button:
id: initnextRU
size_hint: .15, .1
pos_hint:{'right':.5,'bottom':.1}
background_normal: 'nextBut.png'
background_down: 'nextBut2.png'
on_press:
root.check()
Button:
id: initcheckname
size_hint: .15, .1
pos_hint:{'right':.34,'bottom':.2}
background_normal: 'playBut.png'
background_down: 'playBut2.png'
on_press:
root.speak()
<Settings>:
FloatLayout:
Image:
source: 'settings.png'
Button:
id: backmenu
size_hint: .15, .1
pos_hint:{'center_x':.5,'bottom':.2}
background_normal : 'backBut.png'
background_down: 'backBut2.png'
on_press:
root.manager.current = 'menu'
''')
#описание классов
class ChooseLang(Screen):
def build(self):
Window.clearcolor = (.17, .19, .24, 1)
Window.size = (setWindowSize())
self.icon = 'icon.png'
self.title = 'LORA'
self.SM = ScreenManager()
def english(self):
global INITLANG
INITLANG = 'ru'
print("You choose english!")
def russian(self):
global INITLANG
INITLANG = 'ru'
print("Ты выбрал русский")
pass
class Init(Screen):
username = ObjectProperty(None)
age = ObjectProperty(None)
screen_manager = ObjectProperty()
initName = ' '
initAge = 0
def build(self):
Window.clearcolor = (.17, .19, .24, 1)
Window.size = (setWindowSize())
self.icon = 'icon.png'
self.title = 'LORA'
self.SM = ScreenManager()
layout = FloatLayout()
self.name = TextInput(multiline = False)
self.add_widget(self.name)
self.age = TextInput(multiline = False)
self.add_widget(self.age)
return layout
def check(self):
check = 0
username_text = self.username.text
# разобраться с переключением сцены: self.screen_manager.current = 'menu'
if len(username_text)<2:
self.ids['nameError'].text = 'Мало символов!'
if len(username_text)>20:
self.ids['nameError'].text = 'Превышение лимита символов!'
if match(username_text) == False:
self.ids['nameError'].text = 'Используйте русские символы!'
if len(username_text)>2 and len(username_text)<20:
self.ids['nameError'].text = ' '
check = 0
check += 1
username_age = self.age.text
if username_age.isdigit():
age = int(username_age)
if age < 4:
self.ids['ageError'].text = 'Маловато лет..'
if age > 120:
self.ids['ageError'].text = 'Многовато лет..'
if age>4 and age <120:
self.ids['ageError'].text = ' '
check += 1
else:
self.ids['ageError'].text = 'Введите число!'
if check == 2:
global INITLANG
print('Проверка пройдена!')
inInitFile(INITLANG)
inInitFile(username_text)
inInitFile(age)
self.manager.current = 'menu'
else:
print('проверка не пройдена')
print('username:' + username_text + ' len: ' + str(len(username_text)))
print('age:' + username_age)
def speak(self):
username_text = self.username.text
checkSpeak = 0
if match(username_text):
checkSpeak += 1
if len(username_text) > 2 and len(username_text) < 20:
checkSpeak += 1
if(checkSpeak == 2):
loraSpeak(username_text)
else:
loraSpeak('Где-то ошибка!')
pass
class MainMenu(Screen):
def build(self):
Window.clearcolor = (.17, .19, .24, 1)
Window.size = (setWindowSize())
self.icon = 'icon.png'
self.title = 'LORA'
self.SM = ScreenManager()
layout = FloatLayout(size=(200, 200))
return layout
pass
class Settings(Screen):
pass
#добавление экранов в скрин менеджер
screen_manager.add_widget(ChooseLang(name="language"))
screen_manager.add_widget(Init(name="init"))
screen_manager.add_widget(MainMenu(name="menu"))
screen_manager.add_widget(Settings(name="settings"))