按下按钮时如何更改空间? [英] How to change a space when a button is pressed with kivy?

查看:98
本文介绍了按下按钮时如何更改空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过实现代码易于遵循,但是我希望能够在每次按下按钮时重新配置drawingspace.kv,例如说这样的话:

I am trying create a GUI by implementing the template of the ComicCreator GUI sample as a template for my own project. The code is easy to follow, but I would like to be able to reconfigure the drawingspace.kv, each time a button is pushed, say for example something like this:

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

Q: How could I configure the drawingspace.kv to have a different layout with different widgets for each button that is pressed?

推荐答案

一种简洁的方法是使用屏幕.

A neat way to do this is to use screen.

由于我已经从您先前的问题中获得了该应用程序的示例,因此很容易实现屏幕并稍微重写类.

Since I allready have an example of this app from you earlier question, it was easy to implement the screens, and rewrite the classes a bit.

按下按钮时,您可以将屏幕管理器的电流设置为您想要的屏幕名称.

When a button is pressed, you set the screenmanager's current to whatever the name you named the screen you want.

然后,您只需要在每个屏幕内,在kv文件或python文件中编辑布局即可.

Then you just edit the layouts as you want inside of each screen, in the kv file, or python file.

我选择在这里用kv语言制作大部分布局内容.因为我发现以这种方式开发布局更加容易. 以后,如果需要,我可以将其重写为python.

I choose to make most of the layout stuff in kv language here. Because I find it easier to develop a layout the way I want it this way. Later I could rewrite it to python if I want that.

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

So my python file looks like this now:

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文件如下所示:

And kv.kv file looks like this:

#: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"

这篇关于按下按钮时如何更改空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆