0

How to create simple Kivy app?

If user type text into field "Name" using keyboard like on android phone, this name is display

I need this to learn

4

1 回答 1

6

这是我能想到的一个简单的例子。

这是 KVLANG 代码。

<LblTxt@BoxLayout>:
    orientation: 'horizontal'      
    lblTxtIn: 'default'
    theTxt: iAmTxt
    Label:
        text: root.lblTxtIn
    TextInput:
        id: iAmTxt  
        text: 'txt'        

<MyLayout@BoxLayout>:  
    orientation: 'vertical'

    LblTxt:   
        id: lt0
        lblTxtIn: 'LblTxtInput0'

    LblTxt:   
        id: lt1
        lblTxtIn: 'LblTxtInput1'

    LblTxt:   
        id: lt2
        lblTxtIn: 'LblTxtInput2'


    Button:
        text: 'print LblTxtInput [0, 1, 2]'
        on_release: print lt0.theTxt.text, lt1.theTxt.text, lt2.theTxt.text

MyLayout

这是Python代码。

import kivy
kivy.require('1.8.0') # replace with your current kivy version !

from kivy.app import App
from kivy.lang import Builder
from kivy.config import Config
from kivy.core.window import Window

Window.size = (400,130)

from kivy.uix.boxlayout import BoxLayout

class LblTxt(BoxLayout):
    from kivy.properties import ObjectProperty
    theTxt = ObjectProperty(None)

class MyApp(App):

    def build(self):
        self.root = Builder.load_file('simpleForm.kv')
        return self.root

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

这是运行截图。当“print LblTxtInput [0, 1, 2]”按钮被释放时,它将打印 abc 到命令行。

在此处输入图像描述

我希望这能够帮到你。

于 2014-10-06T18:09:26.773 回答