【问题标题】:Kivy toolbar malfunctioningKivy 工具栏出现故障
【发布时间】:2020-12-19 23:10:44
【问题描述】:

我正在尝试使用 kivymd 构建一个包含多个“卡片” 的窗口,其中包含某些信息和一些功能按钮。我想要做的是将 cards 添加到 grid layout 并在 floating layout 中暂停该网格,但我有一些看附图的问题。另外请建议是否有更好的方法来做到这一点。 这是窗口的代码。稍后我将制作一些功能,以便在按下某些按钮时添加更多卡片

<ProjectCard@MDGridLayout>
    md_bg_color: 0, 1, 1, 1
    cols: 1
    MDToolbar:
        right_action_items: [["dots-vertical", lambda x: print(x)]]
        title: "Title"
        size_hint_y:0.3
    MDLabel:
        size_hint_y:0.1
        valign:"bottom"
        text:"mm:hh dd:mm:yy AM"
    MDLabel:
        size_hint_y:0.3
        multiline: True
        text:"john wick, richie rich, courage, garelt"
        valign:"top"
        adaptive_height: True
    GridLayout:
        rows:1
        cols:2
        size_hint_y:0.3
        MDLabel:
            text:"Status: " + "working"
        MDIconButton:
            icon:"details"
            adaptive_height: True




<ProjectsPage>
    FloatLayout
        MDGridLayout:
            pos_hint: {'center_x':0.5}
            size_hint_x:0.95
            spacing:[10, 10]
            cols:1
            ProjectCard:
                size_hint_y:0.3
            ProjectCard:
                size_hint_y:0.3
            ProjectCard:
                size_hint_y:0.3

kivy窗口的形象

【问题讨论】:

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


    【解决方案1】:

    我认为您想使用按钮添加新卡片,并且希望轻松查看所有卡片。因此,您可以在页面上使用 ScrollView。您也可以根据需要设置此页面的网格列。我将此选项设置为 2。我在 .py 文件中创建了这些卡片。您也可以在 .kv 中制作它。我希望这些代码可以帮助您了解如何通过操作轻松创建新卡片。 ma​​in.py:

    #main.py:
    from kivy.uix.screenmanager import Screen,ScreenManager
    from kivymd.uix.boxlayout import MDBoxLayout
    from kivymd.app import MDApp
    from kivy.properties import ObjectProperty
    class ProjectsPage(Screen):
        cards_grid = ObjectProperty()
        def add_card(self):
            card = Card()
            self.cards_grid.add_widget(card)
    class Card(MDBoxLayout):
        pass
    class SM(ScreenManager):
        pass
    class my(MDApp):
        def build(self):
            self.theme_cls.theme_style = "Dark"
            return SM()
    if __name__ == '__main__':
        my().run()
    

    my.kv:

    <SM>:
        ProjectsPage:
    <ProjectCard@MDGridLayout>
        size_hint_y:None
        height: '250sp'
        md_bg_color: 0, 1, 1, .5
        cols: 1
    <ProjectsPage>:
        cards_grid:cards_grid
        BoxLayout:
            padding: '25sp','25sp','25sp','25sp'
            orientation: 'vertical'
            ScrollView:
                size_hint_y:.9
                do_scroll_x:False
                do_scroll_y:True
                GridLayout:
                    id: cards_grid
                    spacing: '25sp'
                    size_hint_y:None
                    height: self.minimum_height
                    cols: 2
                    Card:
                    Card:
            Button:
                size_hint_y:.1
                text: 'Add New Card'
                on_release: root.add_card()
    <Card>:
        orientation: 'vertical'
        size_hint_y:None
        height: '250sp'
        md_bg_color: 0, 1, 1, .5
        MDToolbar:
            right_action_items: [["dots-vertical", lambda x: print(x)]]
            title: "Title"
            size_hint_y:0.3
        MDLabel:
            size_hint_y:0.1
            valign:"bottom"
            text:"mm:hh dd:mm:yy AM"
        MDLabel:
            size_hint_y:0.3
            multiline: True
            text:"john wick, richie rich, courage, garelt"
            valign:"top"
            adaptive_height: True
        GridLayout:
            rows:1
            cols:2
            size_hint_y:0.3
            MDLabel:
                text:"Status: " + "working"
            MDIconButton:
                icon:"details"
                adaptive_height: True
    

    【讨论】:

    • 非常感谢,我在项目页面中添加了滚动视图。做了一些调整,现在很完美。我的票不算数,但我告诉你这很有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多