【问题标题】:How to change a space when a button is pressed with kivy?用kivy按下按钮时如何更改空格?
【发布时间】:2016-07-15 05:55:43
【问题描述】:

我正在尝试通过将ComicCreator GUI 示例的模板实现为我自己项目的模板来创建 GUI。 code 很容易理解,但我希望能够在每次按下按钮时重新配置 drawingspace.kv,例如这样说:

问:我如何配置 drawingspace.kv 以使每个按下的按钮具有不同的布局和不同的小部件?

【问题讨论】:

    标签: python raspberry-pi kivy kivy-language


    【解决方案1】:

    一个巧妙的方法是使用屏幕。

    因为我已经从你之前的问题中获得了这个应用程序的示例,所以很容易实现屏幕,并稍微重写类。

    当按下按钮时,您将屏幕管理器的当前设置为您想要的屏幕名称。

    然后您只需在每个屏幕、kv 文件或 python 文件中根据需要编辑布局。

    我选择在这里用 kv 语言制作大部分布局内容。因为我发现以我想要的方式开发布局更容易。 以后如果我愿意,我可以将它重写为 python。

    所以我的 python 文件现在看起来像这样:

    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.properties import StringProperty
    from kivy.clock import Clock
    from kivy.uix.screenmanager import Screen,ScreenManager,NoTransition
    from kivy.lang import Builder
    import time
    
    
    Builder.load_file("kv.kv")
    
    
    class MyLayout(BoxLayout):
    
        def __init__(self,**kwargs):
            super(MyLayout,self).__init__(**kwargs)
            self.orientation = "vertical"
            self.padding = 10
    
    
    class MainScreen(Screen):
        pass
    
    
    class RemoveScreen(Screen):
        pass
    
    
    class GroupScreen(Screen):
        pass
    
    
    class MyLogo(BoxLayout):
    
        your_time = StringProperty()
        def __init__(self,**kwargs):
            super(MyLogo,self).__init__(**kwargs)
            Clock.schedule_interval(self.set_time, 0.1)
    
        def set_time(self,dt):
            self.your_time = time.strftime("%m/%d/%Y %H:%M")
    
    
    
    
    class MyApp(App):
        def __init__(self,**kwargs):
            super(MyApp,self).__init__(**kwargs)
            self.sm = ScreenManager(transition=NoTransition())
    
            self.sm.add_widget(MainScreen(name = "main"))
            self.sm.add_widget(RemoveScreen(name = "remove"))
            self.sm.add_widget(GroupScreen(name = "group"))
    
            self.sm.current = "main"
    
        def build(self):
            return self.sm
    
    
    if __name__ == "__main__":
        MyApp().run()
    

    而 kv.kv 文件看起来像这样:

    #:kivy 1.9.1
    
    <MyButtons@BoxLayout>:
        padding: 10,10,10,0
        spacing: 10
        size_hint: 1,0.3
        orientation: "horizontal"
        Button:
            text: "Clear"
            on_press: app.sm.current = "main"
        Button:
            text: "Remove"
            on_press: app.sm.current = "remove"
        Button:
            text: "Group"
            on_press: app.sm.current = "group"
        Button:
            text: "Color"
        Button:
            text: "Gestures"
    
    <MyLogo>:
        spacing: 10
        padding: 10,10,10,0
        orientation: "horizontal"
        BoxLayout:
            orientation: "vertical"
            size_hint: 0.3,1
            canvas:
                Rectangle:
                    pos: self.pos
                    size: self.size
            AsyncImage
                source: 'http://lmsotfy.com/so.png'
            Label:
                size_hint: 1,0.3
                text: root.your_time
                color: [0,0,0,1]
            Label:
                size_hint: 1,0.3
                text: "NYC, New York, USA"
                color: [0,0,0,1]
    
    
    <MainScreen>:
        MyLayout:
            MyLogo:
                #Button:
                #    text: "main"
    
            MyButtons:
                #buttons
    
            BoxLayout:
                padding: 10,10,10,10
                size_hint: 1,0.3
                Button:
                    text: "Total figures: 1          Kivy Started"
    
    
    <RemoveScreen>:
        MyLayout:
            MyLogo:
                BoxLayout:
                    orientation: "horizontal"
                    Label:
                        font_size: "40sp"
                        text: "Remove"
                    Button:
                        font_size: "20sp"
                        text: "Remove this or something"
    
            MyButtons:
                #buttons
    
            BoxLayout:
                padding: 10,10,10,10
                size_hint: 1,0.3
                Button:
                    text: "Total figures: 1          Kivy Started"
    
    
    <GroupScreen>:
        MyLayout:
            MyLogo:
                BoxLayout:
                    orientation: "vertical"
                    Label:
                        font_size: "40sp"
                        text: "Group"
                    Button:
                        font_size: "20sp"
                        text: "Something groups stuff"
    
            MyButtons:
                #buttons
    
            BoxLayout:
                padding: 10,10,10,10
                size_hint: 1,0.3
                Button:
                    text: "Total figures: 1          Kivy Started"
    

    【讨论】:

    • 除了example中的文件之外,kv.kv是一个新文件吗?
    • @3kstc 是的,它在 .py 文件 Builder.load_file("kv.kv") 的第 10 行由 python 加载
    • 你是天赐之物!
    • 我很难实现“表格”,就像问题中描述的那样 - 6 行 x 2 列? - 有什么建议?第 1 列将有标准文本,第 2 列文本将从 MySQL 表中检索。
    • @3kstc 你可以在里面放置一个网格布局
    【解决方案2】:

    布局框架应为a screen manager,每个布局为a screen。然后通过按下按钮触发屏幕转换。如果您不知道如何操作,也可以观看教程here,但文档应该足够了。

    【讨论】:

    • 谢谢,实际上我在发帖之前已经阅读了它......但我不明白 如何code 中实现它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    • 2019-06-28
    • 2021-02-12
    相关资源
    最近更新 更多