如何在循环中加上整数值
Posted
技术标签:
【中文标题】如何在循环中加上整数值【英文标题】:how to plus integer value in loop 【发布时间】:2018-06-21 19:03:53 【问题描述】:我有两个文件demo.py
和demo.kv
。有人可以帮我吗?
1
。 +Add More
添加动态行。当我点击Total Value
填充值后,它会显示类似151012
的字符串。不显示12+10+15=37
。我正在使用它的代码
test = ''
for val in values:
test = val[2]+test
self.total_value.text = test
2
。谁能告诉我如何在填充value
TextBox 后将值的总和放入Total value
TextBox,而不是点击Total Value
Box.Means 如何从value
TextBox 调用def test(self)
函数?
demo.py
from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import BooleanProperty, ListProperty, StringProperty, ObjectProperty, NumericProperty
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (500, 400)
class User(Screen):
total_value = ObjectProperty(None)
def add_more(self):
self.ids.rows.add_row()
def test(self):
values = []
rows = self.ids.rows
for row in reversed(rows.children):
vals = []
for ch in reversed(row.children):
if isinstance(ch, TextInput):
vals.append(ch.text)
if isinstance(ch, Button):
vals.insert(0, ch.text)
values.append(vals)
test = ''
for val in values:
test = val[2]+test
self.total_value.text = test
class Row(BoxLayout):
col_data = ListProperty(["?", "?", "?", "?", "?"])
button_text = StringProperty("")
col_data3 = StringProperty("")
col_data4 = StringProperty("")
def __init__(self, **kwargs):
super(Row, self).__init__(**kwargs)
class Rows(BoxLayout):
row_count = 0
def __init__(self, **kwargs):
super(Rows, self).__init__(**kwargs)
self.add_row()
def add_row(self):
self.row_count += 1
self.add_widget(Row(button_text=str(self.row_count)))
class Test(App):
def build(self):
self.root = Builder.load_file('demo.kv')
return self.root
if __name__ == '__main__':
Test().run()
demo.kv
<Row>:
size_hint_y: None
height: self.minimum_height
height: 40
Button:
text: root.button_text
size_hint_x: None
top: 200
TextInput:
text: root.col_data3
width: 300
TextInput:
text: root.col_data4
width: 300
<Rows>:
size_hint_y: None
height: self.minimum_height
orientation: "vertical"
User:
total_value:total_value
BoxLayout:
orientation: "vertical"
padding : 20, 5
BoxLayout:
orientation: "horizontal"
#padding : 10, 10
spacing: 10, 10
size: 450, 40
size_hint: None, None
Label:
size_hint_x: .2
text: "Number"
text_size: self.size
valign: 'bottom'
halign: 'center'
Label:
size_hint_x: .4
text: "name"
text_size: self.size
valign: 'bottom'
halign: 'center'
Label:
size_hint_x: .4
text: "Value"
text_size: self.size
valign: 'bottom'
halign: 'center'
ScrollView:
Rows:
id: rows
BoxLayout:
orientation: "horizontal"
padding : 10, 5
spacing: 10, 10
size: 200, 40
size_hint: None, None
Label:
size_hint_x: .7
text: "Total value"
TextInput:
id: total_value
on_focus:root.test()
BoxLayout:
orientation: "horizontal"
size_hint_x: .2
size_hint_y: .2
Button:
text: "+Add More"
on_press: root.add_more()
如果可以的话,这将是一个很大的帮助。
【问题讨论】:
【参考方案1】:要以简单的方式访问元素,您必须设置 id,在这种情况下,我将为与数字输入关联的 TextInput
设置一个,您还必须放置一个过滤器以仅接受数值:
TextInput:
id: number_input
text: root.col_data4
width: 300
input_filter: 'int'
那么test()
方法就简化为如下:
class User(Screen):
total_value = ObjectProperty(None)
def add_more(self):
self.ids.rows.add_row()
def test(self):
rows = self.ids.rows
total = 0
for row in rows.children:
text = row.ids.number_input.text
total += int(text) if text != "" else 0 # validate if the entry is not empty
self.total_value.text = str(total)
为了能够自动更新值,我们将文本更改链接到一个函数,并在其中调用test()
,为了访问测试,我们必须在 Screen 中放置一个 id:
User:
id: user
total_value: total_value
[...]
能够从App.get_running_app()
访问屏幕:
class Row(BoxLayout):
button_text = StringProperty("")
col_data3 = StringProperty("")
col_data4 = StringProperty("")
def __init__(self, *args, **kwargs):
super(Row, self).__init__(*args, **kwargs)
self.ids.number_input.bind(text=self.on_text)
def on_text(self, text_input, value):
App.get_running_app().root.test()
完整代码:
demo.py
from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import BooleanProperty, ListProperty, StringProperty, ObjectProperty, NumericProperty
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (500, 400)
class User(Screen):
total_value = ObjectProperty(None)
def add_more(self):
self.ids.rows.add_row()
def test(self):
rows = self.ids.rows
total = 0
for row in rows.children:
text = row.ids.number_input.text
total += int(text) if text != "" else 0
self.total_value.text = str(total)
class Row(BoxLayout):
button_text = StringProperty("")
col_data3 = StringProperty("")
col_data4 = StringProperty("")
def __init__(self, *args, **kwargs):
super(Row, self).__init__(*args, **kwargs)
self.ids.number_input.bind(text=self.on_text)
def on_text(self, text_input, value):
App.get_running_app().root.test()
class Rows(BoxLayout):
row_count = 0
def __init__(self, **kwargs):
super(Rows, self).__init__(**kwargs)
self.add_row()
def add_row(self):
self.row_count += 1
self.add_widget(Row(button_text=str(self.row_count)))
class Test(App):
def build(self):
self.root = Builder.load_file('demo.kv')
return self.root
if __name__ == '__main__':
Test().run()
demo.kv
<Row>:
size_hint_y: None
height: self.minimum_height
height: 40
Button:
text: root.button_text
size_hint_x: None
top: 200
TextInput:
text: root.col_data3
width: 300
TextInput:
id: number_input
text: root.col_data4
width: 300
input_filter: 'int'
<Rows>:
size_hint_y: None
height: self.minimum_height
orientation: "vertical"
User:
id: user
total_value: total_value
BoxLayout:
orientation: "vertical"
padding : 20, 5
BoxLayout:
orientation: "horizontal"
#padding : 10, 10
spacing: 10, 10
size: 450, 40
size_hint: None, None
Label:
size_hint_x: .2
text: "Number"
text_size: self.size
valign: 'bottom'
halign: 'center'
Label:
size_hint_x: .4
text: "name"
text_size: self.size
valign: 'bottom'
halign: 'center'
Label:
size_hint_x: .4
text: "Value"
text_size: self.size
valign: 'bottom'
halign: 'center'
ScrollView:
Rows:
id: rows
BoxLayout:
orientation: "horizontal"
padding : 10, 5
spacing: 10, 10
size: 200, 40
size_hint: None, None
Label:
size_hint_x: .7
text: "Total value"
TextInput:
id: total_value
on_focus:root.test()
BoxLayout:
orientation: "horizontal"
size_hint_x: .2
size_hint_y: .2
Button:
text: "+Add More"
on_press: root.add_more()
【讨论】:
【参考方案2】:试试:
test = 0
for val in values:
test = int(val[2])+test
self.total_value.text = str(test)
在你的 .kv 文件中试试这个:
TextInput:
id: total_value
text: root.test()
如果它不起作用,您可以随时使用Kivy Clock 定期调用您的函数。
【讨论】:
是的,我正在尝试解决这个问题,我记得我遇到过类似的问题,但我不记得解决方案了 ;) 感谢回复,但第 2 点解决方案给出错误 AttributeError: 'Row' object has no attribute 'test' 那么 Kivy Clock 应该可以工作了。现在我不能给你更好的解决方案.. :(以上是关于如何在循环中加上整数值的主要内容,如果未能解决你的问题,请参考以下文章