如何在kivy上的小部件之间传递数据

时间:2016-10-05 01:51:12

标签: python kivy kivy-language

我正在尝试在Kivy框架中构建一个应用程序并需要在窗口小部件之间传递数据,例如,从所有输入中获取值,并在单击按钮“开始”时在控制台中打印所有值来自输入的。

main.py

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout  
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.label import Label
from kivy.graphics import Color
from kivy.properties import VariableListProperty, OptionProperty,ObjectProperty
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button

class Banner(Widget):
    def __init__(self, **kwargs):
        super(Banner, self).__init__(**kwargs)

class TestName(Widget):
    def __init__(self, **kwargs):
        super(TestName, self).__init__(**kwargs)

class TimeSampling(Widget):
    def __init__(self, **kwargs):
        super(TimeSampling, self).__init__(**kwargs)

class ImageName(Widget):
    def __init__(self, **kwargs):
        super(ImageName, self).__init__(**kwargs)

class StartButton(Widget):
    def __init__(self, **kwargs):
        super(StartButton, self).__init__(**kwargs)

class ContainerBox(BoxLayout):
    def __init__(self, **kwargs):
        super(ContainerBox, self).__init__(**kwargs)

class EyeData(App):
    title = 'EyeData Desktop App'

    def build(self):
        return ContainerBox()

if __name__ == '__main__':
    EyeData().run()

eyedata.kv

<Banner>:
    BoxLayout:
        canvas.before:
            Color:
                rgba: 0, 0.6, 0.7, 0.9
            Rectangle:
                pos: self.pos
                size: self.size
        size: root.size
        pos:root.pos
        orientation: 'horizontal'
        Label:
            text: '[b]EyeData[/b]'
            markup: True
            font_size: 30
            text_size: self.size
            valign: 'bottom'
            line_height: 2
            padding_x: 40
        Label:
            text: '[i]A smart solution for markting[/i]'
            markup: True
            font_size: 20
            text_size: self.size
            halign: 'right'
            valign: 'bottom'
            line_height: 3
            padding_x: 40

<TestName>:
    BoxLayout:
        canvas.before:
            Color:
                rgba: 1, 1, 1, 0.2
        Rectangle:
            pos: self.pos
            size: self.size
        size: root.size
        pos: root.pos
        orientation:'horizontal'

        Label:
            text: '[b]Test Name:[/b]'
            markup: True
            font_size:15
            text_size: self.size
            valign: 'middle'
            size_hint_x: 0.2
            padding_x: 40

        AnchorLayout:
            size_hint_x: 0.8
            anchor_y: 'center'
            anchor_x: 'left'
            padding: (0,0,40,0)

        TextInput:
            font_size: 18
            size_hint_y: None
            height: 30
            text: ""
            multiline: False


<TimeSampling>:
    BoxLayout:
        canvas.before:
            Color:
                rgba: 1, 1, 1, 0.2
            Rectangle:
                pos: self.pos
                size: self.size
        size: root.size
        pos: root.pos
        orientation:'horizontal'

        Label:
            text: '[b]Time of Sampling(sec):[/b]'
            markup: True
            font_size:15
            text_size: self.size
            valign: 'middle'
            size_hint_x: 0.3
            padding_x: 40

        AnchorLayout:
            anchor_y: 'center'
            anchor_x: 'left'
            padding: (0,0,40,0)

            TextInput:
                font_size: 18
                size_hint_y: None
                height: 30

<ImageName>:
    BoxLayout:
        canvas.before:
            Color:
                rgba: 1, 1, 1, 0.2
            Rectangle:
                pos: self.pos
                size: self.size
        size: root.size
        pos: root.pos
        orientation:'horizontal'

        Label:
            text: '[b]Image:[/b]'
            markup: True
            font_size:15
            text_size: self.size
            valign: 'middle'
            size_hint_x: 0.3
            padding_x: 40

        AnchorLayout:
            size_hint_x: 0.2
            anchor_y: 'center'
            anchor_x: 'center'

            Button:
                text: 'Select:'
                size_hint_y: None
                height: 30

        AnchorLayout:
            anchor_y: 'center'
            anchor_x: 'left'
            padding: (0,0,40,0)

            TextInput:
                font_size: 18
                size_hint_y: None
                height: 30

<StartButton>:
    BoxLayout:
        canvas.before:
            Color:
                rgba: 1, 1, 1, 0.2
            Rectangle:
                pos: self.pos
                size: self.size
        size: root.size
        pos: root.pos
        orientation:'horizontal'

        AnchorLayout:
            anchor_x: 'center'
            anchor_y: 'center'

            Button:
                size_hint_y: None
                size_hint_x: None
                height: 40
                width: 400
                text: 'Start'
                background_color: 0, 0.5, 1, 1

<ContainerBox>:
    spacing:1
    orientation:'vertical'
    Banner: 
    TestName:
    TimeSampling:
    ImageName:
    StartButton:

如果您对代码有任何建议,欢迎您!

0 个答案:

没有答案
相关问题