基维:如何用“TextInput”中的文本更新“Label”的文本?

2024-09-29 19:25:24 发布

您现在位置:Python中文网/ 问答频道 /正文

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

千伏:

^{pr2}$

编辑1: 我想用defadd_product(self):函数更改标签文本。在

^{3}$

编辑2:

我不想只更改标签的文本一次,但我想在每次调用函数(按钮按下)时更改标签文本。例如:每当我在文本输入中写入内容并按下按钮时,我希望将标签文本更改为我在Textinput中所写的内容,而不是仅更改一次,而是每次按下按钮时。(对不起,我没有说清楚(英语不是我的母语)


Tags: 函数文本self数量initdefargsupdate
3条回答

试试这个。您有一个对事件有本机支持的按钮。在

例如,它有“按”或“放”。对于这些事件,您可以附加将被触发的函数。在

生产日期:

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)

千伏:

^{pr2}$

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

以下是您的代码的一个版本,我对其进行了修改以实现您的目标:

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

我想这应该有帮助

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

这会帮助你更好地理解它

相关问题 更多 >

    热门问题