How to insert a text from a variable to a kivy label which is made by me?

1,064 views
Skip to first unread message

Lovindu Lochana

unread,
Apr 8, 2020, 3:23:16 AM4/8/20
to Kivy users support
I want to insert a text from a variable to a label which is made by me. I want to enter the date in the variable fuldate in to a label which is known as Date_lbl. But it gives an error like " NameError: name 'fulldate' is not defined". I tried a lot and I can't find a way to do this. 

This is the code in the py file.
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
import datetime
from kivy.config import Config

Config.set('graphics', 'width', '900')
Config.set('graphics', 'height', '500')
Config.write()

month = datetime.datetime.now().strftime("%B")
date = datetime.datetime.now().strftime("%w")
fulldate = (month+", "+date)

Builder.load_file('total_wealth.kv')

class Money_Manager(App, FloatLayout):
    def build(self):     
        return self
 
    
Money_Manager().run()


This is the code of the kv file.
<SmoothLabel@Label>
background_color: (0,0,0,0)
background_normal: ''
back_color: (255,255,255,1)
border_radius: [18]
canvas.before:
Color:
rgba: (255,255,255,0.3)
RoundedRectangle:
size: self.size
pos: self.pos
radius: self.border_radius

<Money_Manager>

FloatLayout:
size_hint_y: None
height:150
Image:
pos: 0,350
source:'image4.png'
size: self.texture_size
allow_stretch: True
keep_ratio: False

SmoothLabel:
id: Total_Wealth
text: "Total Wealth :"
pos: 600,450
size_hint: (.3, .2)

SmoothLabel:
id: Cash
text: "Cash             :"
size_hint: (.3,0.2)
pos: 600,410

SmoothLabel:
id: Savings
text: "Savings        :"
size_hint: (.3,0.2)
pos: 600,370

SmoothLabel:
id: Date_lbl
text: fulldate



th




Elliot Garbus

unread,
Apr 8, 2020, 9:10:26 AM4/8/20
to kivy-...@googlegroups.com

I’ve made a few changes.  I created a new class, MoneyManager that inherits from FloatLayout.  I have included your variables as members of this class.  In kv, I added full date to the Label by using root.fulldate.  see: https://kivy.org/doc/stable/api-kivy.lang.html?highlight=lang#value-expressions-on-property-expressions-ids-and-reserved-keywords

 

I also added a method to the class to highlight how a kivy property can be used to create a dynamic binding between your python code and kivy. When savings is updated, it is automatically updated on the screen.

 

------------------------

 

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder
from kivy.config import Config
from kivy.properties import NumericProperty

import datetime

Config.set(
'graphics', 'width', '900')
Config.set(
'graphics', 'height', '500')
# Config.write() This writes to kivy config


class MoneyManager(FloatLayout):
    month = datetime.datetime.now().strftime(
"%B")
    date = datetime.datetime.now().strftime(
"%w")
    fulldate = (month +
", " + date)
   
# Make these kivy properties if they are going to be updated
   
savings = NumericProperty(0.00)

   
def add_savings(self):
       
self.savings += 100

class MoneyManagerApp(App):
   
def build(self):
       
return Builder.load_file('total_wealth.kv')


MoneyManagerApp().run()

 

 

 

<SmoothLabel@Label>
  
background_color: (0,0,0,0)
  
background_normal: ''
  
back_color: (255,255,255,1)
   border_radius: [
18]
  
canvas.before:
     
Color:
        
rgba: (255,255,255,0.3)
     
RoundedRectangle:
        
size: self.size
         pos
: self.pos
        
radius: self
.border_radius

MoneyManager:
   
size_hint_y: None
   
height:150
   
Image:
       
pos: 0,350
       
source:'image4.png'
       
size: self.texture_size
       
allow_stretch: True
        keep_ratio: False

    SmoothLabel:
       
id: Total_Wealth
       
text: "Total Wealth :"
       
pos: 600,450
       
size_hint: (.3, .2)

    SmoothLabel:
       
id: Cash
       
text: "Cash             :"
       
size_hint: (.3,0.2)
       
pos: 600,410

   
SmoothLabel:
       
id: savings
       
text: "Savings        : " +  str(root.savings)
       
size_hint: (.3,0.2)
       
pos: 600,370

   
BoxLayout:
        SmoothLabel:
           
id: Date_lbl
           
text: root.fulldate  # root is the root of this rule, MoneyManager

       
Button:
           
text: 'Add to Savings'
           
on_release: root.add_savings()

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/26f60817-e1ba-4a29-9545-241a1516e71e%40googlegroups.com.

 

Reply all
Reply to author
Forward
0 new messages