How to insert all the rows in a postgresql table to a kivy label in a kv file as text?

100 views
Skip to first unread message

Lovindu Lochana

unread,
Apr 9, 2020, 5:12:17 AM4/9/20
to Kivy users support
I have a table with 21 rows which is known as expences. I want to insert all the rows in the table to a kivy label as text. But it shows only the last row. I wasted hours on this. But I was unable to find a solution. What can i do for it? And can you say me how to add a label for each row in the table?

This is the code in 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 :" + app.t_w
pos: 600,450
size_hint: (.3, .2)

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

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

SmoothLabel:
id: Date_lbl
text: app.fulldate
size_hint: (.3, .2)
pos: 30, 450

SmoothLabel:
id: Expences_lbl
text: ("Expences :" + app.total_expences)
size_hint: (.3, .2)
pos: 30,410

SmoothLabel:
id: Income_lbl
text: ("Income     :" + app.total_income)
size_hint: (.3, .2)
pos: 30, 370
ScrollView:
size_hint: (.4,.6)
do_scroll_x: False
do_scroll_y: True
pos: 30,30
Label:
size_hint_y: None
height: self.texture_size[1]
text: app.index+")   "+app.date+"    "+app.details+"    "+app.amount +'\n'
haligh: 'left'



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
import datetime
from kivy.config import Config
from kivy.properties import StringProperty, NumericProperty
import psycopg2
from kivy.uix.label import Label

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

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

connection = psycopg2.connect(user="postgres",password="postgres",host="localhost",port="5432",database="money_db")
cursor = connection.cursor() 

query_cash = "select cash from main where index = 1;"
query_savings = "select savings from main where index = 1;"
query_income = "select sum(amount) from income where month="+"'"+month+"'"+";"
query_expences = "select sum(amount) from expences where month="+"'"+month+"'"+";"

cursor.execute(query_income)
total_income_amount, = cursor.fetchone()

cursor.execute(query_expences)
total_expences_amount, = cursor.fetchone()

cursor.execute(query_cash)
cash_amount, = cursor.fetchone()

cursor.execute(query_savings)
savings_amount, = cursor.fetchone()

cursor.execute("commit;")

q_e_n = "select count(*) from expences where month="+"'"+month+"'"+";"
q_e = "select index, date, details, amount from expences where month="+"'"+month+"'"+";"

cursor.execute(q_e_n)
e_n, = cursor.fetchone()

cursor.execute(q_e)
expences = cursor.fetchall()

cursor.execute("commit;")

Builder.load_file('total_wealth.kv')

class Money_Manager(App, FloatLayout):
    fulldate = StringProperty()
    total_income = StringProperty()
    total_expences = StringProperty()
    cash = StringProperty()
    savings = StringProperty()
    t_w = StringProperty()
    index = StringProperty()
    date = StringProperty()
    details = StringProperty()
    amount = StringProperty()
    index2= NumericProperty()
    
    def build(self):
        i = 1
        for row in expences:
            index_amount = str(i)
            date_amount = str(row[1])
            details_amount = str(row[2])
            amount_amount =  str(row[3])
            
        
            i = i+1        


            self.index = index_amount
            self.date = date_amount
            self.details = details_amount
            self.amount = amount_amount
        
        self.fulldate = (month+", "+date)
        self.total_income = str(total_income_amount)
        self.total_expences = str(total_expences_amount)
        self.cash = str(cash_amount)
        self.savings = str(savings_amount)
        self.t_w = str(cash_amount+savings_amount)   
        
        return self
 
    
Money_Manager().run()


Elliot Garbus

unread,
Apr 9, 2020, 1:48:20 PM4/9/20
to kivy-...@googlegroups.com

There are a number of issues here:

Dynamically create a label for each data element.  This is the fundamental problem.

  • Move for for loop out of build() and to on_start()
  • Create a class derived from Label, and for each iteration of the for loop create an instance of this new class and add it to the scrollview.  Put the attributes you want to display on the label in this class (date, details, amount).
  • You will do this by using the add_widget method off of the scroll view.  Give scrollview an id to access scroll view from python.

 

Other Items:

  1. Separate App and MoneyManager(), there is no reason for these to be combined.  Each will get more complex as you move forward.
  2. There is a lot script code where you are accessing the database.  This should move into a class, or class methods.
  3. In your for loop you are using i, instead you can do the following:

        for i, row in enumerate(expences):

--
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/ac48a029-96b2-4964-9c3e-21d206c8e9a0%40googlegroups.com.

 

Reply all
Reply to author
Forward
0 new messages