Search code examples
pythonkivykivy-languagekivymd

I want to call a screen from python file in kivy builder format


Basically, I am writing code in builder format but I need to use python code for my table screen. I don't know how to call the table screen to open when a button is executed in kivy string file. Here's the minimal working code for the same:

from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.uix.datatables import MDDataTable
from kivy.metrics import dp

Window.size = (350,600)

class FirstWindow(Screen):
    pass

class SecondWindow(Screen):
    pass

class WindowManager(ScreenManager):
    pass


kv="""
WindowManager:
    FirstWindow:

<FirstWindow>:
    name: "first"
    MDRoundFlatButton:
        text: "SIGN UP"
        font_size: 12
        pos_hint: {"center_x": 0.5,"center_y": 0.3}
        on_release: 
        
            app.root.current = "table" # I Want to call Table screen Here
            
            root.manager.transition.direction = "left"

"""

class MainApp(MDApp):
    def build(self):
        screen = Screen()
        table = MDDataTable(pos_hint={'center_x': 0.5, 'center_y': 0.5},size_hint=(0.9, 0.6),check=True,column_data=[("First Name", dp(30)),("Last Name", dp(30)),("Email Address", dp(30)),("Phone Number", dp(30))],row_data=[("John", "Elder", "zzz@xyz.com", "(123) 456-1111"),("Mary", "Elder", "yyy@xyz.com", "(123) 456-1111")])

        # Bind the table
        table.bind(on_check_press=self.checked)
        table.bind(on_row_press=self.row_checked)

        self.theme_cls.theme_style = "Light"
        self.theme_cls.primary_palette = "BlueGray"

        screen.add_widget(table)
        return Builder.load_string(kv) # the table screen should be called from within

    # Function for check presses
    def checked(self, instance_table, current_row):
        print(instance_table, current_row)

    # Function for row presses
    def row_checked(self, instance_table, instance_row):
        print(instance_table, instance_row)

MainApp().run()

Information about the project: I want to link the table to the online database. Other features are not dynamic so I have been coding them in .kv file. The button on the first screen should lead me to the table screen but I am unable to code it. Please help.


Solution

  • you made two mistake

    1. Is you did not give the screen name 'table'
    2. you did not add the screen to the root widget so this code should work for you
    
    from kivy.lang import Builder
    from kivymd.app import MDApp
    from kivy.core.window import Window
    from kivy.uix.screenmanager import ScreenManager, Screen
    from kivymd.uix.datatables import MDDataTable
    from kivy.metrics import dp
    
    Window.size = (350, 600)
    
    
    class FirstWindow(Screen):
        pass
    
    
    class SecondWindow(Screen):
        pass
    
    
    class WindowManager(ScreenManager):
        pass
    
    
    kv = """
    WindowManager:
        FirstWindow:
    
    <FirstWindow>:
        name: "first"
        MDRoundFlatButton:
            text: "SIGN UP"
            font_size: 12
            pos_hint: {"center_x": 0.5,"center_y": 0.3}
            on_release: 
    
                app.root.current = "table" # I Want to call Table screen Here
    
                root.manager.transition.direction = "left"
    
    """
    
    
    class MainApp(MDApp):
        def build(self):
            # naming the screen
            screen = Screen(name='table')
            table = MDDataTable(pos_hint={'center_x': 0.5, 'center_y': 0.5}, size_hint=(0.9, 0.6), check=True,
                                column_data=[("First Name", dp(30)), ("Last Name", dp(30)), ("Email Address", dp(30)),
                                             ("Phone Number", dp(30))],
                                row_data=[("John", "Elder", "zzz@xyz.com", "(123) 456-1111"),
                                          ("Mary", "Elder", "yyy@xyz.com", "(123) 456-1111")])
    
            # Bind the table
            table.bind(on_check_press=self.checked)
            table.bind(on_row_press=self.row_checked)
    
            self.theme_cls.theme_style = "Light"
            self.theme_cls.primary_palette = "BlueGray"
    
            screen.add_widget(table)
            # load the root widget
            root= Builder.load_string(kv)  # the table screen should be called from within
            # add the screen to the root widget
            root.add_widget(screen)
            return root
    
        # Function for check presses
        def checked(self, instance_table, current_row):
            print(instance_table, current_row)
    
        # Function for row presses
        def row_checked(self, instance_table, instance_row):
            print(instance_table, instance_row)
    
    
    MainApp().run()