I'm trying to create an app where after you log in you're shown a table of users. If the login is successful I want the user to be sent to another screen that has an MDDataTable in it.
All of the examples I've found online only show how to display the table using the build method in the main app.
Code example:
class ScreenOne(Screen):
# Displays MDDataTable without the need to press anything to view the table.
class ScreenTwo(Screen):
# When this screen validates user successfully it sends
# me to the other screen that shows an
# MDDataTable and not a blank screen etc...
sm = ScreenManager()
class MainApp(MDApp):
def build(self):
sm.add_widget(ScreenOne(name='screenone'))
sm.add_widget(ScreenTwo(name='screentwo'))
return sm
MainApp().run()
I keep getting either a blank screen or the following error:
ValueError: KivyMD: App object must be initialized before loading root widget. See https://github.com/kivymd/KivyMD/wiki/Modules-Material-App#exceptions
I kept changing the code trying to fix it but I just kept running into all sorts of errors and problems and I just don't understand enough to make my question any clearer.
Here's my .py
file code:
from kivy.properties import ObjectProperty
from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.stacklayout import MDStackLayout
from kivymd.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.uix.datatables import MDDataTable
from kivy.metrics import dp
from kivy.uix.anchorlayout import AnchorLayout
class ClientsTable(Screen):
def load_table(self):
layout = Screen()
data_tables = MDDataTable(
size_hint=(0.9, 0.6),
use_pagination=True,
check=True,
column_data=[
("No.", dp(30)),
("Column 1", dp(30)),
("Column 2", dp(30)),
("Column 3", dp(30)),
("Column 4", dp(30)),
("Column 5", dp(30)),],
row_data=[
(f"{i + 1}", "2.23", "3.65", "44.1", "0.45", "62.5")
for i in range(50)],)
layout.add_widget(data_tables)
return layout
class LoginPage(Screen):
username = ObjectProperty()
password = ObjectProperty()
def validate_user(self):
if self.username.text == "m":
sm.current = "Clientstable"
self.username.text = ""
self.password.text = ""
else:
print("Not here!")
sm = ScreenManager()
class MainWindow(MDApp):
def build(self):
self.title = "EasySport"
sm.add_widget(LoginPage(name='Loginpage'))
sm.add_widget(ClientsTable(name='Clientstable'))
return sm
if __name__ == "__main__":
MainWindow().run()
.kv
code:
ScreenManager:
LoginPage:
ClientsTable:
<LoginPage>:
username: User
password: Pass
MDTextField:
id: User
hint_text: "Username"
size_hint: 0.5, 0.09
pos_hint: {"center_x": 0.5, "center_y": 0.7}
MDTextField:
id: Pass
hint_text: "Password"
size_hint: 0.5, 0.09
pos_hint: {"center_x": 0.5, "center_y": 0.6}
MDFillRoundFlatButton:
text: "Login"
size_hint: 0.5, 0.06
pos_hint: {"center_x": 0.5, "center_y": 0.4}
on_release:
root.validate_user()
root.manager.transition.direction = 'left'
<ClientsTable>:
MDFillRoundFlatButton:
text: "Back"
size_hint: 0.5, 0.06
pos_hint: {"center_x": 0.5, "center_y": 0.4}
on_release:
root.manager.transition.direction = 'right'
root.manager.current = 'Loginpage'
MDFillRoundFlatButton:
text: "Load Table"
size_hint: 0.5, 0.06
pos_hint: {"center_x": 0.5, "center_y": 0.3}
on_release:
root.load_table()
Now it doesn't do anything when I press the Load Table
button!