Python 用kivy按下按钮时如何更改空格?

Python 用kivy按下按钮时如何更改空格?,python,raspberry-pi,kivy,kivy-language,Python,Raspberry Pi,Kivy,Kivy Language,我正在尝试通过将示例的模板作为我自己项目的模板来实现GUI。很容易理解,但我希望能够在每次按下按钮时重新配置drawingspace.kv,例如: #:kivy 1.9.1 <MyButtons@BoxLayout>: padding: 10,10,10,0 spacing: 10 size_hint: 1,0.3 orientation: "horizontal" Button: text: "Clear"

我正在尝试通过将示例的模板作为我自己项目的模板来实现GUI。很容易理解,但我希望能够在每次按下按钮时重新配置
drawingspace.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"


Q:我如何配置
drawingspace.kv
使其具有不同的布局,每个按钮都有不同的小部件?

布局框架应该是,每个布局都是。然后按下按钮即可触发屏幕切换。如果您不知道如何做,也可以观看教程,但文档应该足够了。

一个简单的方法是使用screen

由于我已经准备好了一个来自您前面问题的应用程序示例,所以很容易实现屏幕,并稍微重写一些类

当按下按钮时,您可以将screenmanager的当前值设置为所需屏幕的名称

然后,您只需在每个屏幕内部、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"
#:kivy 1.9.1
:
填充:10,10,10,0
间距:10
尺寸提示:1,0.3
方向:“水平”
按钮:
正文:“清楚”
按:app.sm.current=“main”
按钮:
正文:“删除”
按:app.sm.current=“删除”
按钮:
案文:“集团”
按:app.sm.current=“组”
按钮:
文字:“颜色”
按钮:
文字:“手势”
:
间距:10
填充:10,10,10,0
方向:“水平”
盒子布局:
方向:“垂直”
尺寸提示:0.3,1
画布:
矩形:
pos:self.pos
大小:self.size
异步映像
资料来源:'http://lmsotfy.com/so.png'
标签:
尺寸提示:1,0.3
文本:root.your_time
颜色:[0,0,0,1]
标签:
尺寸提示:1,0.3
文本:“纽约,纽约,美国”
颜色:[0,0,0,1]
:
我的布局:
迈洛戈:
#按钮:
#正文:“主要”
我的按钮:
#钮扣
盒子布局:
填充:10,10,10,10
尺寸提示:1,0.3
按钮:
文字:“总计数字:1辆Kivy已启动”
:
我的布局:
迈洛戈:
盒子布局:
方向:“水平”
标签:
字体大小:“40sp”
正文:“删除”
按钮:
字体大小:“20sp”
文本:“删除此项或其他内容”
我的按钮:
#钮扣
盒子布局:
填充:10,10,10,10
尺寸提示:1,0.3
按钮:
文字:“总计数字:1辆Kivy已启动”
:
我的布局:
迈洛戈:
盒子布局:
方向:“垂直”
标签:
字体大小:“40sp”
案文:“集团”
按钮:
字体大小:“20sp”
文字:“某个东西组的东西”
我的按钮:
#钮扣
盒子布局:
填充:10,10,10,10
尺寸提示:1,0.3
按钮:
文字:“总计数字:1辆Kivy已启动”

谢谢,在发布之前,我已经仔细阅读了。。。。但是我不知道如何在.Is
kv.kv
中实现它,除了在@3kstc中的文件之外,还有一个新文件。是的,它由python在.py文件
Builder.load\u文件(“kv.kv”)的第10行加载。
你真是天赐之物!我很难实现“表”,就像问题中描述的那样——6行2列?-有什么建议吗?第1列将有标准文本,第2列将从MySQL表中检索文本。@3kstc您可以在其中放置gridlayout