I want to exchange line 18 text with line 23 lable text, but here line 18 doesn't work in my code? Any idea?!

from kivy.app import App

from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label

def timer():
    delta = datetime.datetime(2015, 3, 21, 2, 15, 11) - datetime.datetime.now()
    days = delta.days
    hour_string = str(delta).split(', ')[1]
    hours = hour_string.split(':')[0]
    minuts = hour_string.split(':')[1]
    seconds = hour_string.split(':')[2]
    seconds_1 = hour_string.split(':')[2].split('.')[0]
    #print ("%s days" % days)
    #print ("%s hours" % hours)
    #print ("%s minuts" % minuts)
    #print ("%s seconds" % seconds)
    self.l_days.text = days

class CountdownApp(App):
    def build(self):
        b = BoxLayout()
        l_days = Label(text = "days")
        l_hours = Label(text = "hours")
        l_minuts = Label(text = "minuts")
        l_seconds = Label(text = "seconds")

        b.add_widget(l_days)
        b.add_widget(l_hours)
        b.add_widget(l_minuts)
        b.add_widget(l_seconds)
        return b


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

Recommended Answers

All 6 Replies

days may not be a string
do a test print with
print(type(days))

I checked and it was int. I changed it into str and chenged afew parts and clear some extra part of code, it's more clear now:

from kivy.app import App

from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label

def timer(self):
    delta = datetime.datetime(2015, 3, 21, 2, 15, 11) - datetime.datetime.now()
    days = delta.days

    new_days = str(days)
    self.l_days.text = new_days

class CountdownApp(App):
    def build(self):
        b = BoxLayout()
        self.l_days = Label(text = "days")
        b.add_widget(self.l_days)
        return b

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

As you see here:

        new_days = str(days)
        self.l_days.text = new_days

I can't set the label text with a varible.

How about ...

from kivy.app import App
from kivy.uix.label import Label

class LabelApp(App):
    def build(self):
        mytext = str(1234567)
        # you can only return one widget, aka the root widget
        return Label(text=mytext, font_size=60)

LabelApp().run()

Well, thank you @vegaseat, it works.
But still don't know how to set my code wich has some different labels, here with your code i can print only one label with return label, but i have some values that i need to put in different labels.

To return several labels, return the layout in which the labels are ...

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout

class LabelApp(App):
    def build(self):
        mytext2 = str(1234567)
        self.label2 = Label(text=mytext2, font_size=60)
        mytext3 = str(7654321)
        self.label3 = Label(text=mytext3, font_size=60)
        layout = BoxLayout(orientation='vertical')
        layout.add_widget(self.label2)
        layout.add_widget(self.label3)
        # return layout containing several widgets                
        return layout

LabelApp().run()

Yes it works, thank you @vegaseat.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.