Read/write kivy 小部件属性 python
Read/write kivy widget attributes with python
我对 Python 有基本的应用知识,我正在尝试自学 kivy。我希望能够 Python 向 kivy 小部件读取和写入数据。
假设有一个地址簿应用程序可以将日期和时间插入到 TextInput 中。当应用程序启动时,只需 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 阅读...我不知道...呃...phone number TextInput,该怎么做?
如果您希望某些小部件具有额外的功能(例如:在应用程序启动时加载当前日期),请创建满足要求的该小部件的自定义版本。在规则中读取小部件的值非常简单。示例:
#!/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()
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
spinningD20 在这里。公平地说,我回答了他的直接问题,但随后也解释了将其封装到自定义小部件中:
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
希望对您有所帮助!戴夫,祝你好运!
另一个示例,解释了如何通过父网格布局使用当前日期填充文本输入,避免使用 App class 以保持其整洁:
#!/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()
我对 Python 有基本的应用知识,我正在尝试自学 kivy。我希望能够 Python 向 kivy 小部件读取和写入数据。
假设有一个地址簿应用程序可以将日期和时间插入到 TextInput 中。当应用程序启动时,只需 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 阅读...我不知道...呃...phone number TextInput,该怎么做?
如果您希望某些小部件具有额外的功能(例如:在应用程序启动时加载当前日期),请创建满足要求的该小部件的自定义版本。在规则中读取小部件的值非常简单。示例:
#!/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()
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
spinningD20 在这里。公平地说,我回答了他的直接问题,但随后也解释了将其封装到自定义小部件中:
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
希望对您有所帮助!戴夫,祝你好运!
另一个示例,解释了如何通过父网格布局使用当前日期填充文本输入,避免使用 App class 以保持其整洁:
#!/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()