【问题标题】:Why is my navigation code not working? kivy python为什么我的导航代码不起作用?基维蟒
【发布时间】:2021-08-23 11:49:41
【问题描述】:

我有一个屏幕管理器ThirdWindow,它应该在点击B1 时更改屏幕,基本上是一个导航。请注意ThirdWindow 是一个不同的类。当B1 被点击时,callback 被调用。我想它会在ThirdWindow 的帮助下改变屏幕。我使用了self.manager.current,它没有用,我还尝试通过在Dre 类中将ThirdWindow 设为def,但结果是一样的。 def callback(self, instance) 内部的 print 语句没有被打印,所以我认为 def 根本没有被称为 on_click。我该如何解决?谢谢

import kivy
from kivy.app import App
from kivy.core.audio import SoundLoader
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.stacklayout import StackLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.graphics import Rectangle
from kivy import platform
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen

class MainWindow(Screen):
    pass



class ThirdWindow(ScreenManager):
    def load(self):
        sm = ScreenManager
        sm.add_widget(Dre(name='Dre'))
        sm.add_widget(SecondWindow(name='SecondWindow'))
        self.sm.current = 'Dre'

class Dre(RelativeLayout):
    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()
        self.kv = Builder.load_file('Levels.py')

        if platform in ('linux', 'win', 'macosx'):
            with self.canvas.before:
                self.bg = Rectangle(size=self.size, source='Neo.png')

            self.bind(pos=self.update_bg)
            self.bind(size=self.update_bg)
        else:
            with self.canvas.before:
                self.bg = Rectangle(size=self.size, source='Neon.png')

            self.bind(pos=self.update_bg)
            self.bind(size=self.update_bg)


    def update_bg(self, *args):
        if platform in ('linux', 'win', 'macosx'):
            self.bg.pos = self.pos
            self.bg.size = self.size
            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='')
            # B1.bind(on_press=return self.kv)
            self.add_widget(B1)

        else:
            self.bg.pos = self.pos
            self.bg.size = self.size
            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)


    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]
        if platform in ('linux', 'win', 'macosx'):
            with self.canvas.before:
                self.bg = Rectangle(size=self.size, color=self.color)





class LabApp(App):
    def build(self):
        return Dre()


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




if you provide any solution please provide it in .py file.

【问题讨论】:

  • 'Levels.py 中有什么内容?
  • Levels.py 是另一个具有空类的文件夹。我之前尝试将另一个文件夹用于不同的屏幕,但随后将它们全部移动到一个脚本中。那么它会导致问题吗?请帮忙。我仍然无法弄清楚问题

标签: python python-3.x kivy kivy-language


【解决方案1】:

您的代码存在许多问题。

首先,您已经定义了一个ScreenManager 类(ThirdWindow),但是您还没有创建它的实例或将它包含在您的GUI 中。为了使用ScreenManager 管理Screens,您必须将它添加到您的GUI。我建议修改您的 LabApp 类以使用 ThirdWindow

class LabApp(App):
    def build(self):
        return ThirdWindow()

并修改ThirdWindow 以将load() 方法替换为__init__() 方法:

class ThirdWindow(ScreenManager):
    def __init__(self, **kwargs):
        super(ThirdWindow, self).__init__(**kwargs)
        self.add_widget(Dre(name='Dre'))
        self.add_widget(SecondWindow(name='SecondWindow'))

由于Dre 类被用作Screen,因此必须将其重新定义为Screen

class Dre(Screen):

您可以将callback 添加到B1 Button 的创建中:

        B1 = Button(text='P L A Y', font_name='Roboto-Bold.ttf', size_hint=(.2, .15), on_release=self.callback,
                 pos_hint={'center_x': .5, "center_y": .3}, background_color = self.color, background_normal='')

【讨论】:

    猜你喜欢
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    • 2014-01-23
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    • 2016-01-23
    相关资源
    最近更新 更多