如何在Kivy切换屏幕?

发布于 2025-01-30 14:23:50 字数 2584 浏览 5 评论 0原文

我目前在代码中遇到问题,无法切换屏幕。在我说明问题是什么之前,我将在下面包括我的代码段。

来自main.py

import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen

Window.clearcolor = 255 / 255, 255 / 255, 255 / 255, 1
Window.size = 414, 736

sm = ScreenManager()

class PulseWelcome(Screen):
    pass

    class PulseWelcomeCanvas(Widget):
        pass

class PulseLogin(Screen):

    class PulseLoginCanvas(Widget):
        pass

class WindowManager(ScreenManager):
    pass

sm.add_widget(PulseWelcome(name = 'welcome'))
sm.add_widget(PulseLogin(name = 'login'))

class PulseApp(App):
    def build(self):
        return PulseWelcome()

if __name__ == '__main__':
    PulseApp().run()

来自pulse.kv

    #:kivy 2.1.0

WindowManager:
    PulseWelcome:
    PulseLogin:

<PulseWelcome>:

    id: welcome

    PulseWelcomeCanvas:

        Label:
            font_size: 30  
            font_name: 'assets/fonts/tommy.ttf'
            center_x: root.width / 2
            center_y: root.height / 2
            text: "Welcome to Pulse!"
            color: 0, 0, 0, 1

        Button:
            background_normal: 'assets/images/next_purple_normal.png'
            background_down: 'assets/images/next_purple_down.png'
            border: 0, 0, 0, 0
            center_x: root.width / 2
            center_y: 90
            height: 40
            width: 100
            on_press:
                root.manager.transition.direction = 'left'
                root.manager.current = 'login'

<PulseLogin>:

    id: login

    PulseLoginCanvas:

        Label:
            font_size: 30
            font_name: 'assets/fonts/tommy.ttf'
            text: 'Sign In or Sign Up'
            color: 0, 0, 0, 1
            center_x: root.width / 2
            center_y: root.height / 2

我的应用程序在程序启动时应运行,但是当我单击按钮以更改屏幕时,我被介绍了有了这个错误:

File "C:\Users\Nitro\Documents\Pulse\pulse.kv", line 30, in <module>
 root.manager.transition.direction = 'left'
 AttributeError: 'NoneType' object has no attribute 'transition'

我尝试了各种解决方案而没有进步。我的猜测是,因为我试图将屏幕从屏幕类内的小部件切换,所以我给出了较早的讨论错误。我最初决定在每个屏幕类中放一个小部件以整理元素,因为当我试图在.kv文件中组织屏幕类的元素时(pulsewelcome等),我遇到了GUI问题。

我还尝试在屏幕类中定义一个可以通过包含的小部件激活的函数。经过尝试后,我得到了这个错误:

kivy.uix.screenmanager.ScreenManagerException: ScreenManager accepts only Screen widget.

不确定是否有人知道可能导致问题以及如何解决问题,但是如果有人对之前提到的任何问题都有任何类型的输入,那么所有帮助都将非常感谢。

I am currently having an issue in my code where I am unable to switch screens. Before I explain what the issue is specifically, I will include a snippet of my code below.

From main.py

import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen

Window.clearcolor = 255 / 255, 255 / 255, 255 / 255, 1
Window.size = 414, 736

sm = ScreenManager()

class PulseWelcome(Screen):
    pass

    class PulseWelcomeCanvas(Widget):
        pass

class PulseLogin(Screen):

    class PulseLoginCanvas(Widget):
        pass

class WindowManager(ScreenManager):
    pass

sm.add_widget(PulseWelcome(name = 'welcome'))
sm.add_widget(PulseLogin(name = 'login'))

class PulseApp(App):
    def build(self):
        return PulseWelcome()

if __name__ == '__main__':
    PulseApp().run()

From Pulse.kv

    #:kivy 2.1.0

WindowManager:
    PulseWelcome:
    PulseLogin:

<PulseWelcome>:

    id: welcome

    PulseWelcomeCanvas:

        Label:
            font_size: 30  
            font_name: 'assets/fonts/tommy.ttf'
            center_x: root.width / 2
            center_y: root.height / 2
            text: "Welcome to Pulse!"
            color: 0, 0, 0, 1

        Button:
            background_normal: 'assets/images/next_purple_normal.png'
            background_down: 'assets/images/next_purple_down.png'
            border: 0, 0, 0, 0
            center_x: root.width / 2
            center_y: 90
            height: 40
            width: 100
            on_press:
                root.manager.transition.direction = 'left'
                root.manager.current = 'login'

<PulseLogin>:

    id: login

    PulseLoginCanvas:

        Label:
            font_size: 30
            font_name: 'assets/fonts/tommy.ttf'
            text: 'Sign In or Sign Up'
            color: 0, 0, 0, 1
            center_x: root.width / 2
            center_y: root.height / 2

My app runs as it should when the program is started, however, when I click on my button in order to change screens I am presented with this error:

File "C:\Users\Nitro\Documents\Pulse\pulse.kv", line 30, in <module>
 root.manager.transition.direction = 'left'
 AttributeError: 'NoneType' object has no attribute 'transition'

I have tried a variety of solutions with no progress. My guess is that because I am trying to switch screens from a widget inside my screen class I am given the earlier discussed error. I originally decided to put a widget inside each screen class to organize elements because when I had tried to organize elements in the .kv file for the screen class independently (PulseWelcome, etc.) I had gui issues.

I also had tried defining a function in the screen class that could be activated through the contained widget. After trying that I was given this error:

kivy.uix.screenmanager.ScreenManagerException: ScreenManager accepts only Screen widget.

Not sure if anyone out there knows what may be causing the issue and how to fix it but if anyone has any sort of input for any of the issues previously mentioned all help is greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断念 2025-02-06 14:23:50

您尚未初始化ScreenManager返回屏幕,而不是ScreenManager

main.py:

import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen

Window.clearcolor = 255 / 255, 255 / 255, 255 / 255, 1
Window.size = 414, 736

class PulseWelcome(Screen):
    pass

    class PulseWelcomeCanvas(Widget):
        pass

class PulseLogin(Screen):

    class PulseLoginCanvas(Widget):
        pass

class WindowManager(ScreenManager):
    pass


class PulseApp(App):
    def build(self):
        sm = ScreenManager()
        sm.add_widget(PulseWelcome(name='welcome'))
        sm.add_widget(PulseLogin(name='login'))
        return sm

if __name__ == '__main__':
    PulseApp().run()

定义这样的构建功能应​​该可以解决问题。

.KV:

<PulseWelcome>:
    id: welcome
    PulseWelcomeCanvas:
        Label:
            font_size: 30
            font_name: 'assets/fonts/tommy.ttf'
            center_x: root.width / 2
            center_y: root.height / 2
            text: "Welcome to Pulse!"
            color: 0, 0, 0, 1
        Button:
            background_normal: 'assets/images/next_purple_normal.png'
            background_down: 'assets/images/next_purple_down.png'
            border: 0, 0, 0, 0
            center_x: root.width / 2
            center_y: 90
            height: 40
            width: 100
            on_press:
                root.manager.transition.direction = 'left'
                root.manager.current = 'login'
<PulseLogin>:

    id: login

    PulseLoginCanvas:

        Label:
            font_size: 30
            font_name: 'assets/fonts/tommy.ttf'
            text: 'Sign In or Sign Up'
            color: 0, 0, 0, 1
            center_x: root.width / 2
            center_y: root.height / 2

我还从KV文件中删除了ScreenManager,因为在创建ScreenManger时,就像我在这里所做的那样。否则,您将不得不创建像这样的构建函数

def build:
    returm WindowManager()

我在这样做的构建功能上遇到了一些麻烦,所以现在我会以我第一次向您展示的方式做到这一点,

希望这会有所帮助

You have not initialized the screenmanager you returning the screen and not the screenmanager

main.py:

import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen

Window.clearcolor = 255 / 255, 255 / 255, 255 / 255, 1
Window.size = 414, 736

class PulseWelcome(Screen):
    pass

    class PulseWelcomeCanvas(Widget):
        pass

class PulseLogin(Screen):

    class PulseLoginCanvas(Widget):
        pass

class WindowManager(ScreenManager):
    pass


class PulseApp(App):
    def build(self):
        sm = ScreenManager()
        sm.add_widget(PulseWelcome(name='welcome'))
        sm.add_widget(PulseLogin(name='login'))
        return sm

if __name__ == '__main__':
    PulseApp().run()

defining the build function like this should do the trick.

.kv:

<PulseWelcome>:
    id: welcome
    PulseWelcomeCanvas:
        Label:
            font_size: 30
            font_name: 'assets/fonts/tommy.ttf'
            center_x: root.width / 2
            center_y: root.height / 2
            text: "Welcome to Pulse!"
            color: 0, 0, 0, 1
        Button:
            background_normal: 'assets/images/next_purple_normal.png'
            background_down: 'assets/images/next_purple_down.png'
            border: 0, 0, 0, 0
            center_x: root.width / 2
            center_y: 90
            height: 40
            width: 100
            on_press:
                root.manager.transition.direction = 'left'
                root.manager.current = 'login'
<PulseLogin>:

    id: login

    PulseLoginCanvas:

        Label:
            font_size: 30
            font_name: 'assets/fonts/tommy.ttf'
            text: 'Sign In or Sign Up'
            color: 0, 0, 0, 1
            center_x: root.width / 2
            center_y: root.height / 2

I have also removed the screenmanager from the kv file as that wouldn't function when creating the screenmanger like I have done here. Otherwise you would have to create the build function like this

build:

def build:
    returm WindowManager()

I have run in to some trouble with doing the build function like that so for now I would do it the way I first showed you

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文