How can display data from the database to kivy table - python

I want to display a table in one of the screen and the displayed data in the table should come from the loan database, loan table.
I have created the database and also there is no error which is being shown. The table is created but there is no data displayed on it.Its empty table.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
import json
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.properties import BooleanProperty, ListProperty, StringProperty, ObjectProperty
from kivy.uix.recyclegridlayout import RecycleGridLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.popup import Popup
from loandatabase import Database
database = Database()
Builder.load_file('design.kv')
class MainScreen(Screen):
def goto_admin_page(self):
self.manager.current = "adminfirst_screen"
def goto_agent_page(self):
pass
def goto_customer_page(self):
pass
class AdminScreenFirst(Screen):
def go_to_adminsecond(self,uname,pword):
with open("users.json") as file:
users =json.load(file)
if uname in users and users[uname]['password'] == pword:
self.manager.current = "adminsecond_screen"
else:
self.ids.login_wrong.text = "Invalid Credentials.Please Contact the administrator"
class AdminScreenSecond(Screen):
def go_to_adminpending(self):
self.manager.current = "adminPending_screen"
def go_to_adminapproved(self):
self.manager.current = "adminsecond_screen"
def go_to_adminrejected(self):
self.manager.current = "adminsecond_screen"
class AdminPendingScreen(Screen):
def display(self):
self.manager.current = "rv_screen"
class RV(Screen):
data_items = ListProperty([])
def __init__(self, **kwargs):
super(RV, self).__init__(**kwargs)
for row in database.view():
for col in row:
self.data_items.append(col)
class TextInputPopup(Popup):
obj = ObjectProperty(None)
obj_text = StringProperty("")
def __init__(self, obj, **kwargs):
super(TextInputPopup, self).__init__(**kwargs)
self.obj = obj
self.obj_text = obj.text
class SelectableRecycleGridLayout(FocusBehavior, LayoutSelectionBehavior,
RecycleGridLayout):
''' Adds selection and focus behaviour to the view. '''
class SelectableButton(RecycleDataViewBehavior, Button):
''' Add selection support to the Label '''
index = None
selected = BooleanProperty(False)
selectable = BooleanProperty(True)
def refresh_view_attrs(self, rv, index, data):
''' Catch and handle the view changes '''
self.index = index
return super(SelectableButton, self).refresh_view_attrs(
rv, index, data)
def on_touch_down(self, touch):
''' Add selection on touch down '''
if super(SelectableButton, self).on_touch_down(touch):
return True
if self.collide_point(*touch.pos) and self.selectable:
return self.parent.select_with_touch(self.index, touch)
def apply_selection(self, rv, index, is_selected):
''' Respond to the selection of items in the view. '''
self.selected = is_selected
def on_press(self):
popup = TextInputPopup(self)
popup.open()
def update_changes(self, txt):
self.text = txt
class RootWidget(ScreenManager):
pass
class MainApp(App):
def build(self):
return RootWidget()
if __name__ == "__main__":
MainApp().run()
my kivy code :
<MainScreen>:
GridLayout:
cols:1
GridLayout:
cols:1
padding:15,15
spacing:20,20
Label:
text:"Login"
font_size:"20sp"
Button:
text:"Admin"
on_press : root.goto_admin_page()
Button:
text:"Agent"
on_press : root.goto_agent_page()
Button:
text: "Customer"
on_press:root.goto_customer_page()
<AdminScreenFirst>:
GridLayout:
cols:1
padding:15,15
spacing:20,20
Label:
text:"Admin Login"
font_size:"20sp"
TextInput:
id:username
hint_text:"Username"
TextInput:
id:password
password:True
hint_text:"Password"
RelativeLayout:
Button:
text:"Login"
on_press : root.go_to_adminsecond(root.ids.username.text,root.ids.password.text)
size_hint :0.3,0.5
pos_hint: {'center_x' : 0.5 , 'center_y' : 0.6}
Label:
id:login_wrong
text:""
<AdminScreenSecond>:
GridLayout:
cols:1
padding:15,15
spacing:20,20
Label:
text:"Loan"
font_size:"20sp"
Button:
text:"Pending Request"
on_press:root.go_to_adminpending()
Button:
text:"Approved"
on_press: root.go_to_adminapproved()
Button:
text:"Rejected"
on_press: root.go_to_adminrejected()
<AdminPendingScreen>:
GridLayout:
cols:1
padding:15,15
spacing:20,20
Label:
text:"Loans"
Button:
text:"Display"
on_press: root.display()
Label:
id:display
text:""
<TextInputPopup>:
title: "Popup"
size_hint: None, None
size: 400, 400
auto_dismiss: False
BoxLayout:
orientation: "vertical"
TextInput:
id: txtinput
text: root.obj_text
Button:
size_hint: 1, 0.2
text: "Save Changes"
on_release:
root.obj.update_changes(txtinput.text)
root.dismiss()
Button:
size_hint: 1, 0.2
text: "Cancel Changes"
on_release: root.dismiss()
<RV>:
BoxLayout:
orientation: "vertical"
GridLayout:
size_hint: 1, None
size_hint_y: None
height: 25
cols: 11
Label:
text: "LoanID"
Label:
text: "Name"
Label:
text:"Tenure"
Label:
text:"Balance"
Label:
text:"LoanType"
Label:
text:"InterestType"
Label:
text:"Interest % p.a."
Label:
text:"Security"
Label:
text:"Total"
Label:
text:"EMI"
Label:
text:"Request"
BoxLayout:
RecycleView:
viewclass: 'SelectableButton'
data: [{'text': str(x)} for x in root.data_items]
SelectableRecycleGridLayout:
cols: 11
default_size: None, dp(26)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
#orientation: 'vertical'
multiselect: True
touch_multiselect: True
<RootWidget>:
MainScreen:
name:"mainfirst_screen"
AdminScreenFirst:
name:"adminfirst_screen"
AdminScreenSecond:
name:"adminsecond_screen"
AdminPendingScreen:
name:"adminPending_screen"
RV:
name:"rv_screen"
my database code:
import sqlite3
class Database:
def __init__(self):
self.conn = sqlite3.connect("loandatabase.db")
self.cur = self.conn.cursor()
self.cur.execute("CREATE TABLE IF NOT EXISTS loan (id INTEGER PRIMARY KEY, customer_name TEXT, tenure integer, balance integer, loantype TEXT, interesttype TEXT,interest integer,security TEXT,totalpayment integer,emi integer,instruction TEXT)")
self.conn.commit()
def insert(self,cname,tenure,blance,ltype,itype,interest,security,tpay,emi,instr):
self.cur.execute("INSERT INTO loan VALUES (NULL,?,?,?,?,?,?,?,?,?,?)",(cname,tenure,blance,ltype,itype,interest,security,tpay,emi,instr))
self.conn.commit()
def view(self):
self.cur.execute("SELECT * FROM loan")
rows = self.cur.fetchall()
return rows
def __del__(self):
self.conn.close()
[![enter image description here][1]][1]
I would request you to help me with it. Thank You in Advance
I am using Kivy,Python,sqlite3
This is displayed:
[1]: https://i.stack.imgur.com/fkUvS.png

Try this:
class MainApp(App):
title = 'Title of the app'
def build(self):
sm = ScreenManager('''You can add transition here''')
sm.add_widget(MainScreen(name='main'))
sm.add_widget(AdminScreenFirst(name='admin1'))
sm.add_widget(AdminScreenSecond(name='admin2'))
sm.add_widget(AdminPendingScreen(name='pending'))
sm.add_widget(RV(name='rv'))
return sm

Related

How to refresh GridLayout in Kivy with kv file

I need help.
I created a small mobile application with Kivy.
I have two screens: ScreenList and ScreenDetail.
However the screen(ScreenList) containing GridLayout does not refresh
ScreenList: contains a list of items
ScreenDetail: Contains the details of a single item.
How the app works:
When I click on the first item on button 1
I go to the details of the item.
I modify the second field. I replace the text: Firt element for First and update data
After recording, I redirect the application to the screens which contain (ScreenList) the list of elements.
But the list of elements remains unchanged then the data has been modified in the database.
6.And when I return to the screen (ScreenDetail) which contains the details, there I see that the data is updated.
How can I refresh the item list in ScreenList?
Here are the pictures as an example
List before update
before update
after update
List after update
Here is the python code:
import kivy
from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.properties import ObjectProperty, StringProperty
from kivy.lang import Builder
from kivymd.uix.picker import MDTimePicker
from kivymd.uix.picker import MDDatePicker
from kivymd.app import MDApp
import sqlite3
import os.path
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
db_path = os.path.join(BASE_DIR, "donnee/ProjetMaison.db")
def donnee_dic(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
cur_id = None
class ScreenList(Screen):
data_grid = ObjectProperty()
def go_to_detail(self, instance):
global cur_id
cur_id = int(instance.text)
self.manager.current = 'screen_detail'
def __init__(self, **kwargs):
super(ScreenList, self).__init__(**kwargs)
con_list_course = sqlite3.connect(db_path)
con_list_course.row_factory = donnee_dic
curss_list_course = con_list_course.cursor()
data_list_course = curss_list_course.execute("select * FROM courses")
self.data_grid.bind(minimum_height=self.data_grid.setter('height'))
for row in data_list_course:
template_screen = GridLayout(cols=2, size_hint_y=None, height=40)
template_screen.add_widget(Label(text=row['nom']))
template_screen.add_widget(Button(text=str(row['id']), on_press=self.go_to_detail))
self.data_grid.add_widget(template_screen)
con_list_course.close()
def Maj_colonne(id, nom):
try:
sqliteConnection = sqlite3.connect(db_path)
cursor = sqliteConnection.cursor()
sqlite_update_query = """Update courses set nom = ? where id = ?"""
columnValues = (nom, id)
cursor.execute(sqlite_update_query, columnValues)
sqliteConnection.commit()
sqliteConnection.commit()
cursor.close()
except sqlite3.Error as error:
print("Erreur de connexion", error)
finally:
if sqliteConnection:
sqliteConnection.close()
class ScreenDetail(Screen):
label_id = ObjectProperty()
label_nom = ObjectProperty()
def __init__(self, **kwargs):
super(ScreenDetail, self).__init__(**kwargs)
def on_enter(self):
global cur_id
conn = sqlite3.connect(db_path)
cursor = conn.execute("select * FROM courses where id=?",str(cur_id))
for row in cursor:
self.label_id.text = str(row[0])
self.label_nom.text = str(row[1])
cursor.close()
def update_course(self):
id_pk = self.ids['label_id'].text
nom = self.ids['label_nom'].text
if id_pk:
Maj_colonne(str(id_pk), str(nom))
self.ids['label_id'].text = ''
self.ids['label_nom'].text = ''
self.manager.current = 'screen_list'
class Listapp(MDApp):
def build(self):
screenmanager = ScreenManager()
screenmanager.add_widget(ScreenList(name='screen_list'))
screenmanager.add_widget(ScreenDetail(name='screen_detail'))
return screenmanager
if __name__ == '__main__':
Listapp().run()
Here is the kv code:
#:import utils kivy.utils
<ScreenList>:
data_grid: data_grid
MDBoxLayout:
orientation: 'vertical'
md_bg_color: app.theme_cls.primary_color
radius: [25, 0, 0, 0]
ScrollView:
MDGridLayout:
id: data_grid
cols: 1
spacing:10
size_hint_y:None
<ScreenDetail>:
label_id: label_id
label_nom: label_nom
MDBoxLayout:
orientation: 'vertical'
md_bg_color: app.theme_cls.primary_color
ScrollView:
GridLayout:
id: detail_grid
cols:2
Label:
text: 'Numéro:'
bold: True
TextInput:
id: label_id
text: ''
Label:
text: 'Nom:'
bold: True
TextInput:
id: label_nom
text: ''
Button:
text: 'OK'
size_hint_y: None
on_press: root.update_course()
Thank you
Few notes to take, in general, when working with Kivy
When you're trying to share data in between screens, it's often useful to use app methods instead of specific methods of screens.
And when you need to create lots of buttons, maybe inside a loop, and bind methods on its events( on_press, on_release), it's often bad to create button instances on the fly and bind methods on its events because you'll need to do extra work to make sure that those bound methods are called with right parameters when events are fired. Rather create a custom class template and use that instead.
Working solution for your problem (only showing sections that has been added/updated
Created custom GridLayout:
class MyGrid(GridLayout):
pass
Updated __init__ method inside ScreenList:
def __init__(self, **kwargs):
#...
app = MDApp.get_running_app()
for row in data_list_course:
template_screen = MyGrid()
template_screen.ids.lbl.text = row['nom']
template_screen.ids.btn.text = str(row['id'])
self.data_grid.add_widget(template_screen)
Added methods inside app class:
class Listapp(MDApp):
def build(self):
self.screenmanager = ScreenManager()
self.screenmanager.add_widget(ScreenList(name='screen_list'))
self.screenmanager.add_widget(ScreenDetail(name='screen_detail'))
return self.screenmanager
def go_to_detail_(self, inst):
self.inst = inst
self.screenmanager.current = 'screen_detail'
def update_course_(self, label_id, label_nom):
c = self.inst.children[::-1]
c[0].text = label_nom.text
c[1].text = label_id.text
print(label_id.text, label_nom.text)
if label_id.text:
Maj_colonne(str(id_pk), str(nom))
label_nom.text = ''
label_id.text = ''
self.screenmanager.current = 'screen_list'
Here's the updated kv code:
#:import utils kivy.utils
<ScreenList>:
data_grid: data_grid
MDBoxLayout:
orientation: 'vertical'
md_bg_color: app.theme_cls.primary_color
radius: [25, 0, 0, 0]
ScrollView:
MDGridLayout:
id: data_grid
cols: 1
spacing:10
size_hint_y:None
<ScreenDetail>:
label_id: label_id
label_nom: label_nom
MDBoxLayout:
orientation: 'vertical'
md_bg_color: app.theme_cls.primary_color
ScrollView:
GridLayout:
id: detail_grid
cols:2
Label:
text: 'Numéro:'
bold: True
TextInput:
id: label_id
text: ''
Label:
text: 'Nom:'
bold: True
TextInput:
id: label_nom
text: ''
Button:
text: 'OK'
size_hint_y: None
on_press: app.update_course_(label_id, label_nom)
<MyGrid>:
cols:2
size_hint_y:None
height:40
Label:
id: lbl
text: ''
Button:
id: btn
text: ''
on_press:
app.go_to_detail_(root)

Changing widget from another screen in Kivy

I'm trying to update one widget in through a button in another view at Kivy.
Kivy code:
<MainScreen>:
relatorios : relatorios
despesas : despesas
GridLayout:
size: root.width, root.height
cols : 1
Button:
id: relatorios
text: 'Relatorios'
on_press: root.change_screen(rep = 'Report')
Button:
id: despesas
text: 'Despesa'
on_press: root.change_screen(rep = 'Expend')
<ReportScreen>:
GridLayout:
size: root.width, root.height
cols : 1
Button:
id: semanal
text: 'Semanal'
Button:
id: mensal
text: 'Mensal'
Button:
id: anual
text: 'Anual'
Button:
id: voltar
text: 'Voltar'
on_press: root.change_screen(rep = 'Main')
<ExpendScreen>:
kind_selec1 : kind_select
GridLayout:
size: root.width, root.height
cols : 2
Label:
id: kind
text: 'Tipo de Despesa'
Button:
id: kind_select
text: 'Selecionar'
on_press: root.change_screen(rep = 'Kind')
Label:
id: way
text: 'Meio de Pagamento'
Button:
id: selec_way
text: 'Selecionar'
on_press: root.change_screen(rep = 'Way')
Label:
id: parcelas
text: 'Quantidade de Parcelas'
TextInput:
id: qtdy
Label:
id: value
text: 'Valor Total'
TextInput:
id: qtdy
Button:
id: back
text: 'Voltar'
on_press: root.change_screen(rep = 'Main')
Button:
id: submit
text: 'Registrar'
on_press: root.change_name()
<DropDown1>:
cols : 1
orientation: 'vertical'
<DropDown2>:
cols : 1
orientation: 'vertical'
Python code:
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from functools import partial
from functions import Expense
from kivy.properties import ObjectProperty
import kivy
import time
class MainScreen(Widget):
relatorios = ObjectProperty(None)
despesas = ObjectProperty(None)
def change_screen(self,rep):
app_finan_con.screen_manager.current = rep
return
class ReportScreen(Widget):
def change_screen(self,rep):
app_finan_con.screen_manager.current = rep
return
class ExpendScreen(Widget):
kind_selec1 = ObjectProperty()
def __init__(self, **kwargs):
super(ExpendScreen, self).__init__(**kwargs)
def change_screen(self,rep):
app_finan_con.screen_manager.current = rep
def change_name(self):
self.kind_selec1.text= 'porra'
class Dropdown1(BoxLayout):
def __init__(self, **kwargs):
super(Dropdown1, self).__init__(**kwargs)
list = ['Alimentação', 'Transporte', 'Entretenimento','Moradia','Doação']
for item in list:
self.but = Button(text=item)
self.add_widget(self.but)
self.but.bind(on_release=partial(self.It,arg = item))
def It(self,instance,arg):
app_finan_con.screen_manager.current = 'Expend'
ExpendScreen.change_name(ExpendScreen)
class Dropdown2(BoxLayout):
def __init__(self, **kwargs):
super(Dropdown2, self).__init__(**kwargs)
list = ['Crédito', 'Débito', 'Dinheiro']
for item in list:
self.but = Button(text=item)
self.add_widget(self.but)
self.but.bind(on_release=partial(self.It, arg=item))
def It(self,instance,arg):
app_finan_con.screen_manager.current = 'Expend'
class MyApp(App):
def build(self):
self.screen_manager = ScreenManager()
self.mainscreen = MainScreen()
screen = Screen(name='Main')
screen.add_widget(self.mainscreen)
self.screen_manager.add_widget(screen)
self.reportscreen = ReportScreen()
screen = Screen(name='Report')
screen.add_widget(self.reportscreen)
self.screen_manager.add_widget(screen)
self.expendscreen = ExpendScreen()
screen = Screen(name='Expend')
screen.add_widget(self.expendscreen)
self.screen_manager.add_widget(screen)
self.dropdown1 = Dropdown1()
screen = Screen(name='Kind')
screen.add_widget(self.dropdown1)
self.screen_manager.add_widget(screen)
self.dropdown2 = Dropdown2()
screen = Screen(name='Way')
screen.add_widget(self.dropdown2)
self.screen_manager.add_widget(screen)
return self.screen_manager
def change_screen():
app_finan_con.screen_manager.current ='Report'
return
if __name__ == '__main__':
app_finan_con = MyApp()
app_finan_con.run()
The idea is to use screen Dropdown1 to modify screen ExpendScreen through this code:
class Dropdown1(BoxLayout):
def __init__(self, **kwargs):
super(Dropdown1, self).__init__(**kwargs)
list = ['Alimentação', 'Transporte', 'Entretenimento','Moradia','Doação']
for item in list:
self.but = Button(text=item)
self.add_widget(self.but)
self.but.bind(on_release=partial(self.It,arg = item)) (this line activates It method)
def It(self,instance,arg):
app_finan_con.screen_manager.current = 'Expend'
ExpendScreen.change_name(ExpendScreen) (this line activates method in ExpendScreen):
class ExpendScreen(Widget):
kind_selec1 = ObjectProperty()
def __init__(self, **kwargs):
super(ExpendScreen, self).__init__(**kwargs)
def change_screen(self,rep):
app_finan_con.screen_manager.current = rep
def change_name(self):
self.kind_selec1.text= 'porra'
but I get the following error: AttributeError: 'kivy.properties.ObjectProperty' object has no attribute 'text'
but when I call the same method from a button in the same screen (ExpendScreen) it works as expected, kivy code:
Button:
id: submit
text: 'Registrar'
on_press: root.change_name()
Image examples:
Can anyone help me?
Your line of code:
ExpendScreen.change_name(ExpendScreen)
is calling an instance method of ExpendScreen without an instance. That won't work. You need to get the instance of ExpendScreen that is part of your gui, and call its change_name method. I haven't tested this, but I think replacing the above line with:
app_finan_con.expendscreen.change_name()
will do what you want.

How to edit SQLite database values using kivy and RecycleView

I'm trying to display an sqlite3 database in python using kivy. Tested out ikolim's solution here and it works as intended in displaying the database contents into the RecycleView's button labels:
And when a button is pressed, a popup and edit function appears as intended:
And it does edit the selected button's text as shown in the next screenshot.. :
..but not the sqlite database values. Would it be possible to reflect the changes the user makes inside the Kivy app to the values inside the sqlite database?
Here are the codes:
test.py
import sqlite3
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.button import Button
from kivy.properties import BooleanProperty, ListProperty, StringProperty, ObjectProperty
from kivy.uix.recyclegridlayout import RecycleGridLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.popup import Popup
connection = sqlite3.connect("demo.db", isolation_level=None)
cursor = connection.cursor()
class TextInputPopup(Popup):
obj = ObjectProperty(None)
obj_text = StringProperty("")
def __init__(self, obj, **kwargs):
super(TextInputPopup, self).__init__(**kwargs)
self.obj = obj
self.obj_text = obj.text
class SelectableRecycleGridLayout(FocusBehavior, LayoutSelectionBehavior,
RecycleGridLayout):
''' Adds selection and focus behaviour to the view. '''
class SelectableButton(RecycleDataViewBehavior, Button):
''' Add selection support to the Button '''
index = None
selected = BooleanProperty(False)
selectable = BooleanProperty(True)
def refresh_view_attrs(self, rv, index, data):
''' Catch and handle the view changes '''
self.index = index
return super(SelectableButton, self).refresh_view_attrs(rv, index, data)
def on_touch_down(self, touch):
''' Add selection on touch down '''
if super(SelectableButton, self).on_touch_down(touch):
return True
if self.collide_point(*touch.pos) and self.selectable:
return self.parent.select_with_touch(self.index, touch)
def apply_selection(self, rv, index, is_selected):
''' Respond to the selection of items in the view. '''
self.selected = is_selected
def on_press(self):
popup = TextInputPopup(self)
popup.open()
def update_changes(self, txt):
self.text = txt
class RV(BoxLayout):
data_items = ListProperty([])
def __init__(self, **kwargs):
super(RV, self).__init__(**kwargs)
self.get_users()
def get_users(self):
cursor.execute("CREATE TABLE IF NOT EXISTS Callbacks(cName TEXT, cID INT, cbTime INT, cbRems TEXT)")
cursor.execute("INSERT INTO Callbacks VALUES ('Client1','1','1500','Test1')")
cursor.execute("INSERT INTO Callbacks VALUES ('Client2','2','1600','Test2')")
cursor.execute("INSERT INTO Callbacks VALUES ('Client3','3','1700','Test3')")
connection.commit()
cursor.execute("SELECT * FROM Callbacks ORDER BY ROWID DESC")
rows = cursor.fetchall()
# create data_items
for row in rows:
for col in row:
self.data_items.append(col)
for row in rows:
print(self.data_items)
class TestApp(App):
title = "Kivy RecycleView & SQLite3 Demo"
def build(self):
return RV()
if __name__ == "__main__":
TestApp().run()
test.kv
#:kivy 1.10.0
<TextInputPopup>:
title: "Popup"
size_hint: None, None
size: 400, 400
auto_dismiss: False
BoxLayout:
orientation: "vertical"
TextInput:
id: txtinput
text: root.obj_text
Button:
size_hint: 1, 0.2
text: "Save Changes"
on_release:
root.obj.update_changes(txtinput.text)
root.dismiss()
Button:
size_hint: 1, 0.2
text: "Cancel Changes"
on_release: root.dismiss()
<SelectableButton>:
# Draw a background to indicate selection
canvas.before:
Color:
rgba: (.0, 0.9, .1, .3) if self.selected else (0, 0, 0, 1)
Rectangle:
pos: self.pos
size: self.size
<RV>:
BoxLayout:
orientation: "vertical"
GridLayout:
size_hint: 1, None
size_hint_y: None
height: 25
cols: 4
Label:
text: "Name"
Label:
text: "ID"
Label:
text: "CB Time"
Label:
text: "Remarks"
BoxLayout:
RecycleView:
viewclass: 'SelectableButton'
data: [{'text': str(x)} for x in root.data_items]
SelectableRecycleGridLayout:
cols: 4
default_size: None, dp(26)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
multiselect: True
touch_multiselect: True
In order to reflect the changes to the sql, you'll need to use the update command to update the sql table.

Update the recycle view list after selection in Kivy

My target is like
I am choosing an option from the recycle view list from button 1, after choosing the option, then I am choosing from button 2,now the display must not show the choosed value in button 1(previously choosed option must be removed from recycle view 2). More over the button text must be updated to the choosed value text.
from kivy.app import App
from kivy.lang import Builder
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.screenmanager import ScreenManager,Screen,SlideTransition,FadeTransition
from kivy.uix.checkbox import CheckBox
from kivy.properties import ObjectProperty, NumericProperty, BooleanProperty,ListProperty,StringProperty
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.base import runTouchApp
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.recycleview.views import RecycleDataViewBehavior
alph = ['a','b','c','d','e','f','g','h','i']
val1_chsd = None
val2_chsd = None
class Screen3(Screen):
#pass
labeltext = StringProperty('My selection')
def buttontype(self,types):
global buttonval
print("type called :",types)
if types is "button1":
buttonval = "selection1"
print ('value set 1',buttonval)
elif types is "button2":
buttonval = "selection2"
print ('value set 2',buttonval)
def setvalueselected(self,sel_value):
print("Selected value is",sel_value)
global val1_chsd
global val2_chsd
if buttonval is "selection1":
val1_chsd = sel_value
val1_name = sel_value
print("choosed no. 1",val1_name)
if buttonval is "selection2":
val2_chsd = sel_value
val2_name = sel_value
print("choosed no. 2",val2_name)
def printselected(self):
print("abcdef",val1_chsd,val2_chsd)
if val1_chsd != None and val2_chsd != None:
selected = val1_chsd + '\n' + val2_chsd
print ("choosed : ",selected)
self.labeltext = selected
else:
print ("Choose all values")
class Screen4(Screen):
list = ListProperty([])
class Screen5(Screen):
list = ListProperty([])
class ScreenManagement(ScreenManager):
pass
class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior,
RecycleBoxLayout):
''' Adds selection and focus behaviour to the view. '''
class RVval1(RecycleView):
def __init__(self, **kwargs):
super(RVval1, self).__init__(**kwargs)
#print('removedval:', value2_chsd)
self.data = [{'text': str(x)} for x in alph]
class RVval2(RecycleView):
def __init__(self, **kwargs):
super(RVval2, self).__init__(**kwargs)
print('removedval:', val1_chsd)
self.data = [{'text': str(x)} for x in alph]
class SelectableLabel(RecycleDataViewBehavior, Label):
''' Add selection support to the Label '''
index = None
selected = BooleanProperty(False)
selectable = BooleanProperty(True)
sc3 = Screen3()
def refresh_view_attrs(self, rv, index, data):
''' Catch and handle the view changes '''
self.index = index
return super(SelectableLabel, self).refresh_view_attrs(
rv, index, data)
def on_touch_down(self, touch):
''' Add selection on touch down '''
if super(SelectableLabel, self).on_touch_down(touch):
return Truej
if self.collide_point(*touch.pos) and self.selectable:
return self.parent.select_with_touch(self.index, touch)
def apply_selection(self, rv, index, is_selected):
''' Respond to the selection of items in the view. '''
self.selected = is_selected
if is_selected:
print("selection changed to {0}".format(rv.data[index]))
valueselected = format(rv.data[index])
valueselected = valueselected.replace("{'text': '","")
valueselected = valueselected.replace("'}","")
print("valueselected : ",valueselected)
self.sc3.setvalueselected(valueselected)
else:
print("selection removed for {0}".format(rv.data[index]))
presentation = Builder.load_file('dfive2.kv')
class DfiveZApp(App):
def build(self):
return presentation
if __name__ == '__main__':
DfiveZApp().run()
Kivy file
#:import SlideTransition kivy.uix.screenmanager.SlideTransition
ScreenManagement:
id:screenmgr
Screen3:
id: screen_3
name : "screen3"
manager: screenmgr
Screen4:
id: rv_screen_val1
name : "screen4"
Screen5:
id: rv_screen_val2
name : "screen5"
<Screen3>:
BoxLayout:
orientation: "vertical"
BoxLayout:
orientation: "vertical"
#size_hint_y: 1
GridLayout:
cols: 2
size_hint_y: 2
Label:
text:"Value1"
Button:
id: val1_name
text: 'val1'
on_press:
root.buttontype('button1')
app.root.transition = SlideTransition(direction='left')
app.root.current = 'screen4'
Label:
text:"Value2"
Button:
id: val2_name
text: 'val2'
on_press:
root.buttontype('button2')
app.root.transition = SlideTransition(direction='left')
app.root.current = 'screen5'
BoxLayout:
Label:
text:root.labeltext
Button:
text: 'Submit'
on_press:
root.printselected()
#app.root.transition = SlideTransition(direction='left')
<SelectableLabel>:
# Draw a background to indicate selection
canvas.before:
Color:
rgba: (.0, 0.9, .1, .3) if self.selected else (0, 0, 0, 1)
Rectangle:
pos: self.pos
size: self.size
<RVval1>:
viewclass: 'SelectableLabel'
SelectableRecycleBoxLayout:
default_size: None, dp(56)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
multiselect: False
touch_multiselect: False
<RVval2>:
viewclass: 'SelectableLabel'
SelectableRecycleBoxLayout:
default_size: None, dp(56)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
multiselect: False
touch_multiselect: False
<Screen4>:
BoxLayout:
list: rv_list_val1
orientation: "vertical"
RVval1:
id: rv_list_val1
Button:
text: 'Previous screen'
size_hint: None, None
size: 150, 50
#on_press: root.SetText()
on_release:
#root.manager.current = root.manager.previous()
app.root.transition = SlideTransition(direction='right')
app.root.current = 'screen3'
<Screen5>:
BoxLayout:
list: rv_list_val2
orientation: "vertical"
RVval2:
id: rv_list_val2
Button:
text: 'Previous screen'
size_hint: None, None
size: 150, 50
#on_press: root.SetText()
on_release:
#root.manager.current = root.manager.previous()
app.root.transition = SlideTransition(direction='right')
app.root.current = 'screen3'
You can create a method to reload the data on the second step and set a string property on the view.
.py file
from kivy.app import App
from kivy.lang import Builder
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.screenmanager import ScreenManager,Screen,SlideTransition,FadeTransition
from kivy.uix.checkbox import CheckBox
from kivy.properties import ObjectProperty, NumericProperty, BooleanProperty,ListProperty,StringProperty
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.base import runTouchApp
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.recycleview.views import RecycleDataViewBehavior
alph = ['a','b','c','d','e','f','g','h','i']
sel_values1 = []
val1_chsd = None
val2_chsd = None
class Screen3(Screen):
#pass
labeltext = StringProperty('My selection')
val1 = StringProperty('val1')
val2 = StringProperty('val2')
def buttontype(self,types):
global buttonval
print("type called :",types)
if types is "button1":
buttonval = "selection1"
print ('value set 1',buttonval)
elif types is "button2":
buttonval = "selection2"
print ('value set 2',buttonval)
def setvalueselected(self,sel_value):
print("Selected value is",sel_value)
global val1_chsd
global val2_chsd
if buttonval is "selection1":
val1_chsd = sel_value
val1_name = sel_value
print("choosed no. 1",val1_name)
self.val1 = val1_name
if buttonval is "selection2":
val2_chsd = sel_value
val2_name = sel_value
print("choosed no. 2",val2_name)
self.val2 = val2_name
def printselected(self):
print("abcdef",val1_chsd,val2_chsd)
if val1_chsd != None and val2_chsd != None:
selected = val1_chsd + '\n' + val2_chsd
print ("choosed : ",selected)
self.labeltext = selected
else:
print ("Choose all values")
class Screen4(Screen):
list = ListProperty([])
class Screen5(Screen):
list = ListProperty([])
class ScreenManagement(ScreenManager):
pass
class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior,
RecycleBoxLayout):
''' Adds selection and focus behaviour to the view. '''
class RVval1(RecycleView):
def __init__(self, **kwargs):
super(RVval1, self).__init__(**kwargs)
#print('removedval:', value2_chsd)
self.data = [{'text': str(x)} for x in alph]
class RVval2(RecycleView):
def __init__(self, **kwargs):
super(RVval2, self).__init__(**kwargs)
print('removedval:', val1_chsd)
self.data = [{'text': str(x)} for x in alph]
def reload_data(self):
self.data = [{'text': str(x)} for x in alph if x not in sel_values1]
class SelectableLabel(RecycleDataViewBehavior, Label):
''' Add selection support to the Label '''
index = None
selected = BooleanProperty(False)
selectable = BooleanProperty(True)
sc3 = Screen3()
def refresh_view_attrs(self, rv, index, data):
''' Catch and handle the view changes '''
self.index = index
return super(SelectableLabel, self).refresh_view_attrs(
rv, index, data)
def on_touch_down(self, touch):
''' Add selection on touch down '''
if super(SelectableLabel, self).on_touch_down(touch):
return Truej
if self.collide_point(*touch.pos) and self.selectable:
return self.parent.select_with_touch(self.index, touch)
def apply_selection(self, rv, index, is_selected):
''' Respond to the selection of items in the view. '''
self.selected = is_selected
if is_selected:
print("selection changed to {0}".format(rv.data[index]))
valueselected = format(rv.data[index])
valueselected = valueselected.replace("{'text': '","")
valueselected = valueselected.replace("'}","")
print("valueselected : ",valueselected)
print(hasattr(rv, "RVval1"))
if (rv.name == "RVval1"):
sel_values1.append(valueselected)
app = App.get_running_app()
app.root.ids['screen_3'].setvalueselected(valueselected)
app.root.ids['rv_screen_val2'].ids['rv_list_val2'].reload_data()
else:
print("selection removed for {0}".format(rv.data[index-1]))
presentation = Builder.load_file('dfive2.kv')
class DfiveZApp(App):
def build(self):
return presentation
if __name__ == '__main__':
DfiveZApp().run()
.kv file
#:import SlideTransition kivy.uix.screenmanager.SlideTransition
ScreenManagement:
id:screenmgr
Screen3:
id: screen_3
name : "screen3"
manager: screenmgr
Screen4:
id: rv_screen_val1
name : "screen4"
Screen5:
id: rv_screen_val2
name : "screen5"
<Screen3>:
BoxLayout:
orientation: "vertical"
BoxLayout:
orientation: "vertical"
#size_hint_y: 1
GridLayout:
cols: 2
size_hint_y: 2
Label:
text:"Value1"
Button:
id: val1_name
text: root.val1
on_press:
root.buttontype('button1')
app.root.transition = SlideTransition(direction='left')
app.root.current = 'screen4'
Label:
text:"Value2"
Button:
id: val2_name
text: root.val2
on_press:
root.buttontype('button2')
app.root.transition = SlideTransition(direction='left')
app.root.current = 'screen5'
BoxLayout:
Label:
text:root.labeltext
Button:
text: 'Submit'
on_press:
root.printselected()
#app.root.transition = SlideTransition(direction='left')
<SelectableLabel>:
# Draw a background to indicate selection
canvas.before:
Color:
rgba: (.0, 0.9, .1, .3) if self.selected else (0, 0, 0, 1)
Rectangle:
pos: self.pos
size: self.size
<RVval1>:
viewclass: 'SelectableLabel'
SelectableRecycleBoxLayout:
default_size: None, dp(56)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
multiselect: False
touch_multiselect: False
<RVval2>:
viewclass: 'SelectableLabel'
SelectableRecycleBoxLayout:
default_size: None, dp(56)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
multiselect: False
touch_multiselect: False
<Screen4>:
BoxLayout:
list: rv_list_val1
orientation: "vertical"
RVval1:
id: rv_list_val1
name: 'RVval1'
Button:
text: 'Previous screen'
size_hint: None, None
size: 150, 50
#on_press: root.SetText()
on_release:
#root.manager.current = root.manager.previous()
app.root.transition = SlideTransition(direction='right')
app.root.current = 'screen3'
<Screen5>:
BoxLayout:
list: rv_list_val2
orientation: "vertical"
RVval2:
id: rv_list_val2
name: 'RVval2'
Button:
text: 'Previous screen'
size_hint: None, None
size: 150, 50
#on_press: root.SetText()
on_release:
#root.manager.current = root.manager.previous()
app.root.transition = SlideTransition(direction='right')
app.root.current = 'screen3'

Kivy, RecycleView Add and Delete rows

I'm trying to better understand how the RecycleView functions. Seems like only practical examples will teach me. Docs aren't helping. Kindly have a look at my current screen in the pic below.
Now following are what I'm trying to achieve.
Add/Remove new rows to/from the list.
The first column sl/no should maintain the order even after deleting from in between.
How to achieve 'sort' function? Is it by taking the data into python and do the sort and then update that to the RV again ?
Please find the both .py and .kv codes below.
rv_main.py
import os
os.environ['KIVY_GL_BACKEND'] = 'gl'
import kivy
kivy.require('1.11.0')
from kivy.uix.boxlayout import BoxLayout
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.recyclegridlayout import RecycleGridLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.recyclegridlayout import RecycleGridLayout
from kivy.uix.label import Label
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.properties import ObjectProperty
from kivy.properties import ListProperty, BooleanProperty
from kivy.properties import NumericProperty
class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior, RecycleBoxLayout):
''' Adds selection and focus behaviour to the view. '''
#-----------------------------------------------------------------------
class RecycleViewRow(RecycleDataViewBehavior, BoxLayout):
''' Add selection support to the Label '''
index = None
selected = BooleanProperty(False)
selectable = BooleanProperty(True)
slno = StringProperty('')
typ = StringProperty('')
def refresh_view_attrs(self, rv, index, data):
''' Catch and handle the view changes '''
self.index = index
return super(RecycleViewRow, self).refresh_view_attrs(
rv, index, data)
def on_touch_down(self, touch):
''' Add selection on touch down '''
if super(RecycleViewRow, self).on_touch_down(touch):
return True
if self.collide_point(*touch.pos) and self.selectable:
return self.parent.select_with_touch(self.index, touch)
def apply_selection(self, rv, index, is_selected):
''' Respond to the selection of items in the view. '''
self.selected = is_selected
if is_selected:
pass
else:
pass
def delete_row(self,rv, index, is_selected):
if is_selected:
rv.data.pop(index)
rv.layout_manager.clear_selection()
#-----------------------------------------------------------------------
class RV(RecycleView):
def __init__(self, **kwargs):
super(RV, self).__init__(**kwargs)
#fetch data from the database
self.data = [{'slno': str(x+1),'typ': 'default'} for x in range(4)]
#-----------------------------------------------------------------------
class DataTable(BoxLayout):
def addRow(self):
pass
def removeRow(self):
pass
#-----------------------------------------------------------------------
class RvMainApp(App):
def build(self):
return DataTable()
if __name__ == '__main__':
RvMainApp().run()
rvmain.kv
#: kivy 1.11.0
<RecycleViewRow>:
id: rv
slno: ""
typ: ""
canvas.before:
Color:
rgba: (.0, 0.9, .1, .3) if self.selected else (0.4, 0.4, 0.4, 1)
Rectangle:
pos: self.pos
size: self.size
orientation: 'horizontal'
size_hint: 1.0, 1.0
Label:
text: root.slno
size_hint_x : 1.0
Label:
text: root.typ
size_hint_x : 1.0
#----------------------------------------------------------------
<RV>:
id : rv
viewclass: 'RecycleViewRow'
SelectableRecycleBoxLayout:
default_size: None, dp(40)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
#----------------------------------------------------------------
<DataTable>:
orientation : 'vertical'
Button:
BoxLayout:
Button:
RV:
Button:
BoxLayout:
Button:
text: "Add"
on_release: rv.data.append({"slno": "?", "typ": 'default'})
Button:
text: "Remove"
on_release:
You edit the data list of the recycleview. The data will be sorted as it is sorted in that list.
So here is an example of the add remove feature:
from kivy.app import App
from kivy.lang import Builder
KV = '''
<Row#BoxLayout>:
ind: 1
Button:
text: str(root.ind)
Button:
text: "default"
BoxLayout:
ind: 1
orientation: "vertical"
Button:
BoxLayout:
Button:
RecycleView:
id: rv
data: [{"text":"first","ind":1}]
viewclass: 'Row'
RecycleBoxLayout:
default_size_hint: 1, None
default_size: None, dp(56)
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
Button
BoxLayout:
Button:
text: "Add"
on_release:
root.ind += 1
rv.data.append({"ind": root.ind})
Button:
text: "Remove"
on_release:
root.ind = root.ind - 1 if root.ind > 0 else 0
if len(rv.data): rv.data.pop(-1)
'''
class Test(App):
def build(self):
self.root = Builder.load_string(KV)
return self.root
Test().run()
And here is an example on how to sort the data list by some key. In this case ind.
from kivy.app import App
from kivy.lang import Builder
KV = '''
RecycleView:
viewclass: 'Label'
data: sorted([{"text":"!", "ind":3},{"text":"world", "ind":2},{"text":"hello", "ind":1}], key=lambda k: k["ind"])
RecycleBoxLayout:
id: layout
default_size: None, dp(56)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
'''
class TestApp(App):
def build(self):
return Builder.load_string(KV)
if __name__ == '__main__':
TestApp().run()

Categories