Home > Software design >  Dynamic label text. / Python / Kivy
Dynamic label text. / Python / Kivy

Time:11-07

I do a very simple programm, is about 2 buttons , when pressed they are displaying in IDE terminal what button was pressed and how many times. Now I try to display the counting number in a label above each button, I tried a lot of things that I saw on internet but nothing with success and I am stuck here for a couple of days... Heeelppp pleasee !!

I tried to solve my problem with StringProperty() but i don't know what i am doing wrong..

`

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

`

CodePudding user response:

main.py

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