You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Kivy框架中仅通过.py文件实现页面导航跳转功能

Kivy纯Python实现页面导航解决方案

问题核心原因

  • 原Dre类继承自RelativeLayout而非Screen,没有Screen类自带的manager属性,无法调用跳转方法
  • 根组件未使用ScreenManager,缺少页面管理的上下文,无法实现多页面切换
  • 桌面端的PLAY按钮未绑定点击回调,仅移动端按钮做了绑定配置
  • SecondWindow的背景未绑定尺寸/位置变化监听,窗口拉伸时背景会显示异常
  • 原页面跳转的逻辑注释未启用,且相关配置缺失

完整可运行代码

import kivy
from kivy.app import App
from kivy.core.audio import SoundLoader
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.relativelayout import RelativeLayout
from kivy.graphics import Rectangle
from kivy import platform
from kivy.uix.screenmanager import ScreenManager, Screen

class Dre(Screen):
    def __init__(self, **kwargs):
        super(Dre, self).__init__(**kwargs)
        self.color = [254/255, 102/255, 37/255, 1]
        self.H_color = [254/255, 102/255, 37/255]
        self.sound_theme = None
        self.init_audio()

        # 背景初始化
        if platform in ('linux', 'win', 'macosx'):
            with self.canvas.before:
                self.bg = Rectangle(size=self.size, source='Neo.png')
            # 初始化控件
            self.add_widget(Label(text='D  R  E  A  M  S',
                          pos_hint={'center_x': .5, 'center_y': .8},
                          font_size='60dp', font_name='Roboto-Bold.ttf', color=self.H_color))
            B1 = Button(text='P L A Y', font_name='Roboto-Bold.ttf', size_hint=(.2, .15),
                     pos_hint={'center_x': .5, "center_y": .3}, background_color = self.color, background_normal='',
                     on_press=self.callback)
            self.add_widget(B1)
        else:
            with self.canvas.before:
                self.bg = Rectangle(size=self.size, source='Neon.png')
            # 初始化控件
            self.add_widget(Label(text='D  R  E  A  M  S',
                                  pos_hint={'center_x': .5, 'center_y': .8},
                                  font_size='30dp', font_name='Roboto-Bold.ttf', color=self.H_color))
            B1 = Button(text='P L A Y', font_name='Roboto-Bold.ttf', size_hint=(.2, .15),
                        pos_hint={'center_x': .5, "center_y": .3}, background_color=self.color, background_normal='',
                        on_press=self.callback)
            self.add_widget(B1)
        
        self.bind(pos=self.update_bg)
        self.bind(size=self.update_bg)

    def update_bg(self, *args):
        # 仅更新背景,避免重复创建控件
        self.bg.pos = self.pos
        self.bg.size = self.size

    def callback(self, instance):
        print('working')
        self.manager.current = 'SecondWindow'

    def init_audio(self):
        self.sound_theme = SoundLoader.load('Bg_theme.mp3')
        self.sound_theme.volume = 1
        self.sound_theme.loop = True
        if self.sound_theme:
            self.sound_theme.play()
            print('okay')
        else:
            print('not okay')


class SecondWindow(Screen):
    def __init__(self, **kwargs):
        super(SecondWindow, self).__init__(**kwargs)
        self.color = [254 / 255, 102 / 255, 37 / 255, 1]
        with self.canvas.before:
            self.bg = Rectangle(size=self.size, color=self.color)
        self.bind(pos=self.update_bg, size=self.update_bg)
    
    def update_bg(self, *args):
        self.bg.pos = self.pos
        self.bg.size = self.size


class LabApp(App):
    def build(self):
        # 初始化页面管理器,添加所有页面
        sm = ScreenManager()
        sm.add_widget(Dre(name='Dre'))
        sm.add_widget(SecondWindow(name='SecondWindow'))
        sm.current = 'Dre'
        return sm


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

内容的提问来源于stack exchange,提问作者ash2021

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 08:45:04