Animesh Singh
Animesh Singh

Reputation: 9

How to use datatables in kivy along/in .kv file in KivyMD?

I am working on a password manager app which has two screens. One screen is for user login/registration and other screen to show user their usernames and passwords for different urls.I created a separate kv file for this. Now in second screen I want to add a datatable which will display data of users in well arranged format.

According to kivy documentation we can only add datatable in our main program and not in .kv file but in my case I already have a .kv file. So how can I add datatable in my program in second screen?

from kivy.config import Config
from kivy import Config
Config.set('graphics', 'width', '500')
Config.set('graphics', 'height', '600')
Config.set('graphics', 'resizable', 'False')
from kivy.lang import Builder
from kivymd.app import MDApp

from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.uix.datatables import MDDataTable
import sqlite3


class MainWindow(Screen):
    pass


class SecondWindow(Screen):
    pass


class WindowManager(ScreenManager):
    pass

class MainApp(MDApp):
   
    def build(self):
      
       Config.set('graphics', 'resizable', False)
       self.theme_cls.theme_style = "Dark"
       self.theme_cls.primary_palette = "Purple"
     #  self.background_color = (0,0,0,0)
       
       return Builder.load_file("login.kv")
MainApp().run()

My .kv file------>

WindowManager:
    MainWindow:
    SecondWindow:

<MainWindow>:
    name: "main"

    MDCard:
        size_hint:None,None
        size:400,500
        pos_hint:{"center_x":.5,"center_y":.5}
        elevation:10
        padding:25
        spacing:25
        background_color:25

        orientation:"vertical"



<SecondWindow>:
    name: "second"
    BoxLayout:
        orientation:"vertical"
        size:root.width,root.height
        padding:30
        spacing:25

        Label:
            markup:True
            font_size:35
            text:"SAVED PASSWORDS"
            halign: "left"
            size_hint_y:  None
            height:self.texture_size[1]
            #padding_y:-15 
        
   

        MDDataTable:
            text: "Go Back"

Upvotes: 0

Views: 5927

Answers (2)

Shourov
Shourov

Reputation: 69

You also can do the same thing by this way

from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.uix.datatables import MDDataTable
from kivy.metrics import dp
from kivy.uix.anchorlayout import AnchorLayout
from kivy.lang.builder import Builder

KV = """
ScreenManager:
    DemoPage:
    
    ClientsTable:


<DemoPage>:
    MDRaisedButton:
        text: " Next "
        size_hint: 0.5, 0.06
        pos_hint: {"center_x": 0.5, "center_y": 0.4}
        on_release: 
            root.manager.current = 'Clientstable'
            
            
<ClientsTable>:
    name: 'Clientstable'
 """


class ClientsTable(Screen):
    def load_table(self):
        layout = AnchorLayout()
        self.data_tables = MDDataTable(
            pos_hint={'center_y': 0.5, 'center_x': 0.5},
            size_hint=(0.9, 0.6),
            use_pagination=True,
            check=True,
            column_data=[
                ("No.", dp(30)),
                ("Head 1", dp(30)),
                ("Head 2", dp(30)),
                ("Head 3", dp(30)),
                ("Head 4", dp(30)), ],
            row_data=[
                (f"{i + 1}", "C", "C++", "JAVA", "Python")
                for i in range(50)], )
        self.add_widget(self.data_tables)
        return layout

    def on_enter(self):
        self.load_table()


class DemoPage(Screen):
    pass


sm = ScreenManager()

sm.add_widget(DemoPage(name='demopage'))
sm.add_widget(ClientsTable(name='Clientstable'))


class MainWindow(MDApp):
    def build(self):
        screen = Builder.load_string(KV)
        return screen


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

Upvotes: 1

Ne1zvestnyj
Ne1zvestnyj

Reputation: 1397

You can do it this way (for example)

from kivymd.app import MDApp
from kivymd.uix.datatables import MDDataTable

from kivy.lang.builder import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.metrics import dp

KV = """
<LoginWindow>:
    name: "main"
    
    MDBoxLayout:
        orientation: 'vertical'
        padding: 10
        MDLabel:
            halign: "center"
            text: 'User login/registration'
        
        MDFloatingActionButton:
            icon: 'key'
            pos_hint: {'center_x': 0.5}
            on_release: 
                app.change_screen('second')
                app.add_datatable()

<DataWindow>:
    name: "second"
    MDBoxLayout:
        orientation: 'vertical'
        padding: 10
        MDFloatingActionButton:
            icon: 'arrow-left'
            on_release: app.change_screen('main')

        AnchorLayout:
            id: data_layout
    
WindowManager:
    LoginWindow:

    DataWindow:
        id: data_scr
"""


class LoginWindow(Screen):
    pass


class DataWindow(Screen):
    pass


class WindowManager(ScreenManager):
    pass


class Example(MDApp):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.data_tables = None

    def build(self):
        return Builder.load_string(KV)

    def add_datatable(self):
        self.data_tables = MDDataTable(
            size_hint=(0.9, 0.8),
            column_data=[
                ("No.", dp(30)),
                ("User", dp(30)),
                ("Password", dp(30)),
            ],
            row_data=[
                (
                    "1",
                    "The pitonist",
                    "Strong password",
                ),
                (
                    "2",
                    "The c++ lover",
                    "Save me!!!:)",
                ),
            ]
        )
        self.root.ids.data_scr.ids.data_layout.add_widget(self.data_tables)

    def change_screen(self, screen: str):
        self.root.current = screen


Example().run()

Upvotes: 0

Related Questions