【问题标题】:Button on click do not change screen in kivymd单击按钮不会更改 kivymd 中的屏幕
【发布时间】:2020-04-04 18:13:02
【问题描述】:

我是 kivy 的新手,不明白为什么我在导航抽屉中的按钮没有更改为 kivymd 中的另一个类 单击时类 ContentNavigationDrawer 中有一个按钮我希望它更改为另一个屏幕 我不明白为什么它没有改变我已经阅读了有关 ScreenManager 的文档但仍然无法理解

这是我的代码 main.py

from kivy.clock import Clock
from kivy.properties import StringProperty
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.app import MDApp
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout


class AboutUs(Screen):
    pass


class Share(FloatLayout):
    data = {
        "facebook": "Facebook",
        "whatsapp": "WhatsApp",
        "instagram": "Instagram",
    }

    def callback(self, instance):
        print('Hello')
        print(instance.icon)



class ContentNavigationDrawer(BoxLayout):
    def hllo(self):
        print('Hello though')


class MainScreen(FloatLayout):

    def quote(self, *args):
        self.quote_text = 'I often think that the night is more alive and more richly colored than  
                                    the day. -Vincent van Gogh '
    quote_text = StringProperty()


class TtApp(MDApp):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.title = "Story A Day"
        self.theme_cls.theme_style = "Light"

    def build(self):
        self.theme_cls.primary_palette = "Blue"  # "Purple", "Red"
        main = MainScreen()
        Clock.schedule_once(main.quote, 5)


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

这是我的 main.kv 文件

Screen:
    AboutUs:

    MainScreen:

    NavigationLayout:

        ScreenManager:

            Screen:

                BoxLayout:
                    orientation: 'vertical'

                    MDToolbar:
                        title: "Quote"
                        anchor_title: 'left'
                        elevation: 10
                        left_action_items: [['menu', lambda x: nav_drawer.set_state()]]
                        mode: "free-center"

                    Widget:



        MDNavigationDrawer:
            title: 'Quote'
            id: nav_drawer
            swipe_distance: 10

            ContentNavigationDrawer:
                id: content_drawer


<ContentNavigationDrawer>
    name: 'main'
    background_color: 2, 3, 4, 5
    orientation: "vertical"
    padding: "8dp"
    spacing: "8dp"

    AnchorLayout:
        anchor_x: "right"
        size_hint_y: None
        height: avatar.height

        Image:
            id: avatar
            size_hint: None, None
            size: "56dp", "56dp"
            source: "data/logo/kivy-icon-256.png"

        MDLabel:
            anchor_y: 'left'
            text: "[b]Story A Day[/b]"
            theme_text_color: "Custom"
            text_color: 0, 0, 0, 1
            markup: True
            font_style: "H4"
            size_hint_y: None
            height: self.texture_size[1]

    MDRaisedButton:
        md_bg_color: 0, 0, 0, 1
        text: 'You Can'
        on_release: root.hllo()

    MDRaisedButton:
        text: 'Click Me'
        on_release: print('Hell')

    MDLabel:
        text: 'Story A Day' 
        font_style: "Button"
        size_hint_y: None
        height: self.texture_size[1]

    OneLineAvatarListItem:
        text: "About Us"

        IconLeftWidget:
            icon: "information"

    Button:
        text: 'hello'
        on_release: app.root.manager.current = 'Abt'


    ScrollView:


<MainScreen>:
    canvas:
        Color:
            rgba: 0, 0, 0, 1 
        Rectangle:
            pos: self.pos
            size: self.size

    Label:
        id: quote
        text: root.quote_text
        font_size: '20sp'
        size: self.texture_size
        pos_hint: {'x': -0.2, 'y': 0.27}


<AboutUs>:
    name: 'Abt'
    Label:
        text: 'Hellooo'

【问题讨论】:

    标签: python kivy python-3.7 kivy-language


    【解决方案1】:

    您必须将 ScreenManger 作为规则小部件并将您的 MainScreen 放在第一位,最后放入您的 AboutUs 类。

    【讨论】:

      【解决方案2】:

      首先将所有屏幕放入 ScreenManager。所以screenmanager可以控制换屏

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-13
        • 2021-06-21
        • 2015-12-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多