【问题标题】:Dynamic label text. / Python / Kivy动态标签文本。 / 蟒蛇 / 基维
【发布时间】:2022-11-06 09:14:32
【问题描述】:

我做了一个非常简单的程序,大约有 2 个按钮,当按下它们时,它们会在 IDE 终端中显示按下了什么按钮以及按下了多少次。现在我尝试在每个按钮上方的标签中显示计数,我尝试了很多我在互联网上看到的东西,但没有成功,我被困在这里几天......Heeelppp pleasee !

我试图用 StringProperty() 解决我的问题,但我不知道我做错了什么..

`

import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty, NumericProperty, StringProperty


class ButoaneleMele(Widget):
    total_label = StringProperty()
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.numaratoarebtn1 = 0
        self.numaratoarebtn2 = 0
        self.total_label = " initial"

    def update_numaratoarebtn1(self):

        self.numaratoarebtn1 += 1
        self.total = "Total Clicks for Button 1: " + str(self.numaratoarebtn1)
        print(self.total)

    def update_numaratoarebtn2(self):

        self.numaratoarebtn2 += 1
        self.total = "Total Clicks for Button 2: " + str(self.numaratoarebtn2)
        print(self.total)

    def display_label(self):
        label = "text update"
        self.parent.ids.counter.update_label(label)

    def update_label(self, param):
        self.param = param
        self.total_label = param
        print(param)



btn = ButoaneleMele()



class TablaMeaApp(App):  # <- Main Class
    def build(self):
        return ButoaneleMele()


if __name__ == "__main__":
    TablaMeaApp().run()

`

`

<ButoaneleMele>:
    FloatLayout:

        Label:
            id: counter
            pos_hint:{"x":2.6,"y":2.5}
            text: root.total_label
        Button:
            pos_hint:{"x":2,"y":2}
            text: "Butonul 1"
            on_press: root.update_numaratoarebtn1()

        Label:
            pos_hint:{"x":4.6,"y":2.5}
            text: " 2 "
        Button:
            pos_hint:{"x":4,"y":2}
            text: "Butonul 2"
            on_press: root.update_numaratoarebtn2()

`

【问题讨论】:

    标签: python string dynamic kivy label


    【解决方案1】:

    主文件

    from kivy.app import App
    from kivy.uix.widget import Widget
    
    class ButoaneleMele(Widget):
        def __init__(self, **kwargs):
            super().__init__(**kwargs)
            self.numaratoarebtn1 = 0
            self.numaratoarebtn2 = 0
    
        def update_numaratoarebtn1(self):
            self.numaratoarebtn1 += 1
            self.ids.counter1.text = f"Total Clicks for Button 1: {self.numaratoarebtn1}"
            print(self.ids.counter1.text)
    
        def update_numaratoarebtn2(self):
            self.numaratoarebtn2 += 1
            self.ids.counter2.text = f"Total Clicks for Button 2: {self.numaratoarebtn2}"
            print(self.ids.counter2.text)
    
    class TablaMeaApp(App):
        def build(self):
            return ButoaneleMele()
    
    if __name__ == "__main__":
        TablaMeaApp().run()
    

    tablamea.kv

    <ButoaneleMele>:
        FloatLayout:
            Label:
                id: counter1
                pos_hint:{"x":2,"y":2.6}
                text: 'Total Clicks for Button 1: 0'
            Button:
                pos_hint:{"x":2,"y":2}
                text: "Butonul 1"
                on_press: root.update_numaratoarebtn1()
            Label:
                id: counter2
                pos_hint:{"x":4,"y":2.6}
                text: 'Total Clicks for Button 2: 0'
            Button:
                pos_hint:{"x":4,"y":2}
                text: "Butonul 2"
                on_press: root.update_numaratoarebtn2()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多