0

I tried to change label to button but doesn't work for my app, and I can't find a suitable solution.

I just want to select the data from a single row of the gridlayout. I doesnt matter if isn't 'selected' at all.

The best solution, and near to what I expected its to change as a button all the table, so instead of CustLabel@Label in the prodtable.py file changed it to CustLabel@Button.

prodtable.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.lang import Builder

from pymongo import MongoClient
from collections import OrderedDict

Builder.load_string('''
<ProdTable>:
    id: main_win
    orientation: 'vertical'
    canvas.before:
        Color:
            rgba: (1,1,1,1)
        Rectangle:
            size: self.size
            pos: self.pos
    RecycleView:
        viewclass: 'CustLabel'
        id: table_floor
        canvas.before:
            Color:
                rgba: (1,1,1,1)
            Rectangle:
                size: self.size
                pos: self.pos
        RecycleGridLayout:
            id: table_floor_layout
            default_size_hint: (1, None)
            size_hint_y: None
            height: self.minimum_height
            canvas.before:
                Color:
                    rgba: (1,1,1,1)
                Rectangle:
                    size: self.size
                    pos: self.pos
<CustLabel@Button>:
    bcolor: (1,1,1,1)
    on_release: app.root.find_row(self)
    background_normal: ''
    background_color: root.bcolor
    canvas.before:
        Color:
            rgba: root.bcolor
        Rectangle:
            size: self.size
            pos: self.pos
''')
class ProdTable(BoxLayout):
    def __init__(self,table='', **kwargs):
        super().__init__(**kwargs)

        prods = table

        col_titles = [k for k in prods.keys()]
        rows_len = len(prods[col_titles[0]])
        self.columns0 =len(col_titles)

        print(rows_len)
        table_data = []
        for r in range(rows_len):
            for t in col_titles:
                table_data.append({'text':str(prods[t][r]),'size_hint_y':None,'height':20,'bcolor':(.01,.4,.47,1)})

        self.columns1 =len(col_titles)
        table_ttls = []
        for t in col_titles:
            table_ttls.append({'text':str(t),'size_hint_y':None,'height':27,'bcolor':(.01,.27,.33,1)})#'text':str(t),'size_hint_y':None,'height':30,'bcolor':(.06,.45,.45,1)})
       [r]),'size_hint_y':None,'height':20,'bcolor':(.06,.25,.25,1)})

        self.ids.table_floor_layout.cols = self.columns0
        self.ids.table_floor.data = table_data
        self.ids.table_floor_t.data = table_ttls
        self.ids.table_floor_layout_t.cols = self.columns1

admin.py

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.lang import Builder
from collections import OrderedDict
from pymongo import MongoClient
import pymongo
from utils.prodtable import ProdTable


#Builder.load_file('admin/admin.kv')


class AdminWindow(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        client = MongoClient()
        db = client.bonanzapos
        self.prods = db.stocks
        prod_scrn = self.ids.scrn_prods
        prods = self.get_prods()
        prodstable = ProdTable(table=prods)
        prod_scrn.add_widget(prodstable)

def get_prods(self):
        client = MongoClient()
        db = client.bonanzapos
        prods = db.stocks
        _stocks = OrderedDict()
        _stocks['Nombre'] = {}
        _stocks['Codigo'] = {}
        _stocks['Precio'] = {}
        _stocks['Stock'] = {}
        Codigo = []
        Nombre = []
        Precio = []
        Stock = []
        for prod in prods.find():
            try:
                Codigo.append(prod['Codigo'])
            except KeyError:
                Codigo.append('')
            nom = prod['Nombre']
            if len(nom)>30:
                nom=nom[:30]+'...'
            Nombre.append(nom)
            try:
                Precio.append(prod['Precio'])
            except KeyError:
                Precio.append('')
            try:
                sto = str(round(float(prod['Stock']),2))
                Stock.append(sto)
            except KeyError:
                Stock.append('')

        prods_lenght = len(Codigo)
        idx = 0
        while idx < prods_lenght:
            _stocks['Codigo'][idx] = Codigo[idx]
            _stocks['Nombre'][idx] = Nombre[idx]
            _stocks['Precio'][idx] = Precio[idx]
            _stocks['Stock'][idx] = Stock[idx]
            idx += 1
        return _stocks

    def change_screen(self, instance):
        if instance.text == 'Productos':
            self.ids.scrn_mngr.current = 'scrn_prod'
class AdminApp(App):
    def build(self):
        return AdminWindow()

if __name__=='__main__':
    AdminApp().run()

admin.kv

<FlatButton@ButtonBehavior+Label>:
    text: 'default'
<AdminWindow>:
    refresh: app_stop.__self__
    id: main_win
    orientation: 'vertical'
    BoxLayout:
        id: cont_nav
        canvas.before:
            Color:
                rgba: (1,1,1,1)
            Rectangle:
                size: self.size
                pos: self.pos
        BoxLayout:
            id: nav_tabs
            size_hint_x: .1
            orientation: 'vertical'
            spacing: 2
            canvas.before:
                Color:
                    rgba: (.02,.3,.4,1)
                Rectangle:
                    size: self.size
                    pos: self.pos
            ToggleButton:
                id: prod_toggle
                text: 'Productos'
                size_hint_y:.1
                background_color: (.01,.4,.5,1)
                background_normal: ''
                group: 'admin_navs'
                on_release: prod_toggle.state='down'
                on_state: root.change_screen(self)
        BoxLayout:
            id: cont
            size_hint_x: .8
            padding: (5,5)
            canvas.before:
                Color:
                    rgba: (1,1,1,1)
                Rectangle:
                    size: self.size
                    pos: self.pos
            ScreenManager:
                id: scrn_mngr
                canvas.before:
                    Color:
                        rgba: (1,1,1,1)
                    Rectangle:
                        size: self.size
                        pos: self.pos
                Screen:
                    id: scrn_prod
                    name: 'scrn_prod'
                    canvas.before:
                        Color:
                            rgba: (1,1,1,1)
                        Rectangle:
                            size: self.size
                            pos: self.pos
                    BoxLayout:
                        orientation: 'vertical'
                        spacing: 10
                        canvas.before:
                            Color:
                                rgba: (1,1,1,1)
                            Rectangle:
                                size: self.size
                                pos: self.pos
                        BoxLayout:
                            id: scrn_prods

The mongodb file.

stocks.bson:

{ "_id" : ObjectId("5d6069798afdc6aa1ee06083"), "Tipo" : "Textil", "Codigo" : "967954A4B8", "Nombre" : "Rayon", "Precio" : 94, "Stock" : 81.90999999999998, "Vendido" : 0, "Ultima_venta" : "", "Ingresado" : "10-08-2019", "Especificaciones" : "Esto es tela", "ultima_modif" : ISODate("2019-08-23T16:32:25.701Z"), "agregado" : ISODate("2019-08-23T16:32:25.701Z") }
{ "_id" : ObjectId("5d60814cb1d2ec9c023b78b5"), "Tipo" : "Textil", "Codigo" : "1D0511AECE", "Nombre" : "Tela", "Precio" : 89, "Stock" : 725.620000000006, "Vendido" : 0, "Ultima_venta" : "", "Ingresado" : "10-08-2019", "Especificaciones" : "Todo sale porque si", "ultima_modif" : ISODate("2019-08-23T18:14:04.007Z"), "agregado" : ISODate("2019-08-23T18:14:04.007Z") }
{ "_id" : ObjectId("5d6489e900da617879e3abfe"), "Tipo" : "Textil", "Codigo" : "7C9C9A1EAB", "Nombre" : "Textil", "Precio" : 12, "Precio Fijo" : 12, "Stock" : 87.74, "Vendido" : 0, "Ultima_venta" : "", "Ingresado" : "10-10.2018", "Especificaciones" : "Muchas", "ultima_modif" : ISODate("2019-08-26T19:39:53.311Z"), "agregado" : ISODate("2019-08-26T19:39:53.311Z") }
  • Hi Edgar welcome to Stackoverflow. Can you please reduce your app to a minimum working example which only shows the issue where you are having trouble. This reduces the time I need for looking at your problem a lot. Thank you. – PalimPalim Nov 7 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.