Как в Kivy делать много оконные приложения без Builder'а
Хочу сделать программу на Kivy, с использованием нескольких окон, но мне бы желательно это сделать без Builder'а, но я никак не могу понять что нужно вписать в on_press. Я пробовал так:
1. on_press = root.manage.current = "name" 2. on_press = root.manager.current == "name"
Но в первом варианте выбывает SyntaxError: invalid syntax, а во втором NameError: name "root" is not defined.
Вот часть кода:
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
class LenPasword(Screen):
pass
program = ScreenManager()
program.add_widget(LenPasword(name = "lenpasword"))
class PaswordingApp(App):
def build(self):
boxlayout = BoxLayout(orientation = "vertical", spacing = 5, padding = [10])
button_new_pasword = Button(text = "New Pasword",
background_color = [0, 1.5, 3, 1],
size_hint = [1, 0.1])
boxlayout.add_widget(button_new_pasword)
return boxlayout
return program
if __name__ == "__main__":
PaswordingApp().run()
Ответы (1 шт):
Автор решения: gil9red
→ Ссылка
Шаги:
- Создаете окна от Screen
- В конструкторе заполняете их виджетами
- Добавляете у каждого код смены текущего экрана через
self.manager.current = - В классе-приложении создаете
ScreenManager, добавляете в него ваши окна и возвращаетеScreenManager
Попробуйте так:
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
class ScreenMain(Screen):
def __init__ (self, **kwargs):
super().__init__(**kwargs)
boxlayout = BoxLayout(orientation="vertical", spacing=5, padding=[10])
button_new_pasword = Button(
text="New Pasword",
background_color=[0, 1.5, 3, 1],
size_hint=[1, 0.1],
on_press=self._on_press_button_new_pasword,
)
boxlayout.add_widget(button_new_pasword)
self.add_widget(boxlayout)
def _on_press_button_new_pasword(self, *args):
self.manager.transition.direction = 'left'
self.manager.current = 'lenpasword'
class LenPasword(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
boxlayout = BoxLayout(orientation="vertical", spacing=5, padding=[10])
button_new_pasword = Button(
text="Return",
background_color=[2, 1.5, 3, 1],
size_hint=[1, 0.1],
on_press=self._on_press_button_new_pasword,
)
boxlayout.add_widget(button_new_pasword)
self.add_widget(boxlayout)
def _on_press_button_new_pasword(self, *args):
self.manager.transition.direction = 'right'
self.manager.current = 'main_screen'
class PaswordingApp(App):
def build(self):
sm = ScreenManager()
sm.add_widget(ScreenMain(name='main_screen'))
sm.add_widget(LenPasword(name='lenpasword'))
return sm
if __name__ == "__main__":
PaswordingApp().run()