How to reference objects in other screens using kivy screenmanager - python

I am trying to update a field that exists in another screen but am not succeeding.
I would be very very pleased when someone could tell me what I am doing wrong here.
myscreenskv.py:
style = r'''
# File: myscreenskv.py
#: import myscreens myscreens
<ScreenManagement>:
MainScreen:
Screen1:
Screen2:
<MainScreen>:
name: 'main'
mainlog:mainlog
id: scrmain
BoxLayout:
orientation: "vertical"
Label:
text: "Main"
Label:
id: mainlog
Button:
text: "go to screen 1"
on_press:
app.root.current = "screen1"
root.action1()
Button:
text: "go to screen 2"
on_press:
app.root.current = "screen2"
root.action2()
<Screen1>:
name: 'screen1'
sc1log:sc1log
id: scr1
BoxLayout:
orientation: "vertical"
Label:
text: "Screen1"
Label:
id: sc1log
Button:
text: "go to main screen"
on_press: app.root.current = "main"
Button:
text: "go to screen 2"
on_press: app.root.current = "screen2"
<Screen2>:
name: 'screen2'
id: scr2
sc2log:sc2log
BoxLayout:
orientation: "vertical"
Label:
text: "Screen2"
Label:
id: sc2log
Button:
text: "go to main screen"
on_press: app.root.current = "main"
Button:
text: "go to screen 1"
on_press: app.root.current = "screen1"
'''
.py:
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from myscreenskv import style
class MainScreen(Screen):
def __init__(self, **kwargs):
super(MainScreen, self).__init__(**kwargs)
def action1(self):
self.ids.scr1.sc1log.text = 'Coming from main'
def action2(self):
self.ids.scr2.sc2log.text = 'Coming from main'
class Screen1(Screen):
def __init__(self, **kwargs):
super(Screen1, self).__init__(**kwargs)
def action1(self):
self.ids.main.mainlog.text = 'Coming from screen1'
def action2(self):
self.ids.scr2.sc2log.text = 'Coming from screen1'
class Screen2(Screen):
def __init__(self, **kwargs):
super(Screen2, self).__init__(**kwargs)
def action1(self):
self.ids.main.mainlog.text = 'Coming from screen2'
def action2(self):
self.ids.scr1.sc1log.text = 'Coming from screen2'
class MyscreensApp(App):
def build(self):
Builder.load_string(style)
sm = ScreenManager()
sm.add_widget(MainScreen())
sm.add_widget(Screen1())
sm.add_widget(Screen2())
sm.current = 'main'
return sm
if __name__ == '__main__':
MyscreensApp().run()

You are trying to access ids dictionary, that's nice, yet in a completely different instance, that's why this error:
AttributeError: 'super' object has no attribute '__getattr__'
You need to access the right instance, to be able to access its properties, which in your case you need to access the ScreenManager, to access its screens property (a list of instances), from which you can do the desired edits of for example text:
MainScreen.action1():
self.manager.screens[1].sc1log.text = 'Coming from main'
# no ids, because you put it into a variable before
To understand why it works let's look at the widget tree:
<MainScreen>:
id: scrmain
BoxLayout:
Label:
Label:
id: mainlog
Button:
Button:
here the ids are a dictionary in MainScreen accessible from MainScreen().ids(an instance) and this is the output:
{'mainlog': <WeakProxy to <kivy.uix.label.Label object at 0x12345678>>}
which means you can't really assign the root widget to its own dictionary - at least not this way + makes no sense anyway, because you can just call root, which gives you the instance of the root widget.

Related

Kivy - CustomDropdown - ScreenManager - failure

I´m trying to add a CustomDropDown to a button, but somehow it seems like the CustomDropDown-widget is initiated twice (dubble list) and when I click on a name I get the following answer:
Error:
File "C:\Continuum\anaconda3\lib\site-packages\kivy\lang\builder.py", line 64, in custom_callback
exec(__kvlang__.co_value, idmap)
File "C:\Temp\privat\zucchini\my.kv", line 6, in <module>
app.root.ids.btn.text = '{}'.format(args[1])
File "kivy\properties.pyx", line 863, in kivy.properties.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__'
So yes, I fail to see what I´m doing wrong.. And thoughts are welcome! :)
CODE:
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.properties import ObjectProperty
class WindowManager(ScreenManager):
pass
class MainWindow(Screen):
dropdown = ObjectProperty(None)
def __init__(self, **kw):
super().__init__(**kw)
self.cols = 1
class SecondWindow(Screen):
pass
class MyApp(App):
"""
"""
def build(self):
return Builder.load_file("my.kv")
if __name__ == "__main__":
MyApp().run()
With the kv-file looking like:
#:import Factory kivy.factory.Factory
<CustomDropDown#DropDown>:
id: dropdown
on_select:
app.root.ids.btn.text = '{}'.format(args[1])
self.dismiss()
Button:
id: btn1
text: 'name1'
size_hint_y: None
height: '32dp'
on_release:
dropdown.select(btn1.text)
Button:
id: btn2
text: 'name2'
size_hint_y: None
height: '32dp'
on_release:
dropdown.select(btn2.text)
Button:
id: btn3
text: 'name3'
size_hint_y: None
height: '32dp'
on_release:
dropdown.select(btn3.text)
WindowManager:
MainWindow:
SecondWindow:
<MainWindow>:
name: "main"
GridLayout:
cols: 1
GridLayout:
cols: 1
GridLayout:
cols: 1
GridLayout:
cols: 2
Button:
id: btn
text: 'Set name'
on_release: Factory.CustomDropDown().open(self)
Button:
text: "Submit"
on_release:
app.root.current = "second"
root.manager.transition.direction = "right"
<SecondWindow>:
name: "second"
Button:
text: "Go Back"
on_release:
app.root.current = "main"
root.manager.transition.direction = "left"
Firstly mate, you forgot to add to from kivy.app import App on your .py file.This code not giving me twice your button.It looks correct and gave me only 3 button as you wish.I showed you below how you can call which button clicked.And i suggest you that you can create these 3 button in .py file with a loop easily.Because these are looks like same.I hope this codes help you.
Its my code:
My .py file:
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.uix.dropdown import DropDown
class CustomDropDown(DropDown):
def give_my_value(self,value):
print('Selected value is :',value)
class WindowManager(ScreenManager):
pass
class MainWindow(Screen):
dropdown = ObjectProperty(None)
def __init__(self, **kw):
super().__init__(**kw)
self.cols = 1
class SecondWindow(Screen):
pass
class MyApp(App):
"""
"""
def build(self):
return Builder.load_file("my.kv")
if __name__ == "__main__":
MyApp().run()
my.kv file:
#:import Factory kivy.factory.Factory
<CustomDropDown#DropDown>:
id: dropdown
on_select:
self.dismiss()
Button:
id: btn1
text: 'name1'
size_hint_y: None
height: '32dp'
on_release:
dropdown.give_my_value(self.text)
dropdown.dismiss()
Button:
id: btn2
text: 'name2'
size_hint_y: None
height: '32dp'
on_release:
dropdown.give_my_value(self.text)
dropdown.dismiss()
Button:
id: btn3
text: 'name3'
size_hint_y: None
height: '32dp'
on_release:
dropdown.give_my_value(self.text)
dropdown.dismiss()
WindowManager:
MainWindow:
SecondWindow:
<MainWindow>:
name: "main"
GridLayout:
cols: 1
GridLayout:
cols: 1
GridLayout:
cols: 1
GridLayout:
cols: 2
Button:
id: btn
text: 'Set name'
on_release: Factory.CustomDropDown().open(self)
Button:
text: "Submit"
on_release:
app.root.current = "second"
root.manager.transition.direction = "right"
<SecondWindow>:
name: "second"
Button:
text: "Go Back"
on_release:
app.root.current = "main"
root.manager.transition.direction = "left"

KivyMD: Accessing Label ID From Screen Class

I am using KivyMD and would like to refrash data in the the secound screen using the "on_pre_enter" method.
I am trying to access a label id from screen class "on_pre_enter" method with out success.
I am able to access the label id from the "MainApp" class but unable to access it from the "Second_Screen" class.
MainApp.py
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen
class MainApp(MDApp):
def __init__(self, **kwargs):
self.title = "My Material Application"
self.theme_cls.primary_palette = "Blue"
super().__init__(**kwargs)
def on_start(self):
self.root.ids.main_screen_label.text = "Main Screen Updated"
def build(self):
self.root = Builder.load_file("app.kv")
class Second_Screen(Screen):
def on_pre_enter(self):
MainApp.ids.second_screen_label = "Second Screen Updated"
pass
if __name__ == "__main__":
MainApp().run()
App.kv
NavigationLayout:
MDNavigationDrawer:
NavigationDrawerSubheader:
text: "Menu:"
NavigationDrawerIconButton:
icon:'battery'
text: "Main Screen"
on_release:
screen_manager.current = "main_screen"
NavigationDrawerIconButton:
icon:'battery'
text: "Second Screen"
on_release:
screen_manager.current = "second_screen"
BoxLayout:
id: box1
orientation: 'vertical'
MDToolbar:
title: "My App"
md_bg_color: app.theme_cls.primary_color
left_action_items:
[['menu', lambda x: app.root.toggle_nav_drawer()]]
ScreenManager:
id: screen_manager
Screen:
name: "main_screen"
BoxLayout:
size_hint: .8, .8
pos_hint: {"center_x": .5, "center_y": .5}
spacing: dp(100)
orientation: 'vertical'
MDLabel:
id: main_screen_label
text: "Main Screen Default"
Second_Screen:
name: "second_screen"
FloatLayout:
id: float
size_hint: .8, .8
pos_hint: {"center_x": .5, "center_y": .5}
spacing: dp(100)
orientation: 'vertical'
MDLabel:
id: second_screen_label
text: "Second Screen Default"
I also tried using:
class Second_Screen(Screen):
def on_pre_enter(self):
self.ids.second_screen_label.text = "Second Screen Updated"
pass
After starting the i get the following error:
self.ids.second_screen_label.text = "Second Screen Updated"
File "kivy\properties.pyx", line 863, in
kivy.properties.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__'
What is the correct way to access the id's defined in the KV file from the screen class?
Define screen layout in separate rule <MyScreen>:
<MyScreen>:
name: "my_screen"
FloatLayout:
# ...
To add this screen to ScreenManager, just write:
ScreenManager:
id: screen_manager
MyScreen:
To get access to screen widgets use
from screen class: self.ids.my_id
from app class: self.root.ids.screen_manager.get_screen("screen_name").ids.my_id
from other widgets: App.get_running_app()..root.ids.screen_manager.get_screen("screen_name").ids.my_id
So your full code will be:
main.py
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen
class MainApp(MDApp):
def __init__(self, **kwargs):
self.title = "My Material Application"
self.theme_cls.primary_palette = "Blue"
super().__init__(**kwargs)
def build(self):
self.root = Builder.load_file("app.kv")
def on_start(self):
self.root.ids.screen_manager.get_screen(
"main_screen"
).ids.main_screen_label.text = "Main Screen Updated"
class MainScreen(Screen):
pass
class SecondScreen(Screen):
def on_pre_enter(self):
self.ids.second_screen_label.text = "Second Screen Updated"
if __name__ == "__main__":
MainApp().run()
app.kv
NavigationLayout:
MDNavigationDrawer:
NavigationDrawerSubheader:
text: "Menu:"
NavigationDrawerIconButton:
icon: "battery"
text: "Main Screen"
on_release:
screen_manager.current = "main_screen"
NavigationDrawerIconButton:
icon: "battery"
text: "Second Screen"
on_release:
screen_manager.current = "second_screen"
BoxLayout:
id: box1
orientation: "vertical"
MDToolbar:
title: "My App"
md_bg_color: app.theme_cls.primary_color
left_action_items:
[["menu", lambda x: app.root.toggle_nav_drawer()]]
ScreenManager:
id: screen_manager
MainScreen:
SecondScreen:
<MainScreen>:
name: "main_screen"
BoxLayout:
size_hint: .8, .8
pos_hint: {"center_x": .5, "center_y": .5}
spacing: dp(100)
orientation: "vertical"
MDLabel:
id: main_screen_label
text: "Main Screen Default"
<SecondScreen>:
name: "second_screen"
FloatLayout:
id: float
size_hint: .8, .8
pos_hint: {"center_x": .5, "center_y": .5}
spacing: dp(100)
orientation: "vertical"
MDLabel:
id: second_screen_label
text: "Second Screen Default"

Kivy select screen in .py-file and use in .kv-file

I'm trying to make a list of screens and randomly select one of them in this python file (other code is not included here, like builder etc):
class selectionScreen
testScreen = StringProperty('Screen1')
def screenSelector(self)
screenList = ['Screen1', 'Screen2']
testNumber = random.randint(0, 1)
testScreen = screenList[testNumber]
return testscreen
I then want to choose this screen in my .kv-file, which looks somewhat like this, but also includes screen 1 and 2
ScreenManager:
SelectionScreen:
name: 'SelectionScreen'
Screen0:
name: 'Screen0'
Screen1:
name: 'Screen1'
<SelectionScreen>:
BoxLayout:
Button:
text: "Click when ready"
size_hint: .6, .5
font_size: 10
on_press: root.screenSelector()
on_release: root.manager.current = 'Screen0'
<Screen0>:
BoxLayout:
orientation: 'vertical'
Button:
text: "Next"
on_release: root.manager.current = str(root.testScreen)
The SelectionScreen is working fine, but now I am not able to reach screen0. When removing the line
on_press: root.screenSelector()
I can, however, do this.
Thank you in advance for any help!
Problem
You are not able to reach screen0 because it is not in your screenList.
The variable, testScreen defined in method screenSelector is a local variable, which is not the same class variable, testScreen defined in class selectionScreen.
Undefined variable, return testscreen due to typo error.
At Screen0, the code, on_release: root.manager.current = str(root.testScreen) will cause an AttributeError: 'Screen0' object has no attribute 'testScreen'
Solution
In order to reach Screen0, you have to do the following:
Python Script
Add Screen0 into the variable, screenList
Replace random.randint(0, 1) with random.randint(0, 2)
Replace testScreen with self.testScreen in method screenSelector
kv file
Define an id, e.g. id: selection_screen
Reference it, e.g. root.manager.ids.selection_screen.testScreen
Add on_press call to screenSelector in the other screens, e.g. on_press: root.manager.ids.selection_screen.screenSelector()
Example
main.py
from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.properties import StringProperty
import random
class ScreenManager(ScreenManager):
pass
class SelectionScreen(Screen):
testScreen = StringProperty('Screen1')
def screenSelector(self):
screenList = ['Screen0', 'Screen1', 'Screen2']
testNumber = random.randint(0, 2)
self.testScreen = screenList[testNumber]
return self.testScreen
class Screen0(Screen):
pass
class Screen1(Screen):
pass
class Screen2(Screen):
pass
class Test(App):
def build(self):
return ScreenManager()
if __name__ == "__main__":
Test().run()
test.kv
#:kivy 1.10.0
<ScreenManager>:
SelectionScreen:
id: selection_screen
name: 'SelectionScreen'
Screen0:
name: 'Screen0'
Screen1:
name: 'Screen1'
Screen2:
name: 'Screen2'
<SelectionScreen>:
BoxLayout:
Button:
text: "Click when ready"
size_hint: .6, .5
font_size: 10
on_press: root.screenSelector()
on_release:
root.manager.current = str(root.manager.ids.selection_screen.testScreen)
<Screen0>:
BoxLayout:
orientation: 'vertical'
Button:
text: "Screen 0"
on_press: root.manager.ids.selection_screen.screenSelector()
on_release:
root.manager.current = str(root.manager.ids.selection_screen.testScreen)
<Screen1>:
BoxLayout:
orientation: 'vertical'
Button:
text: "Screen 1"
on_press: root.manager.ids.selection_screen.screenSelector()
on_release:
root.manager.current = str(root.manager.ids.selection_screen.testScreen)
<Screen2>:
BoxLayout:
orientation: 'vertical'
Button:
text: "Screen 2"
on_press: root.manager.ids.selection_screen.screenSelector()
on_release:
root.manager.current = str(root.manager.ids.selection_screen.testScreen)
Output

Why is my Kivy Actionbar gone?

Good evening,
I'm trying to combine kivy's actionbar with a screenmanager. I've gotten to the point where I can switch through screens, but am not able to get my actionbar to show. I've followed alot of examples, but none that can help me with my problem. I'm fairly new to kivy so I haven't been working with it for very long.
I was kind of wondering if someone would be able to point out where my problem lies, because I'm trying to build my very own GUI with an action bar that lets me switch through screens.
here is my main.py:
#!/usr/bin/env python3
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
class Menu(BoxLayout):
pass
class ScreenThermo(Screen):
pass
class ScreenLight(Screen):
pass
class ScreenEnergy(Screen):
pass
class ScreenWeather(Screen):
pass
class Manager(ScreenManager):
screen_thermo = ObjectProperty(None)
screen_light = ObjectProperty(None)
screen_energy = ObjectProperty(None)
screen_weather = ObjectProperty(None)
class MenuApp(App):
def thermostaat(self):
print("Thermostaat")
def verlichting(self):
print("Verlichting")
def energie(self):
print("Energie")
def weer(self):
print("Het Weer")
def build(self):
return Manager()
if __name__ == '__main__':
MenuApp().run()
And here's my menu.kv file:
#:kivy 1.0.9
<Menu>:
orientation: "vertical"
ActionBar:
ActionView:
ActionPrevious:
ActionButton:
text: "Thermostaat"
on_release: app.thermostaat()
ActionButton:
text: "Verlichting"
#I want my screens to switch when clicking on this actionbar button
on_press: root.manager.current= 'light'
on_release: app.verlichting()
ActionButton:
text: "Energieverbruik"
on_release: app.energie()
ActionButton:
text: "Het Weer"
on_release: app.weer()
Button:
text: "Nothing"
background_color: 1, 1, 1, 0.6
background_normal: ""
<ScreenThermo>:
Button:
text: "stuff1"
#this is a test to see if i can switch through screens
on_press: root.manager.current= 'light'
<ScreenLight>:
Button:
text: "stuff2"
<ScreenEnergy>:
Button:
text: "stuff3"
<ScreenWeather>:
Button:
text: "stuff4"
<Manager>:
id: screen_manager
screen_thermo: screen_thermo
screen_light: screen_light
screen_energy: screen_energy
screen_weather: screen_weather
ScreenThermo:
id: screen_thermo
name: 'thermo'
manager: screen_manager
ScreenLight:
id: screen_light
name: 'light'
manager: screen_manager
ScreenEnergy:
id: screen_energy
name: 'energy'
manager: screen_manager
ScreenWeather:
id: screen_weather
name: 'weather'
manager: screen_manager
As you can see, I'm trying to have my screens switch on a actionbar button click, but somehow when i launch it, my actionbar is gone.
If anyone could help me with this issue, that would be amazing.
In the following example we will replace the root widget, Manager with Menu, the detail solution is as follow:
main.py - Python Script
1. build method
Make the class Menu the root widget by replacing:
def build(self):
return Manager()
with:
def build(self):
return Menu()
2. Menu classs
Declare a variable, manager of type ObjectProperty which we will hook it up to the ScreenManager.
class Menu(BoxLayout):
manager = ObjectProperty(None)
menu.kv - kv File
3. Hook up the ObjectProperty to id
Here we hook up the ObjectProperty, manager to the ScreenManager's id, screen_manager so that we can reference it e.g. root.manager.current.
<Menu>:
manager: screen_manager
4. Define the height of ActionBar
We set the height of the ActionBar to 10% (0.1) of the parent's height using size_hint_y = 0.1
ActionBar:
size_hint_y: 0.1
ActionView:
5. Define the height of the Button
We set the height of the Button to 10% (0.1) of the parent's height using
size_hint_y = 0.1
Button:
size_hint_y: 0.1
text: "Nothing"
6. Add ScreenManager as children of Menu
We add Manager as children of Menu after Button. Since we did not specify the height, it defaults to the remaining available height i.e. 0.8 (80% of parent's height).
Manager:
id: screen_manager
Example
main.py
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
class Menu(BoxLayout):
manager = ObjectProperty(None)
class ScreenThermo(Screen):
pass
class ScreenLight(Screen):
pass
class ScreenEnergy(Screen):
pass
class ScreenWeather(Screen):
pass
class Manager(ScreenManager):
screen_thermo = ObjectProperty(None)
screen_light = ObjectProperty(None)
screen_energy = ObjectProperty(None)
screen_weather = ObjectProperty(None)
class MenuApp(App):
def thermostaat(self):
print("Thermostaat")
def verlichting(self):
print("Verlichting")
def energie(self):
print("Energie")
def weer(self):
print("Het Weer")
def build(self):
return Menu()
if __name__ == '__main__':
MenuApp().run()
menu.kv
#:kivy 1.10.0
<Menu>:
manager: screen_manager
orientation: "vertical"
ActionBar:
size_hint_y: 0.1
ActionView:
ActionPrevious:
ActionButton:
text: "Thermostaat"
on_release: app.thermostaat()
ActionButton:
text: "Verlichting"
#I want my screens to switch when clicking on this actionbar button
on_press: root.manager.current= 'light'
on_release: app.verlichting()
ActionButton:
text: "Energieverbruik"
on_release: app.energie()
ActionButton:
text: "Het Weer"
on_release: app.weer()
Button:
size_hint_y: 0.1
text: "Nothing"
background_color: 1, 1, 1, 0.6
background_normal: ""
Manager:
id: screen_manager
<ScreenThermo>:
Button:
text: "stuff1"
#this is a test to see if i can switch through screens
on_press: root.manager.current= 'light'
<ScreenLight>:
Button:
text: "stuff2"
<ScreenEnergy>:
Button:
text: "stuff3"
<ScreenWeather>:
Button:
text: "stuff4"
<Manager>:
id: screen_manager
screen_thermo: screen_thermo
screen_light: screen_light
screen_energy: screen_energy
screen_weather: screen_weather
ScreenThermo:
id: screen_thermo
name: 'thermo'
manager: screen_manager
ScreenLight:
id: screen_light
name: 'light'
manager: screen_manager
ScreenEnergy:
id: screen_energy
name: 'energy'
manager: screen_manager
ScreenWeather:
id: screen_weather
name: 'weather'
manager: screen_manager
Output
you never add the menu to your app also you need to add a manager property in your menu. Try something like this:
in the .py:
...
class Menu(BoxLayout):
manager = ObjectProperty(None)
...
class MenuApp(App):
def thermostaat(self):
print("Thermostaat")
def verlichting(self):
print("Verlichting")
def energie(self):
print("Energie")
def weer(self):
print("Het Weer")
...
Notice that I have removed the build method
In your kv add this block of code at the end:
...
BoxLayout: #use a box layout or whatever you want
orientation: 'vertical'
Menu:
size_hint_y: .1
manager: manager
Manager:
size_hint_y: .9
id: manager

Kivy with Menubar

I want to create a Kivy-App with a Menubar which is always the same.
My Code:
demo.py:
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout
class Display(BoxLayout):
pass
class Screen_One(Screen):
pass
class Screen_Two(Screen):
pass
class DemoApp(App):
def build(self):
return Display()
if __name__ == '__main__':
DemoApp().run()
demo.kv:
<Display>:
BoxLayout:
orientation: "vertical"
BoxLayout:
size_hint: 1, None
height: '48dp'
Button:
text: 'One'
on_release: sm.current = 'Screen_One'
Button:
text: 'Two'
on_release: sm.current = 'Screen_Two'
ScreenManager:
id: sm
Screen_One:
Screen_Two:
<Screen_One>:
Button:
text: 'One'
<Screen_Two>:
Button:
text: 'Two'
But it doesn't work, if i click on "One" i received:
kivy.uix.screenmanager.ScreenManagerException: No Screen with name "Screen_One".
I reduced my code to the leading part. I also tried to put the menu-kivy-code in a seperate file and import but then i also can't access the ScreenManager and can't switch the Screens.
ScreenManager.current atribute is the name of the screen currently shown. You have not set the name attribute in your classes (screens). Your demo.kv file should be:
<Display>:
BoxLayout:
orientation: "vertical"
BoxLayout:
size_hint: 1, None
height: '48dp'
Button:
text: 'One'
on_release: sm.current = 'screen_one' #<<<<<<<<<<<<<<<<
Button:
text: 'Two'
on_release: sm.current = 'screen_two' #<<<<<<<<<<<<<<<<
ScreenManager:
id: sm
Screen_One:
Screen_Two:
<Screen_One>:
name: 'screen_one' #<<<<<<<<<<<<<<<<
Button:
text: 'One'
<Screen_Two>:
name: 'screen_two' #<<<<<<<<<<<<<<<<
Button:
text: 'Two'

Categories