我正在制作一个GUI界面,在其中有一个函数生成标签,我想让用户有可能修改这些标签的文本。
但这对我来说是个巨大的头痛,因此我尝试了一个较小规模的实验,创建了一个标签,显示创建的标签数量,并尝试使用不同的解决方案更新“新标签数量文本”,但没有成功。
我尝试了线程,但失败了。然后我尝试了Kivy中的Clock对象,但我也失败了,这并不是因为它们无法工作,而是因为我是编程新手,我不太理解它们。 py:
class Screen_two(Screen):
productsamount = StringProperty(None)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Clock.schedule_once(self.update_amount_label, 0)
def update_amount_label(self, dt):
n = 0
for i in db.databaseDict.items():
n += 1
self.ids.productsamount.text = 'Amount of goods: {}'.format(n)
kv:
<Screen_two>:
productsamount: productsamount
Label:
font_size:'11dp'
id:productsamount
编辑1: 我想改变标签文本的def
add_product(self):
函数。class addbut_popup(FloatLayout):
addprodname = ObjectProperty(None)
addprodprice = ObjectProperty(None)
def print_to_consol(self):
print('Added.')
def add_product(self):
if self.addprodname.text != "" and '.' not in self.addprodname.text:
if re.findall(r'\D', self.addprodprice.text) == []:
db.add_product(self.addprodname.text, self.addprodprice.text)
self.print_to_consol()
self.reset()
else:
invalid()
self.reset()
else:
invalid()
self.reset()
def reset(self):
self.addprodname.text = ''
self.addprodprice.text = ''
编辑2:
我不想只改变标签的文本一次,而是希望每次调用函数(按下按钮)时都能更改它。例如:每当我在文本输入框中写入内容并按下按钮时,我希望将标签的文本更改为我在文本输入框中写的内容,而不仅仅是一次性的更改。(抱歉没有表达清楚,英语不是我的母语)