当一个按钮按下kivy如何改变的空间?(How to change a space when a b

2019-09-28 15:21发布

我试图通过实现的模板创建GUI ComicCreator GUI样品为我自己的项目模板。 该代码是容易做到,但我希望能够重新配置drawingspace.kv ,每按下一个按钮的时候,比方说像这样:

问:我怎么能配置drawingspace.kv有不同的布局,被按下的每个按钮不同部件?

Answer 1:

一个巧妙的办法做,这是使用的屏幕。

因为我有媒体链接从你刚才的问题这个应用程序的一个例子,这是容易实现的屏幕,并重写类一点。

当按下一个按钮,您可以设置屏幕管理器的电流,以任何名义,你命名你想要的画面。

只要你想每一个屏幕内然后你只需编辑布局,在KV文件,或Python文件。

我选择让大多数的布局东西在这里千伏语言。 因为我觉得这样更容易地开发布局我想是这样的方式。 后来,我可以把它改写到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"


Answer 2:

的布局框内应该是屏幕管理器 ,并且每个布局的屏幕 。 屏幕转换将通过按下按钮来触发,然后。 您还可以观看教程在这里 ,如果你不知道如何做到这一点,但文档应该够了。



文章来源: How to change a space when a button is pressed with kivy?