I'm playing around with the ScreenManager and Transitions. In my code i also have integrated an ActionBar. I tried this:
ActionButton:
text: 'Back to Main Menu'
on_release:
app.root.ids.sm.current = 'first'
root.manager.transition.direction = "right"
But i get this Error
AttributeError: 'SomeMenu_ActionBar' object has no attribute 'manager'
I also tried to put the SomeMenu_ActionBar with the other Screens under the ScreenManager. Doesnt work either, since the ScreenManager only accepts Screen Widgets.
Is it even possible, to change the transition of the ActionBar? Preferably i just dont want any transition at all, for all Buttons. Do i maybe have to add a BoxLayout in every Screen which looks like a ActionBar and that could maybe work?
Here is my pythoncode and kv:
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.metrics import dp, sp
from kivy.lang import Builder
class WelcomeScreen(Screen):
pass
class FirstScreen(Screen):
pass
class SecondScreen(Screen):
pass
class ScreenManager(ScreenManager):
pass
class CrimePrevention(BoxLayout):
pass
Builder.load_file("screenmanager.kv")
class TestApp(App):
title = "Kivy ScreenManager & ActionBar Demo"
def build(self):
return CrimePrevention()
if __name__ == '__main__':
TestApp().run()
kv-File:
#:kivy 2.1.0
<CrimePrevention>:
orientation: "vertical"
canvas.before:
Color:
rgb: .6, .6, .6
Rectangle:
pos: self.pos
size: self.size
SomeMenu_ActionBar:
id: ActionBar
ScreenManager:
id: sm
WelcomeScreen:
FirstScreen:
SecondScreen:
<SomeMenu_ActionBar@ActionBar>:
ActionView:
id: ActionView
HiddenIcon_ActionPrevious:
ActionGroup:
id: App_ActionGroup
mode: "spinner"
text: "Jump to Screen"
ActionButton:
text: "Crime Prediction"
on_release: app.root.ids.sm.current = 'second'
ActionButton:
text: "Forum"
on_release: app.root.ids.sm.current = 'second'
ActionButton:
text: "Probable Suspect"
on_release: app.root.ids.sm.current = 'second'
ActionGroup:
id: App_ActionGroup
mode: 'spinner'
text: 'App'
ActionButton:
text: 'Settings'
on_press: app.open_settings()
ActionButton:
text: 'Quit'
on_press: app.get_running_app().stop()
ActionGroup:
id: File_ActionGroup
mode: 'spinner'
text: 'File'
ActionButton:
text: 'Open'
ActionButton:
text: 'Save'
ActionButton:
text: 'Back to Main Menu'
on_release:
app.root.ids.sm.current = 'first'
#root.manager.transition.direction = "right"
<HiddenIcon_ActionPrevious@ActionPrevious>:
title: ''
with_previous: False
app_icon: ''
app_icon_width: 0
app_icon_height: 0
size_hint_x: None
width: len(self.title)*10
<WelcomeScreen>:
name: 'welcome'
Label:
text: 'Welcome Screen'
font_size: sp(50)
<FirstScreen>:
name: 'first'
Label:
text: 'First Screen'
<SecondScreen>:
name: 'second'
BoxLayout:
orientation: 'vertical'
Label:
text: 'Predict Crime'
font_size: 50
BoxLayout:
Button:
text: 'Back to Main Menu'
font_size: 30
on_release:
app.root.ids.sm.current = 'first'
#root.manager.transition.direction = "right"
Button:
text: 'get random colour screen'
font_size: 30
on_release:
app.root.ids.sm.current = 'first'
#root.manager.transition.direction = "right"
CodePudding user response:
The problem is that your code is trying to access the manager
attribute of the SomeMenu_ActionBar
. This is happening because root
(as descrribed in the documentation) is the root widget of the rule in which it appears. So root
in this case is the SomeMenu_ActionBar
. The overuse of the term root
in the kivy documentation can result in some confusion. There is also a root
widget that can de defined in a kv
file, but your kv
file does not define such a root
. And there is also the root
widget of the kivy Application
. So in your code:
ActionButton:
text: 'Back to Main Menu'
on_release:
app.root.ids.sm.current = 'first'
root.manager.transition.direction = "right"
the root
refers to the containing SomeMenu_ActionBar
. You can modify that code (as shown below) to access the root
widget of the App
:
ActionButton:
text: 'Back to Main Menu'
on_release:
app.root.ids.sm.current = 'first'
app.root.ids.sm.transition.direction = "right"