gpt4 book ai didi

python - Kivy:如何用 'Label' 中的文本更新 'TextInput' 的文本?

转载 作者:太空宇宙 更新时间:2023-11-03 14:37:05 25 4
gpt4 key购买 nike

我正在制作一个 GUI,其中有一个生成标签的函数,我想让用户也可以更改这些标签的文本。

但这让我非常头疼,所以我尝试在较小的范围内创建一个标签来显示创建的标签数量,并尝试使用不同的解决方案更新“新数量标签文本”,但没有成功。

我试过线程但失败了。然后我尝试了 Kivy 中的时钟对象,但我也失败了,我没有失败是因为那些不起作用是因为我是编程新手而且我并不真正了解它们。:

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)

千伏:

<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:

我不想只更改 Label 的文本一次,但我想在每次调用函数(按下按钮)时更改它,例如:每当我在文本输入中写一些东西并按下按钮时,我想更改 Label 的文本到我在 Textinput 中写的内容,而且不仅仅是一次,而是每次按下按钮时。 (抱歉没说清楚(Englsih 不是我的母语))

最佳答案

我认为这应该有帮助

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import StringProperty, ObjectProperty
import random
from kivy.lang.builder import Builder


class YourWidget(Widget):
def __init__(self, **kwargs):
super(YourWidget, self).__init__(**kwargs)


def change_text(self):
self.label1.text = str(self.ids.input1.text)



Builder.load_string('''
<YourWidget>:
input1:input1
label1:label1
BoxLayout:
orientation: 'vertical'
size: root.size
TextInput:
id: input1
Label:
id: label1
text: ''
Button:
id: button1
text: "Change text"
on_release: root.change_text()

''')


class YourApp(App):
def build(self):
return YourWidget()


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

这会帮助你更好地理解它

关于python - Kivy:如何用 'Label' 中的文本更新 'TextInput' 的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57163638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com