使用kivy出错重写python代码

2024-09-29 23:15:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我想重写一些代码。我的主要问题是我不理解Test(App)中的build(self)函数。self.swiper_manager是否引用了.kv文件中的swiper_manager = Nonestart_screen.ids.swiper_manager引用了MDSwiperManager: id: swiper_manager
我试图重写的代码。你知道吗

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

下面的代码是我重写的最佳尝试 文件名:Prelogin/预筛选经理.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)

我的代码的上下文如下: 文件名:Prelogin/预筛选管理器.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:

你知道吗主.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()

你知道吗主电源.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

错误(我不明白)

[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__'

Tags: fromimportselfinfomanagerscreenstartclass
1条回答
网友
1楼 · 发布于 2024-09-29 23:15:55

Appbuild()方法返回将作为App显示的根的Widget,即类TestApp中在def build()之前的行

theme_cls = ThemeManager()
theme_cls.primary_palette = 'Indigo'
swiper_manager = None

创建由Test类的所有实例共享的类变量(但是由于它是一个App,所以应该只有一个实例)。你知道吗

因此,在build()方法中,self.swiper_manager引用名为swiper_manager的类变量。带有ids的行引用kv文件中的id。你知道吗

这很好,但是在prescreenmanager.py中,有一堆代码是任何方法都没有的。这段代码与Test中的类变量创建一样,是在Python加载类时运行的,甚至在构建App之前也是如此。因此,此时,ids尚未定义,因此您将看到错误。你知道吗

我仍然不知道你想用prescreenmanager.py中的悬空代码做什么。你知道吗

相关问题 更多 >

    热门问题