How to make multi-window applications without Builder in Kivy
I want to make a program on Kivy, using several windows, but I would like to do it without the Builder, but I can not understand what needs to be entered in on_press. I tried this way:
1. on_press = root.manage.current = "name" 2. on_press = root.manager.current == "name"
But in the first option, SyntaxError: invalid syntax
is eliminated, and in the second NameError: name "root" is not defined
.
Here is part of the code:
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()
3
Author: Victor VosMottor, 2020-07-28
1 answers
Steps:
- Creating windows from Screen
- In the constructor, fill them with widgets
- Add a code for changing the current screen for each user via
self.manager.current =
- In the application class, create
ScreenManager
, add your windows to it, and returnScreenManager
Try this way:
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()
3
Author: gil9red, 2020-07-28 12:58:20