'How to add Screen Manager to my KIVYMD code, but not in kv file only class method
I'm new here, I have a small problem in kivymd. Is there anyone here who would be able to help me?
this is my main.py file
from kivymd.app import MDApp
from kivymd.theming import ThemeManager
from kivymd.uix.slider import Slider
from kivy.uix.boxlayout import BoxLayout
from kivymd.uix.label import MDLabel
from kivymd.uix.toolbar import MDToolbar
from kivymd.uix.textfield import MDTextField
class rendertime(BoxLayout):
def init(self, kwargs):
super().init(kwargs)
self.padding = "30dp"
self.orientation = 'vertical'
self.spacing = "10dp"
self.top_name = MDToolbar(title = "my app")
self.how_many_text = MDLabel(text = "my label")
self.how_many = MDTextField(text = "1")
self.add_widget(self.top_name)
self.add_widget(self.how_many_text)
self.add_widget(self.how_many)
self.hour_value = MDLabel(text = "0")
self.add_widget(self.hour_value)
self.hours_slider = Slider(min = 0, max = 24)
self.add_widget(self.hours_slider)
self.hours_slider.bind(value = self.on_value_hours)
def on_value_hours(self, instance, slider):
self.hour_value.text = "%d"%slider
class myapp(MDApp):
title = "my app v"
def build(self):
self.theme_cls = ThemeManager()
self.theme_cls.primary_palette = 'Orange'
self.theme_cls.theme_style = 'Dark'
return rendertime()
if name== "main":
myapp().run()
Now I would like to switch screens for this. Add 2 buttons at the very bottom and they are to switch to the next screen. I don't want to use the .kv file I wanted to do everything in class.
I don't want to use
kv = """
MDTextField:
id: frame
hint_text: "text"
"""
Please, if anyone can explain it to me, I would be very grateful. Thank you and have a nice day. Good luck everyone
this is a screenmanager file transition.kv
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.lang import Builder
Builder.load_file("transition.kv")
class Screen1(Screen):
pass
class Screen2(Screen):
pass
class Screen3(Screen):
pass
class Screen4(Screen):
pass
class MainApp(MDApp):
def build(self):
self.sm = ScreenManager(transition = NoTransition())
self.sm.add_widget(Screen1(name = "Screen1"))
self.sm.add_widget(Screen2(name = "Screen2"))
self.sm.add_widget(Screen3(name = "Screen3"))
return self.sm
def change_screen(self, screen):
self.sm.current = screen
if __name__ == "__main__":
MainApp().run()
and kv file
<Screen1>:
BoxLayout:
orientation: "vertical"
MDToolbar:
title: "My App"
MDRoundFlatButton:
text: "Go to Screen 2"
on_release: app.root.current = "Screen2"
MDRoundFlatButton:
text: "Go to Screen 3"
on_release: app.root.current = "Screen3"
MDLabel:
text: "Screen 1"
<Screen2>:
BoxLayout:
orientation: "vertical"
MDToolbar:
title: "Screen 2"
left_action_items: [["arrow-left", lambda x: app.change_screen("Screen1")]]
MDLabel:
text: "Screen 2"
<Screen3>:
BoxLayout:
orientation: "vertical"
MDToolbar:
title: "Screen 3"
left_action_items: [["arrow-left", lambda x: app.change_screen("Screen1")]]
MDLabel:
text: "Screen 3
Just don't know how to connect these code
This is a solution only one python file
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from kivy.uix.image import Image
from kivy.core.window import Window
from kivymd.uix.boxlayout import MDBoxLayout
from kivy.uix.scrollview import ScrollView
from kivymd.uix.list import MDList, OneLineListItem
class Screen1(Screen):
def __init__(self, **kwargs):
super(Screen1, self).__init__(**kwargs)
layout = FloatLayout()
btn = Button(text = "screen 2", size_hint = (None,None), size = (100,100), pos_hint = {'x':0,'y':0}, on_release = self.screen_switch)
layout.add_widget(btn)
self.add_widget(layout)
def screen_switch(self, instance):
self.manager.current = 's2'
self.manager.transition.direction = 'left'
class Screen2(Screen):
def __init__(self, **kwargs):
super(Screen2, self).__init__(**kwargs)
layout = FloatLayout()
btn = Button(text = "screen 1", size_hint = (None,None), size = (100,100), pos_hint = {'x':0,'y':0}, on_release = self.screen_switch)
layout.add_widget(btn)
self.add_widget(layout)
myb = MDBoxLayout(padding = 50, orientation = 'vertical')
mys = ScrollView()
myl = MDList()
myb.add_widget(mys)
mys.add_widget(myl)
contacts = ["c1", "c2", "c3"]
for c in contacts:
myl.add_widget(
OneLineListItem(
text = c
)
)
self.add_widget(myb)
def screen_switch(self, instance):
self.manager.current = 's1'
self.manager.transition.direction = 'right'
class myapp(MDApp):
def build(self):
self.theme_cls.theme_style = 'Dark'
sm = ScreenManager()
sm.add_widget(Screen1(name = 's1'))
sm.add_widget(Screen2(name = 's2'))
return sm
if __name__ == "__main__":
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|