Reputation: 79
I am trying to rewrite some code. My main problem is that I don't understand the build(self)
function in Test(App)
. Does self.swiper_manager
reference swiper_manager = None
and start_screen.ids.swiper_manager
reference MDSwiperManager: id: swiper_manager
in the .kv file?
Code that I am trying to rewrite.
class MySwiperManager(BoxLayout):
pass
class Test(App):
theme_cls = ThemeManager()
theme_cls.primary_palette = 'Indigo'
swiper_manager = None
def build(self):
start_screen = MySwiperManager()
self.swiper_manager = start_screen.ids.swiper_manager
paginator = MDSwiperPagination()
paginator.screens = self.swiper_manager.screen_names
paginator.manager = self.swiper_manager
self.swiper_manager.paginator = paginator
start_screen.add_widget(paginator)
return start_screen
The code below is my best attempt to rewrite Name of file: Prelogin/prescreenmanager.py
from kivy.app import App
from kivy.core.window import Window
from kivy.metrics import dp
from kivy.properties import StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import Screen
from kivy.lang import Builder
from kivymd.uix.card import MDCard
from kivymd.uix.managerswiper import MDSwiperPagination
from kivymd.theming import ThemeManager
class MySwiperManager(BoxLayout):
pass
class PreScreenManager(Screen):
theme_cls = ThemeManager()
theme_cls.primary_palette = 'Indigo'
swiper_manager = None
start_screen = MySwiperManager()
swiper_manager = start_screen.ids.swiper_manager
paginator = MDSwiperPagination()
paginator.screens = swiper_manager.screen_names
paginator.manager = swiper_manager
swiper_manager.paginator = paginator
start_screen.add_widget(paginator)
The context of my code is as follows: Name of file: Prelogin/prescreenmanager.kv
<ScreenOne@Screen>:
name: 'screen one'
Label:
text: 'Hello World'
<ScreenTwo@Screen>:
name: 'screen two'
<ScreenThree@Screen>:
name: 'screen three'
<ScreenFour@Screen>:
name: 'screen four'
<ScreenFive@Screen>:
name: 'screen five'
<PreScreenManager>:
MySwiperManager:
orientation: 'vertical'
MDSwiperManager:
id: swiper_manager
ScreenOne:
ScreenTwo:
ScreenThree:
ScreenFour:
ScreenFive:
main.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.anchorlayout import AnchorLayout
from kivy.factory import Factory
from kivymd.theming import ThemeManager
# Load the kv files
Builder.load_file("Prelogin/prescreenmanger.kv")
from Prelogin.prescreenmanger import PreScreenManager
class MainApp(App):
pass
if __name__ == "__main__":
MainApp().run()
main.kv
AnchorLayout:
canvas.before:
Rectangle:
size: self.size
pos: self.pos
source: "Start/Background.png"
anchor_x: 'center'
anchor_y: 'top'
Image:
source: 'Start/Loginlogo.png'
keep_ratio: False
allow_stretch: True
opacity: 1
size_hint: 1, 0.25
pos_hint: (0,0)
ScreenManager:
id: screen_manager
PreScreenManager:
id: PreScreenManager
Error (That I don't understand)
[INFO ] [Logger ] Record log in C:\Users\Bruger\.kivy\logs\kivy_19-11-23_9.txt
[INFO ] [deps ] Successfully imported "kivy_deps.gstreamer" 0.1.17
[INFO ] [deps ] Successfully imported "kivy_deps.glew" 0.1.12
[INFO ] [deps ] Successfully imported "kivy_deps.sdl2" 0.1.22
[INFO ] [Kivy ] v1.11.1
[INFO ] [Kivy ] Installed at "C:\Program Files\Python37\lib\site-packages\kivy\__init__.py"
[INFO ] [Python ] v3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)]
[INFO ] [Python ] Interpreter at "C:\Program Files\Python37\python.exe"
[INFO ] [Factory ] 184 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_pil, img_gif (img_ffpyplayer ignored)
[INFO ] [KivyMD ] v0.102.0
[INFO ] [Window ] Provider: sdl2
[INFO ] [GL ] Using the "OpenGL" graphics system
[INFO ] [GL ] GLEW initialization succeeded
[INFO ] [GL ] Backend used <glew>
[INFO ] [GL ] OpenGL version <b'4.5.0 - Build 24.20.100.6286'>
[INFO ] [GL ] OpenGL vendor <b'Intel'>
[INFO ] [GL ] OpenGL renderer <b'Intel(R) HD Graphics 620'>
[INFO ] [GL ] OpenGL parsed version: 4, 5
[INFO ] [GL ] Shading version <b'4.50 - Build 24.20.100.6286'>
[INFO ] [GL ] Texture max size <16384>
[INFO ] [GL ] Texture max units <32>
[INFO ] [Window ] auto add sdl2 input provider
[INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
[INFO ] [Text ] Provider: sdl2
[INFO ] [GL ] NPOT texture support is available
Traceback (most recent call last):
File "kivy\properties.pyx", line 860, in kivy.properties.ObservableDict.__getattr__
KeyError: 'swiper_manager'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\Users\Bruger\OneDrive\Dokumenter\Python - Java - kivy\Kivy2\Initium\Start\main.py", line 10, in <module>
from Prelogin.prescreenmanger import PreScreenManager
File "C:\Users\Bruger\OneDrive\Dokumenter\Python - Java - kivy\Kivy2\Initium\Prelogin\prescreenmanger.py", line 16, in <module>
class PreScreenManager(Screen):
File "C:\Users\Bruger\OneDrive\Dokumenter\Python - Java - kivy\Kivy2\Initium\Prelogin\prescreenmanger.py", line 21, in PreScreenManager
swiper_manager = start_screen.ids.swiper_manager
File "kivy\properties.pyx", line 863, in kivy.properties.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__'
Upvotes: 0
Views: 139
Reputation: 39092
The build()
method of an App
returns the Widget
that will be the root of the App
display, The lines in the class TestApp
before the def build()
theme_cls = ThemeManager()
theme_cls.primary_palette = 'Indigo'
swiper_manager = None
create class variables that are shared by all instances of the Test
class (but since it is an App
, there should only be one instance).
So, in the build()
method, self.swiper_manager
references the class variable named swiper_manager
. And the line with ids
references the id
in the kv
file.
That is all fine, but in your prescreenmanager.py
, you have a bunch of code that is not in any method. That code, like the class variable creation in Test
, is run when Python loads the class, even before the App
is built. So, at that point, the ids
have not yet been defined, so you get the error you see.
And I still don't know what you are trying to do with that dangling code in prescreenmanager.py
.
Upvotes: 1