Skip to content
Advertisement

How to go to second page in kivymd python

I have the first page with login and perform checking on the .py file with database (sqlite3). Now the problem is how do I go to another page after perform the checking. Here I use the sm.windowManager but it does not work and give me a blank page. Is there anything can help me to go to other page on the .py file instead on the .kv file on_press

Here is the code:

from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.lang import Builder
import sqlite3

class MainWindow(Screen):
    pass

class ReserveWindow(Screen):
    pass

class windowManager(ScreenManager):
    pass


sm = windowManager()
sm.add_widget(MainWindow(name='Main'))
sm.add_widget(ReserveWindow(name='Reserve'))


class Myapp(MDApp):
    connection = None
    connection = sqlite3.connect("book.db")
    cur = None
    def build(self):
        return sm

    def check(self,ID,password):
        connection = sqlite3.connect("book.db")
        c = connection.cursor()
        c.execute("select mmu_id,password from User where mmu_id =(?) and password = (?)", (ID, password))
        exists = c.fetchall()
        if exists:
            sm.current = 'Reserve'
        else:
            print("Error: Wrong ID or password.")


Myapp().run()

Myapp.kv

windowManager:
MainWindow:
ReserveWindow:
<MainWindow>:
    MDScreen:
        md_bg_color : [1,1,1,1]
        MDCard :
            size_hint : None,None
            size : 320,400
            pos_hint : {"center_x":.5,"center_y":.5}
            elevation : 15
            padding: 20
            spacing : 30
            orientation : 'vertical'
            MDLabel :
                text : 'Login'
                font_style : 'Button'
                font_size : 45
                halign : 'center'
                size_hint_y : None
                height : self.texture_size[1]
            MDTextFieldRound:
                hint_text : 'MMU Id'
                id : ID
                icon_right : 'account'
                size_hint_x : None
                width : 240
                font_size: 20
                pos_hint : {"center_x":.5}
            MDTextFieldRound:
                hint_text : 'password'
                id: password
                icon_right : 'eye-off'
                size_hint_x : None
                width : 240
                font_size: 20
                pos_hint : {"center_x":.5}
            MDRoundFlatButton :
                text : 'SIGN-IN'
                pos_hint : {"center_x":.5}
                font_size : 15
                md_bg_color : [118/255,251/255,130/255,1]
                theme_text_color: 'Custom'
                text_color: [0,0,0,1]
                on_press: app.check(ID.text,password.text)
            Widget:
                size_hint_y : None
                height : 15

<ReserveWindow>:
    MDScreen:
        MDLabel :
            text : 'Page 2'

Advertisement

Answer

The lines:

sm = windowManager()
sm.add_widget(MainWindow(name='Main'))
sm.add_widget(ReserveWindow(name='Reserve'))

build your widget tree. However the lines in your kv file:

windowManager:
    MainWindow:
    ReserveWindow:

also build a widget tree (but without the required name properties for each Screen).

I recommend changing you kv to include the names:

windowManager:
    MainWindow:
        name: "Main"
    ReserveWindow:
        name: "Reserve"

And using that to build your widget tree in your App:

# sm = windowManager()
# sm.add_widget(MainWindow(name='Main'))
# sm.add_widget(ReserveWindow(name='Reserve'))


class Myapp(MDApp):
    connection = None
    connection = sqlite3.connect("book.db")
    cur = None
    def build(self):
        sm = Builder.load_file('Myapp.kv')
        return sm

    def check(self,ID,password):
        connection = sqlite3.connect("book.db")
        c = connection.cursor()
        c.execute("select mmu_id,password from User where mmu_id =(?) and password = (?)", (ID, password))
        exists = c.fetchall()
        if exists:
            self.root.current = 'Reserve'
        else:
            print("Error: Wrong ID or password.")

Note that naming your kv file as my.kv would allow it to be loaded automatically and you could eliminate the build() method entrely (see documentation). But that would required a change in your check() method.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement