Kivy:如何使用“TextInput”的文本更新“Label”的文本?

3

我正在制作一个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:

我不想只改变标签的文本一次,而是希望每次调用函数(按下按钮)时都能更改它。例如:每当我在文本输入框中写入内容并按下按钮时,我希望将标签的文本更改为我在文本输入框中写的内容,而不仅仅是一次性的更改。(抱歉没有表达清楚,英语不是我的母语)


更改self.ids.productsamount.text的值会自动更改文本。我认为你的问题是你甚至没有触发update_amount_label函数。为什么不添加一个按钮,在单击时触发该函数呢? - Martin
@Matrin,你是正确的Martin。很抱歉回复得晚了,现在我只需要弄清如何在另一个类的方法中激活一个函数...帮助将不胜感激,因为我尝试了但没有成功。 - Mate
4个回答

1

1

这是我修改过的代码版本,可以实现你想要的功能:

from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.screenmanager import Screen, ScreenManager


class Screen_two(Screen):
    # StringProperty that will be the Label text
    productsamount = StringProperty('This is going to change')

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # change label after 5 seconds
        Clock.schedule_once(self.update_amount_label, 5)

    def update_amount_label(self, dt):
        n = 0
        for i in range(5):
            n += 1

        # update label text
        self.productsamount = 'Amount of goods: {}'.format(n)

Builder.load_string('''
<Screen_two>:
    Label:
        text: root.productsamount    # use the StringProperty for Label text
        font_size:'11dp'
''')

class tmpApp(App):
    def build(self):
        sm = ScreenManager()
        sm.add_widget(Screen_two(name='screen2'))
        return sm

tmpApp().run()

很抱歉回复晚了,约翰·安德森先生,但我一直在试错中解决这个问题。您的答案很有帮助,确实改变了标签的文本,但对我没有帮助。因为我想要连续地改变标签的文本(每当单击“a”按钮时)。我将在我的Edit1中添加按钮功能,这样您就可以更好地理解了。 - Mate
在按钮回调方法中,只需将“productsamount”设置为您想要出现在“Label”中的任何内容。 - John Anderson

1
尝试类似以下的代码。你有一个原生支持事件的按钮。
例如,它具有'on_press'或'on_release'。你可以将函数附加到这些事件上,以便触发该函数。 py:
class Screen_two(Screen):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)


    def update_amount_label(self):
        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

    Button:
        on_press: root.update_amount_label()

ps: 我没有测试过,但我认为它应该可以工作



谢谢你的帮助,马丁。我也尝试了你的解决方案,但按钮只能改变标签一次,然后就不能再改变了。 - Mate
那是因为你需要在函数内使用对象变量。 - Martin

1

我认为这应该会有所帮助

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()

这将帮助你更好地理解它。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接