Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python读取/写入kivy小部件属性_Python_Widget_Kivy_Textinput - Fatal编程技术网

使用python读取/写入kivy小部件属性

使用python读取/写入kivy小部件属性,python,widget,kivy,textinput,Python,Widget,Kivy,Textinput,我有一个基本的Python工作知识,我正在努力自学kivy。我希望能够让Python向kivy小部件读写数据 想象一下,有一个地址簿应用程序可以将日期和时间插入文本输入。当应用程序启动时,只需让Python获取日期和时间并正确插入即可 此程序代码将给出一个简单地址簿的示例: from kivy.app import App from kivy.uix.gridlayout import GridLayout from kivy.uix.label import Label from kivy.

我有一个基本的Python工作知识,我正在努力自学kivy。我希望能够让Python向kivy小部件读写数据

想象一下,有一个地址簿应用程序可以将日期和时间插入文本输入。当应用程序启动时,只需让Python获取日期和时间并正确插入即可

此程序代码将给出一个简单地址簿的示例:

from kivy.app import App

from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput

class AddressApp(App):
    def build(self):
        pass

if __name__ == '__main__':
AddressApp().run()
这是它的address.kv文件:

GridLayout:
    cols: 2
    Label:
    text: 'Date'
TextInput:
    id: textinputdate
Label:
    text: 'Time'
TextInput:
    id: textinputtime
Label:
    text: 'Name'
TextInput:
    id: textinputname
Label:
    text: 'Address'
TextInput:
    id: textinputaddress
Label:
    text: 'email'
TextInput:
    id: textinputemail
Label:
    text: 'Phone'
TextInput:
    id: textinputphone

之后,如果我想让Python阅读。。。我不知道。。。UH电话号码文本输入,如何实现?

如果您希望某个小部件具有额外功能(例如:在应用程序启动时加载当前日期),请创建该小部件的自定义版本,以满足要求。读取规则中小部件的值非常简单。例如:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.textinput import TextInput
from kivy.clock import Clock
import time

gui = '''
BoxLayout:
    orientation: 'vertical'

    GridLayout:
        cols: 2

        Label:
            text: 'current time'

        DateInput:
            id: date_input

    Button:
        text: 'write date to console'
        on_press: print(date_input.text)
'''


class DateInput(TextInput):

    def __init__(self, **kwargs):
        super(DateInput, self).__init__(**kwargs)
        Clock.schedule_interval(self.update, 1)  # update every second

    def update(self, dt):
        self.text = time.ctime()


class Test(App):

    def build(self):
        return Builder.load_string(gui)


Test().run()

kivy的FreeNode IRC频道上一个叫spinningD20的家伙给我看了这个

有一种比添加自定义小部件更简单的方法。只要你只想在应用程序启动时在TextInput中插入一个值

address.py

from kivy.app import App

from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput

import time

class AddressApp(App):
        def build(self):
            self.root.ids.textinputdate.text = time.strftime("%x")

if __name__ == '__main__':
        AddressApp().run()
地址:千伏

GridLayout:
    cols: 2
    Label:
        text: 'Date'
    TextInput:
        id: textinputdate
    Label:
        text: 'Time'
    TextInput:
        id: textinputtime
    Label:
        text: 'Name'
    TextInput:
        id: textinputname
    Label:
        text: 'Address'
    TextInput:
        id: textinputaddress
    Label:
        text: 'email'
    TextInput:
        id: textinputemail
    Label:
        text: 'Phone'
    TextInput:
        id: textinputphone

在这里旋转20。公平地说,我回答了他的直接问题,但随后又解释了如何将其封装到自定义小部件中:

from kivy.app import App

from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput

import time


class DateInput(TextInput):
    def __init__(self, **kwargs):
        super(DateInput, self).__init__(**kwargs)
        self.text = time.strftime("%x")


class Container(GridLayout):
    def __init__(self, **kwargs):
        # using super calls the base class's init.  We'll hand it keyword arguments we received, just in case
        super(Container, self).__init__(**kwargs)
        # now we can do stuff here
        self.ids.textinputtime.text = 'from python'


class AddressApp(App):
        def build(self):
            pass

if __name__ == '__main__':
        AddressApp().run()
以千伏为单位:

# anything without the <> symbols is part of the App's kv.  So here's the one thing the App will have in its kv
Container:

# here's the custom widget's kv, just like your previous example
<Container>:
    cols: 2
    Label:
        text: 'Date'
    DateInput:
        id: dateinputdate
    Label:
        text: 'Time'
    TextInput:
        id: textinputtime
    Label:
        text: 'Name'
    TextInput:
        id: textinputname
    Label:
        text: 'Address'
    TextInput:
        id: textinputaddress
    Label:
        text: 'email'
    TextInput:
        id: textinputemail
    Label:
        text: 'Phone'
    TextInput:
        id: textinputphone
#任何没有符号的东西都是应用程序的一部分。因此,这是该应用程序的一个特点
容器:
#这是定制小部件的kv,就像前面的示例一样
:
科尔斯:2
标签:
文本:“日期”
日期输入:
id:dateinputdate
标签:
文字:“时间”
文本输入:
id:textinputtime
标签:
文本:“名称”
文本输入:
id:textinputname
标签:
文本:“地址”
文本输入:
id:textinputaddress
标签:
文本:“电子邮件”
文本输入:
id:textinputemail
标签:
短信:“电话”
文本输入:
id:textinputphone

我希望这有帮助!祝你好运,戴夫

另一个示例,解释如何通过父网格布局使用当前日期填充文本输入,避免应用程序类保持干净:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.uix.gridlayout import GridLayout
import time

gui = '''
#:import time time
DateGrid
    cols: 1

    Label:
        text: 'Customer data'

    TextInput:
        id: date_input

    TextInput:
        id: name_input

    TextInput:
        id: email_input

    Button:
        text: 'refresh date'
        on_press: date_input.text = time.ctime()
'''


class DateGrid(GridLayout):

    def __init__(self, **kwargs):
        super(DateGrid, self).__init__(**kwargs)
        Clock.schedule_once(self.populate_inputs, 0.5)

    def populate_inputs(self, *x):
        _ = self.ids

        _.date_input.text = time.ctime()
        _.name_input.text = 'Foo Snowman'
        _.email_input.text = 'foo.snowman@gravy.com'


class Test(App):

    def build(self):
        return Builder.load_string(gui)


Test().run()

强烈建议不要将任何代码放在
build
方法中,这与应用程序的一般行为无关。有时你想在其他应用程序中重复使用小部件,这个解决方案使得不进行不必要的进一步重写是不可能的。换句话说,这些东西应该嵌入到定制的小部件中,以节省将来的工作。这取决于具体情况。在本例中,我将在TextInput中输入一个默认值。换句话说,我正在进行应用程序设置。我不需要创建一个自定义文本输入库,如果我创建了,我最终会创建六个自定义文本输入库,这比我在启动时向其中插入值要多。然后将它们填充到父窗口小部件中,在本例中是网格布局。这只是一个很好的做法。你能举个例子吗?