now i have made this whole project using kivyMD and I'm new to it what i want is to just login but for some reason its showing the error that the screen does not exist i tried everything but nothing helps any help will be appreciated. NOW HERE the main .py file.
Builder.load_file("welcome.kv")
class Login_page(Screen):
def validate_user(self):
self.sm = ScreenManager(transition=NoTransition())
print(details.keys())
Username = MainApp.get_running_app().root.get_screen('Login_Page').ids.Username.text
if Username in list(details.keys()):
if details[Username] == MainApp.get_running_app().root.get_screen('Login_Page').ids.Password.text:
self.manager.current = "Main_page"
else:
print("not here")
class ScreenManagement(ScreenManager):
pass
class Main_page(Screen):
pass
class MainApp(MDApp):
def build(self):
self.sm = ScreenManager(transition=NoTransition())
self.sm.add_widget(Login_page(name = "Login_Page"))
self.sm.add_widget(Sign_in_Page(name="Sign_in_Page"))
self.sm.add_widget(Main_page(name = "Main_page"))
self.sm.add_widget(Billings_Table(name="Billings_Table"))
self.sm.add_widget(Employee_Table(name="Employee_Table"))
self.sm.add_widget(Courier_Order_and_Details(name = "Courier_Order_and_Details"))
return self.sm
now below is the .kv part
<Login_Page>:
name : "Login_Page"
MDScreen:
MDToolbar:
title : "Courier Service System"
pos_hint : {"top":1}
Image :
source : "courier_design(256256).png"
pos_hint : {'center_x':0.5,'center_y':0.7}
MDTextField :
id : Username
hint_text: "Enter Username"
halign : "center"
size_hint : (0.25, 0.10)
pos_hint : {'center_x': 0.5, 'center_y': 0.45}
font_size : 12
MDTextField :
id : Password
hint_text : "Password"
halign : "center"
size_hint : (0.25, 0.1)
pos_hint : {'center_x': 0.5, 'center_y': 0.35}
font_size : 12
password : True
MDFillRoundFlatButton :
text : "Log in"
font_size : 12
size_hint : (0.25,0.05)
pos_hint : {'center_x' : 0.5,'center_y' : 0.28}
on_release: app.validate_user()
MDLabel :
text : "——————— or ———————"
font_size : 15
size_hint : (0.25, 0.05)
pos_hint : {'center_x': 0.508, 'center_y': 0.23}
theme_text_color : "Secondary"
MDFillRoundFlatButton :
text : "Sign in"
font_size : 12
size_hint : (0.25, 0.05)
pos_hint : {'center_x': 0.5, 'center_y': 0.18}
on_release: app.root.current = "Sign_in_Page"
<Main_page>
name : "Main_page"
MDScreen:
MDLabel :
text : "Our Services :"
align : "center"
font_size : 30
size_hint : (0.5, 0.5)
pos_hint : {'center_x': 0.5, 'center_y': 0.8}
theme_text_color : "Secondary"
MDToolbar:
title: "Back"
left_action_items: [["arrow-left", lambda x: app.change_screen("Login_Page")]]
pos_hint : {"top":1}
font_size : 15
MDFillRoundFlatButton :
text : "Courier Order and Courier Details"
font_size : 14
size_hint : (0.4, 0.09)
pos_hint : {'center_x': 0.5, 'center_y': 0.6}
on_release: app.root.current = "Courier_Order_and_Details"
MDFillRoundFlatButton :
text : "Billings Details"
font_size : 14
size_hint : (0.4, 0.09)
pos_hint : {'center_x': 0.5, 'center_y': 0.4}
on_release: app.root.current = "Billings_Table"
MDFillRoundFlatButton :
text : "Our Employees"
font_size : 14
size_hint : (0.4, 0.09)
pos_hint : {'center_x': 0.5, 'center_y': 0.2}
on_release: app.root.current ='Employee_Table'
now there two validate codes and i know that first one i used when i wrote
root.validate_user
and still got the similar problem
CodePudding user response:
When building a large app with kivymd, it is more effective to use the screen manager as the root of the app. which means the kv fill look like this
#: import NoTransition kivy.uix.screenmanager.NoTransition
<LoginPage>:
MDScreen:
MDToolbar:
title : "Courier Service System"
pos_hint : {"top":1}
Image :
source : "courier_design(256256).png"
pos_hint : {'center_x':0.5,'center_y':0.7}
MDTextField :
id : Username
hint_text: "Enter Username"
halign : "center"
size_hint : (0.25, 0.10)
pos_hint : {'center_x': 0.5, 'center_y': 0.45}
font_size : 12
MDTextField :
id : Password
hint_text : "Password"
halign : "center"
size_hint : (0.25, 0.1)
pos_hint : {'center_x': 0.5, 'center_y': 0.35}
font_size : 12
password : True
MDFillRoundFlatButton :
text : "Log in"
font_size : 12
size_hint : (0.25,0.05)
pos_hint : {'center_x' : 0.5,'center_y' : 0.28}
on_release: root.validate_user()
MDLabel :
text : "——————— or ———————"
font_size : 15
size_hint : (0.25, 0.05)
pos_hint : {'center_x': 0.508, 'center_y': 0.23}
theme_text_color : "Secondary"
MDFillRoundFlatButton :
text : "Sign in"
font_size : 12
size_hint : (0.25, 0.05)
pos_hint : {'center_x': 0.5, 'center_y': 0.18}
on_release: app.root.current = "signin_page"
<MainPage>
MDScreen:
MDLabel :
text : "Our Services :"
align : "center"
font_size : 30
size_hint : (0.5, 0.5)
pos_hint : {'center_x': 0.5, 'center_y': 0.8}
theme_text_color : "Secondary"
MDToolbar:
title: "Back"
left_action_items: [["arrow-left", lambda x: app.change_screen("Login_Page")]]
pos_hint : {"top":1}
font_size : 15
MDFillRoundFlatButton :
text : "Courier Order and Courier Details"
font_size : 14
size_hint : (0.4, 0.09)
pos_hint : {'center_x': 0.5, 'center_y': 0.6}
on_release: app.root.current = "c_o_a_details"
MDFillRoundFlatButton :
text : "Billings Details"
font_size : 14
size_hint : (0.4, 0.09)
pos_hint : {'center_x': 0.5, 'center_y': 0.4}
on_release: app.root.current = "billings_table"
MDFillRoundFlatButton :
text : "Our Employees"
font_size : 14
size_hint : (0.4, 0.09)
pos_hint : {'center_x': 0.5, 'center_y': 0.2}
on_release: app.root.current ='employee_table'
ScreenManager:
id: screen_manager
transition: NoTransition()
LoginPage:
id: login_page
name: "login_page"
MainPage:
id: main_page
name: "main_page"
SigninPage:
id: signin_page
name: "signin_page"
BillingsTable:
id: billings_table
name: "billings_table"
EmployeeTable:
id: employee_table
name: "employee_table"
CourierOrderAndDetails:
id: c_o_a_details
name: "c_o_a_details"
and the main.py
file should be something like this
class LoginPage(Screen):
def validate_user(self):
print(details.keys())
Username = self.ids.Username.text
if Username in list(details.keys()):
if details[Username] == self.ids.Password.text:
MDApp.get_running_app().switch_screen(main_page)
else:
print("not here")
class MainPage(Screen):
pass
class SigninPage(Screen):
pass
class BillingsTable(Screen):
pass
class EmployeeTable(Screen):
pass
class CourierOrderAndDetails(Screen):
pass
class MainApp(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.load_kv("welcome.kv")
def switch_screen(self, screen_name, *args):
self.root.current = screen_name
if you noticed, i have restructured most of your code. the idea is not that what your did was wrong. just that it would be easier to scale the app when structured like this.
The error you had with the main page screen not being found is because first you created a whole new instance of the screenmanager object which is completely differen from the one in the build function of the MainApp class and hence does not have the screens added
here: self.sm = ScreenManager(transition=NoTransition())
Next you use self.manager.current = "Main_page"
to naviagate to the mainpage screen but Login_Page has no attribute self.manager
. you created self.sm
but used self.manager
Anyways, This should solve your problem.