Accessing widget in child layout kivy - python

This is my code. I have a screen manager with two screens.
Screen 1: Login
Screen 2: Tabbed options
In screen 2 I have a dropdown widget in which I want to load some default values. But I am not able to access the id of the same.
Builder.load_string("""
<TACLogin>
FloatLayout:
MDToolbar:
title: "TEST"
pos_hint: {"x":0.001, "y":0.9}
MDRectangleFlatIconButton:
id: tac_screen1_next_id
icon: "page-next-outline"
font_size: '20sp'
font_name: 'fonts/prox_nova.ttf'
md_bg_color: 0, 0.502, 0.502, 1
text_color: 1,1,1,1
text: 'Login'
on_press: root.manager.current = 'tacconfig'
size_hint: None, None
size: dp(100), dp(30)
pos_hint: {"x":0.7, "y":0.2}
<TACConfig>
BoxLayout:
orientation:"vertical"
MDToolbar:
title: "Taxonomy Administration Console"
specific_text_color: app.theme_cls.accent_color
MDTabs:
id: android_tabs
text_color_normal: 1,1,1,1
Tab:
text:"Home"
text_color_normal: 1,1,1,1
MDLabel:
color: 0,0,0,1
font_size: '20sp'
font_name: 'fonts/prox_nova.ttf'
pos_hint: {"x":0.4, "y":0.1}
text:'WELCOME ADMIN'
Tab:
text_color_normal: 1,1,1,1
text:"Configure User"
TACUserConfig:
Tab:
text_color_normal: 1,1,1,1
text:"Configure DataSource"
TACScreen1:
Tab:
text_color_normal: 1,1,1,1
text:"Exit"
<TACUserConfig>
FloatLayout:
id: ucScreen_id
MDLabel:
color: 0,0,0,1
font_size: '15sp'
font_name: 'fonts/prox_nova.ttf'
text:'Select User Type'
pos_hint:{'center':(0.7,0.7)}
size_hint:0.8,1.4
MDDropDownItem:
id: user_type
pos_hint:{"x":0.5, "y":0.68}
dropdown_bg: [1, 1, 1, 1]
""")
This is my python code
class TACLogin(Screen, MDApp):
def __init__(self, **kwargs):
super(TACLogin, self).__init__(**kwargs)
class TACUserConfig(Screen, MDApp):
def __init__(self, **kwargs):
super(TACUserConfig, self).__init__(**kwargs)
print(self.ids)
def on_start(self):
self.ids.ucScreen_id.ids.user_type.items = ["Business User", "Admin User"]
sm = ScreenManager()
sm.add_widget(TACLogin(name='taclogin'))
sm.add_widget(TACConfig(name='tacconfig'))
sm.add_widget(TACUserConfig(name='tacuserconfig'))
class TestApp(MDApp):
def build(self):
self.theme_cls.primary_palette = "Teal"
return sm
if __name__ == '__main__':
TestApp().run()
I am trying to add values to the dropdown,the on_start function is not fired at all and I am not able to access the id of the dropdown user_type. Can someone please help me out?

Related

KivyMD: 'super' object has no attribute '__getattr__' during screen transition based on condition

I want to switch from login screen to menu screen based on a successful authentication, but this is the best I could do after a long search on condition-based screen transitions. Most sites say that screen-transitions in kivymd should be done using 'on-release' in .kv file, but I don't think that it would work in my code.
I've marked on the code the problematic line, which is raising the exception.
Teste.py
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.dialog import MDDialog
from kivymd.uix.button import MDFlatButton
from kivy.uix.screenmanager import ScreenManager, Screen
class login(Screen):
pass
class menu(Screen):
pass
Builder.load_file('lteste.kv')
class LoginApp(MDApp):
dialog = None
def build(self): #método construtor da parte visual do aplicativo
self.theme_cls.theme_style = "Light"
self.theme_cls.primary_palette = "Indigo"
self.theme_cls.accent_palette = "Blue"
self.sm = ScreenManager()
self.sm.add_widget(login(name="login"))
self.sm.add_widget(menu(name="menu"))
self.sm.current = "menu"
return self.sm
def dialog_box(self):
if not self.dialog:
self.dialog = MDDialog(
title="Log In",
text=f"Welcome {self.root.ids.user.text}!",
buttons=[MDFlatButton(text="Ok", text_color=self.theme_cls.primary_color,
on_release=self.close),],)
return self.dialog.open()
def login(self):
if self.root.ids.user.text=='1' and self.root.ids.password.text=='1':
self.sm.current = "menu" #<- problem
self.dialog_box()
return True
else:
return False
def close(self, instance):
self.dialog.dismiss()
LoginApp().run()
lteste.kv
<login>:
id: login
name: "login"
MDCard:
size_hint: None, None
size: 300, 600
pos_hint: {"center_x": 0.5, "center_y": 0.5}
elevation: 10
padding: 65
spacing: 35
orientation: 'vertical'
MDIcon:
icon: 'account'
icon_color: 0, 0, 0, 0
halign: 'center'
font_size: 180
MDTextFieldRound:
id: user
icon_left: "account-check"
hint_text: "Usuário"
foreground_color: 1, 0, 1, 1
size_hint_x: None
width: 220
font_size: 20
pos_hint: {"center_x": 0.5}
MDTextFieldRound:
id: password
icon_left: "key-variant"
hint_text: "Senha"
foreground_color: 1, 0, 1, 1
size_hint_x: None
height: 1
width: 220
font_size: 20
pos_hint: {"center_x": 0.5}
password: True
MDFillRoundFlatButton:
text: "ENTRAR"
font_size: 15
pos_hint: {"center_x": 0.5}
on_press: app.login()
MDFillRoundFlatButton:
text: "REGISTRAR-SE"
font_size: 15
pos_hint: {"center_x": 0.5}
<menu>
name: "menu"
id: menu
MDCard:
size_hint: None, None
size: 300, 600
pos_hint: {"center_x": 0.5, "center_y": 0.5}
elevation: 10
padding: 65
spacing: 35
orientation: 'vertical'
MDRaisedButton:
text: "Test"
The application root does not contain the ids you want. They are in the login screen widget because you defined them under it in kivy language. It would help to store a reference on the login screen to access them. Replace
self.sm.add_widget(login(name="login"))
with
self.login_screen = login(name="login")
self.sm.add_widget(self.login_screen)
Then, you can access the widgets like so,
text=f"Welcome {self.login_screen.ids.user.text}!",
and
if self.login_screen.ids.user.text=='1' \
and self.login_screen.ids.password.text=='1':
There is an example in the Kivy Documentation on kivy.uix.widget.Widget.ids and also Accessing Widgets defined inside Kv lang in your Python code.

How to change screens from python code in Kivy

So I'm working on making a fully functional login screen for my first serious kivy app and I want to be able to change windows/screens when the user presses on a button. I know normally in the KV file i'd just use on release but I want the on release to call to a method to verify the users credentials, and if those credentials are correct then change screens. So how in python itself would I be able to call to screen manager to change the screen?
from kivy.app import App
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
class LoginLayout(Screen):
def login(self, **kwargs):
print("Login function working")
userEmail = self.ids.username.text
userPassword = self.ids.password.text
print(userEmail)
print(userPassword)
ScreenManager.current('emailWindow')
class EmailWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file('loginScreen.kv')
class LoginScreen(App):
def build(self):
return kv
app = LoginScreen()
app.run()
KV
ScreenManager:
LoginLayout:
EmailWindow:
<LoginLayout>:
name: 'loginWindow'
canvas.before:
Color:
rgba: (28/255, 102/255, 137/255, 1)
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
orientation: 'vertical'
size: root.width, root.height
Label:
text: 'Username'
font_name: 'Framd.ttf'
font_size: 20
TextInput:
id: username
multiline: False
size_hint: (.5, .3)
pos_hint: {'center_x' : .5}
Label:
text: 'Password'
font_name: 'Framd.ttf'
font_size: 20
TextInput:
id: password
multiline: False
size_hint: (.5, .3)
pos_hint: {'center_x' : .5}
Button:
text: 'Login'
size_hint: (.2, .8)
pos_hint: {'center_x' : 0.5}
font_name: 'Framd.ttf'
on_release: root.login()
Button:
text: 'Create Account'
size_hint: (.2, .8)
pos_hint: {'center_x' : 0.5}
font_name: 'Framd.ttf'
Button:
text: 'Forgot login Info'
size_hint: (.2, .8)
pos_hint: {'center_x' : 0.5}
font_name: 'Framd.ttf'
<EmailWindow>:
name: 'emailWindow'
canvas.before:
Color:
rgba: (28/255, 102/255, 137/255, 1)
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
orientation: 'vertical'
size: root.width, root.height
Label:
text: 'To:'
font_name: 'Framd.ttf'
TextInput:
multiline: False
pos_hint: {'center_x': 0.5}
size_hint: (.5, .3)
font_name: 'Framd.ttf'
Label:
text: 'Subject'
TextInput:
multiline: False
pos_hint: {'center_x': 0.5}
size_hint: (.5, .3)
font_name: 'Framd.ttf'
Label:
text: 'Body'
font_name: 'Framd.ttf'
TextInput:
size_hint: (.5, .7)
pos_hint: {'center_x': 0.5}
multiline: True
Button:
text: 'send'
size_hint: (.2, .8)
pos_hint: {'center_x' : 0.5}
font_name: 'Framd.ttf'
When a Screen gets added to a ScreenManager, it gets a manager attribute set to the ScreenManager. So in your login() method you can do:
self.manager.current = 'emailWindow'
instead of:
ScreenManager.current('emailWindow')
Note that using ScreenManager, as you do above, references the ScreenManager class, not the ScreenManager instance that is in your App.

How to get user input from an MDTextField in KivyMD

Please I keep getting an attribute error when I try to get the text a user input into an MDTextField, tried everything I could think of, researched and couldn't get anything. Please I need your help.
I try to get the text with this argument of self.root.ids.MyId.text
This is the error I get:
File "C:/Users/user/Documents/KmD/Webma/main_webma.py", line 120, in login
email_addr = self.root.ids.email.text
File "kivy\properties.pyx", line 864, in kivy.properties.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__'
And this is my main.py code
from kivymd.app import MDApp
from kivy.clock import Clock
from kivy.lang import Builder
from home_page_strings import toolbar
import sqlite3
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.uix.picker import MDThemePicker
from kivy.uix.popup import Popup
from kivymd.uix.textfield import MDTextField
# calling of screen classes and addition to the screen_manager
class Friend1(Screen):
pass
class WelcomeScreen(Screen):
def __init__(self, **kwargs):
super(WelcomeScreen, self).__init__(**kwargs)
Clock.schedule_once(self.callNext, 5)
def callNext(self, dt):
self.manager.current = 'account'
class AccountScreen(Screen):
pass
class HomeScreen(Screen):
pass
class RoomScreen(Screen):
pass
class ContactsScreen(Screen):
pass
class CallsScreen(Screen):
pass
class SettingsScreen(Screen):
pass
class FaqScreen(Screen):
pass
class InviteScreen(Screen):
pass
manager = ScreenManager()
manager.add_widget(Friend1(name='friend1'))
manager.add_widget(HomeScreen(name='home'))
manager.add_widget(WelcomeScreen(name='welcome'))
manager.add_widget(RoomScreen(name='room'))
manager.add_widget(ContactsScreen(name='contacts'))
manager.add_widget(CallsScreen(name='calls'))
manager.add_widget(SettingsScreen(name='settings'))
manager.add_widget(FaqScreen(name='faq'))
manager.add_widget(InviteScreen(name='invite'))
manager.add_widget(AccountScreen(name='account'))
# end of call of screen classes and addition to screenmanager
class EMC_WebmaApp(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
# -------------------------- CODE FOR THE DATABASE ---------------------------------- #
conn = sqlite3.connect('webma_database.db')
cursor = conn.cursor()
# cursor.execute('''CREATE TABLE users (
# email text,
# password integer
# )''')
def sign_up(self, cursor):
cursor.execute('INSERT INTO users VALUES('', '')')
conn.commit()
conn.close()
# -------------------------- END OF CODE FOR THE DATABASE ---------------------------------- #
# BUILDER FUNCTION AND SCREEN
def build(self):
screen = Screen()
# theme settings
self.theme_cls.primary_palette = 'DeepPurple'
self.theme_cls.theme_style = 'Dark'
# end of theme declaration
# loaded strings
navigation_bar = Builder.load_string(toolbar)
# end of loaded strings
# addition of loaded strings to screen + return screen
screen.add_widget(navigation_bar)
return screen
# end of addition
# END OF BUILDER FUNCTION
# USER VALIDATION FUNCTION
def login(self):
email_addr = self.root.ids.email.text
if email_addr == str(''):
print('Invalid Email Address')
# END OF USER VALIDATION
def change(self, dt):
self.root.manager.current = 'home'
# App theme manager
def change_theme(self):
picker = MDThemePicker()
picker.open()
# End of app theme manager
# home page search bar
def search_bar(self, dt):
searchbar = Popup(
title='Search',
content=MDTextField(
helper_text='Input values to be searched for',
helper_text_mode='on_focus'
),
auto_dismiss=True,
# size_hint=(None, None),
# size=(400, 98)
size_hint=(0.7, 0.17),
pos_hint={'center_x': 0.5, 'center_y': 0.8}
)
searchbar.open()
# APP RUN
if __name__ == '__main__':
EMC_WebmaApp().run()
# WELL... THIS IS THE END OF EVERYTHING
Then my string code where I try to get my text from
toolbar = '''
ScreenManager:
WelcomeScreen:
HomeScreen:
RoomScreen:
ContactsScreen
CallsScreen:
SettingsScreen:
FaqScreen:
InviteScreen:
AccountScreen:
Friend1:
<WelcomeScreen>:
name: 'welcome'
Image:
source: 'logo.png'
size_hint: 0.4, 0.4
pos_hint: {'center_x': 0.5, 'center_y':0.6}
MDLabel:
text: 'Webma'
halign: 'center'
font_style: 'H3'
color: (1, 1, 1, 1)
pos_hint: {'center_x': 0.5, 'center_y':0.8}
MDLabel:
text: 'Powered by emc_codes'
halign: 'center'
bold: 'True'
italic: True
pos_hint: {'center_x': 0.5, 'center_y':0.1}
<HomeScreen>:
name: 'home'
NavigationLayout:
ScreenManager:
Screen:
BoxLayout:
orientation: 'vertical'
spacing: '8dp'
MDToolbar:
title: 'EMC_Webma'
left_action_items: [['menu', lambda x: navigation.set_state('open')]]
right_action_items: [['card-search', app.search_bar]]
elevation: 10
ScrollView:
MDList:
TwoLineAvatarIconListItem:
text: 'Friend 1'
ImageLeftWidget:
source: 'einstein.jpg'
IconRightWidget:
icon: 'dots-vertical'
MDNavigationDrawer:
id: navigation
BoxLayout:
orientation: 'vertical'
spacing: '8dp'
padding: '8dp'
Image:
source: 'einstein.jpg'
MDLabel:
text: 'Enwerem Melvin Confidence'
font_style: 'Subtitle2'
size_hint_y: None
height: self.texture_size[1]
MDLabel:
text: 'enweremmelvinconfidence#gmail.com'
font_style: 'Caption'
size_hint_y: None
height: self.texture_size[1]
ScrollView:
MDList:
OneLineIconListItem:
text: 'Room'
on_release: root.manager.current = 'room'
IconLeftWidget:
icon: 'theater'
on_release: root.manager.current = 'room'
OneLineIconListItem:
text: 'Contacts'
on_release: root.manager.current = 'contacts'
IconLeftWidget:
icon: 'contacts'
on_release: root.manager.current = 'contacts'
OneLineIconListItem:
text: 'Calls'
on_release: root.manager.current = 'calls'
IconLeftWidget:
icon: 'cellphone-android'
on_release: root.manager.current = 'calls'
OneLineIconListItem:
text: 'Settings'
on_release: root.manager.current = 'settings'
IconLeftWidget:
icon: 'settings'
on_release: root.manager.current = 'settings'
OneLineIconListItem:
text: 'FAQ'
on_release: root.manager.current = 'faq'
IconLeftWidget:
icon: 'frequently-asked-questions'
on_release: root.manager.current = 'faq'
OneLineIconListItem:
text: 'Invite Friends'
on_release: root.manager.current = 'invite'
IconLeftWidget:
icon: 'account-plus'
on_release: root.manager.current = 'invite'
OneLineIconListItem:
text: 'Theme Manager'
on_release:
app.change_theme()
IconLeftWidget:
icon: 'theme-light-dark'
<RoomScreen>:
name: 'room'
MDLabel:
text: 'Welcome to the Room Screen'
halign: 'center'
bold: True
font_size: 50
MDIconButton:
icon: 'keyboard-backspace'
pos_hint: {'center_x': 0.06, 'center_y': 0.96}
on_release: root.manager.current = 'home'
size_hint: None, None
<ContactsScreen>:
name: 'contacts'
MDLabel:
text: 'Welcome to the Contacts Screen'
halign: 'center'
bold: True
font_size: 50
MDIconButton:
icon: 'keyboard-backspace'
pos_hint: {'center_x': 0.06, 'center_y': 0.96}
on_release: root.manager.current = 'home'
size_hint: None, None
<CallsScreen>:
name: 'calls'
MDLabel:
text: 'Welcome to the Calls Screen'
halign: 'center'
bold: True
font_size: 50
MDIconButton:
icon: 'keyboard-backspace'
pos_hint: {'center_x': 0.06, 'center_y': 0.96}
on_release: root.manager.current = 'home'
size_hint: None, None
<SettingsScreen>:
name: 'settings'
MDLabel:
text: 'Welcome to the Settings Screen'
halign: 'center'
bold: True
font_size: 50
MDIconButton:
icon: 'keyboard-backspace'
pos_hint: {'center_x': 0.06, 'center_y': 0.96}
on_release: root.manager.current = 'home'
size_hint: None, None
<FaqScreen>:
name: 'faq'
MDLabel:
text: 'Welcome to the Faq Screen'
halign: 'center'
bold: True
font_size: 50
MDIconButton:
icon: 'keyboard-backspace'
pos_hint: {'center_x': 0.06, 'center_y': 0.96}
on_release: root.manager.current = 'home'
size_hint: None, None
<InviteScreen>:
name: 'invite'
MDLabel:
text: 'Welcome to the Invite Screen'
halign: 'center'
bold: True
font_size: 50
MDIconButton:
icon: 'keyboard-backspace'
pos_hint: {'center_x': 0.06, 'center_y': 0.96}
on_release: root.manager.current = 'home'
size_hint: None, None
<AccountScreen>:
name: 'account'
BoxLayout:
MDLabel:
text: 'Account Login'
pos_hint: {'center_x': 0.6, 'center_y': 0.9}
color: (1, 1, 1, 1)
font_size: 40
MDTextFieldRound:
id: email
hint_text: 'Email Address'
icon_right: 'email'
icon_right_color: app.theme_cls.primary_color
pos_hint: {'center_x': 0.5, 'center_y': 0.7}
size_hint_x: None
width: 300
MDTextFieldRound:
id: password
hint_text: 'Enter Password'
icon_right: 'lock'
icon_right_color: app.theme_cls.primary_color
password: True
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
size_hint_x: None
width: 300
MDRaisedButton:
text: 'Login'
md_bg_color: app.theme_cls.primary_color
pos_hint: {'center_x': 0.5, 'center_y': 0.2}
on_release: app.login() # root.manager.current = 'home'
<Friend1>
name: 'friend1'
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: 'Friend 1'
left_action_items: [['keyboard-backspace', app.change]]
right_action_items: [['phone', lambda x: print('go ahead and make a call')], ['video', lambda x: print('Video')], ['dots-vertical', lambda x: print('dots')]]
MDLabel:
text: 'This is a test screen for our Chat'
halign: 'center'
'''
Please guys I really appreciate the help. Thanks in advance...
The email id only appears in the ids for the AccountScreen. Any id appearing in a kv gets inserted into the ids of the widget that is the root of the rule that contains the id. So, you must access the AccountScreen instance to get to the email id:
# BUILDER FUNCTION AND SCREEN
def build(self):
screen = Screen()
# theme settings
self.theme_cls.primary_palette = 'DeepPurple'
self.theme_cls.theme_style = 'Dark'
# end of theme declaration
# loaded strings
self.navigation_bar = Builder.load_string(toolbar) # save a reference to the loaded ScreenManager
# end of loaded strings
# addition of loaded strings to screen + return screen
screen.add_widget(self.navigation_bar)
return screen
# end of addition
# END OF BUILDER FUNCTION
# USER VALIDATION FUNCTION
def login(self):
# use the saved reference to access the email address
email_addr = self.navigation_bar.get_screen('account').ids.email.text
if email_addr == str(''):
print('Invalid Email Address')
# END OF USER VALIDATION
value = self.root.get_screen('nameofscreen').ids.username.text
print("What you typed is: ", value)
syntax = self.root.get_screen('name').ids.id.text

Python KivyMD: how to call a function using a button?

im new to kivy and kivyMD, and i was trying to call a function that would print the email and password of a user. how would can i bind the function or use on_press in this code?
i tried using on_pressed:root.function() method, but it doesnt work since my function is not written in the pr-emade ScreenManager
.PY
import...
Builder.load_string("""
#:include kv/login.kv
#:import utils kivy.utils
#:import images_path kivymd.images_path
""")
class MyApp(MDApp):
def __init__(self, **kwargs):
self.title = "iKarate"
self.theme_cls.theme_style = "Light"
self.theme_cls.primary_palette = "Blue"
self.sm = ScreenManager()
super().__init__(**kwargs)
def build(self):
self.sm.add_widget(Factory.LoginScreen())
return self.sm
def doThis(self):
email = self.email
password = self.password
print(email, password)
if __name__ == "__main__":
MyApp().run()
.KV
#:kivy 1.11.1
<LoginScreen#Screen>:
name: "login"
BackgroundLayer:
#MDCard:
MDCard:
orientation: "vertical"
size_hint: [0.8, 0.6]
pos_hint: {"center_x": 0.5, "center_y": 0.6}
BoxLayout:
orientation: "vertical"
MDLabel:
text: "Welcome to the log in page"
text_size: self.size
font_size: 25
bold: True
halign: "center"
valign: "middle"
Image:
size_hint_y: 10
source: "kv/image.png"
MDTextField:
id: email
hint_text: "E-mail"
MDTextField:
id: password
hint_text: "Password"
password: True
MDFillRoundFlatButton:
id: btn
text: "Sign In"
width: dp(200)
pos_hint: {"center_x": .5}
on_press:print("pressed")
<BackgroundLayer#BoxLayout>:
orientation: "vertical"
BoxLayout:
orientation: "vertical"
canvas.before:
Color:
rgba: utils.get_color_from_hex("#00146e")
Rectangle:
pos: 0, self.center_y + self.height/3 - 50
size: (self.width,70)
BoxLayout:
orientation: "horizontal"
on_press:print("pressed") successfully prints "pressed"
Use this if you call a function from App class:
on_press: app.doThis()
And use this if you call a function from Screen class:
on_press: root.doThis()

KivyMD: Accessing Label ID From Screen Class

I am using KivyMD and would like to refrash data in the the secound screen using the "on_pre_enter" method.
I am trying to access a label id from screen class "on_pre_enter" method with out success.
I am able to access the label id from the "MainApp" class but unable to access it from the "Second_Screen" class.
MainApp.py
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen
class MainApp(MDApp):
def __init__(self, **kwargs):
self.title = "My Material Application"
self.theme_cls.primary_palette = "Blue"
super().__init__(**kwargs)
def on_start(self):
self.root.ids.main_screen_label.text = "Main Screen Updated"
def build(self):
self.root = Builder.load_file("app.kv")
class Second_Screen(Screen):
def on_pre_enter(self):
MainApp.ids.second_screen_label = "Second Screen Updated"
pass
if __name__ == "__main__":
MainApp().run()
App.kv
NavigationLayout:
MDNavigationDrawer:
NavigationDrawerSubheader:
text: "Menu:"
NavigationDrawerIconButton:
icon:'battery'
text: "Main Screen"
on_release:
screen_manager.current = "main_screen"
NavigationDrawerIconButton:
icon:'battery'
text: "Second Screen"
on_release:
screen_manager.current = "second_screen"
BoxLayout:
id: box1
orientation: 'vertical'
MDToolbar:
title: "My App"
md_bg_color: app.theme_cls.primary_color
left_action_items:
[['menu', lambda x: app.root.toggle_nav_drawer()]]
ScreenManager:
id: screen_manager
Screen:
name: "main_screen"
BoxLayout:
size_hint: .8, .8
pos_hint: {"center_x": .5, "center_y": .5}
spacing: dp(100)
orientation: 'vertical'
MDLabel:
id: main_screen_label
text: "Main Screen Default"
Second_Screen:
name: "second_screen"
FloatLayout:
id: float
size_hint: .8, .8
pos_hint: {"center_x": .5, "center_y": .5}
spacing: dp(100)
orientation: 'vertical'
MDLabel:
id: second_screen_label
text: "Second Screen Default"
I also tried using:
class Second_Screen(Screen):
def on_pre_enter(self):
self.ids.second_screen_label.text = "Second Screen Updated"
pass
After starting the i get the following error:
self.ids.second_screen_label.text = "Second Screen Updated"
File "kivy\properties.pyx", line 863, in
kivy.properties.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__'
What is the correct way to access the id's defined in the KV file from the screen class?
Define screen layout in separate rule <MyScreen>:
<MyScreen>:
name: "my_screen"
FloatLayout:
# ...
To add this screen to ScreenManager, just write:
ScreenManager:
id: screen_manager
MyScreen:
To get access to screen widgets use
from screen class: self.ids.my_id
from app class: self.root.ids.screen_manager.get_screen("screen_name").ids.my_id
from other widgets: App.get_running_app()..root.ids.screen_manager.get_screen("screen_name").ids.my_id
So your full code will be:
main.py
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen
class MainApp(MDApp):
def __init__(self, **kwargs):
self.title = "My Material Application"
self.theme_cls.primary_palette = "Blue"
super().__init__(**kwargs)
def build(self):
self.root = Builder.load_file("app.kv")
def on_start(self):
self.root.ids.screen_manager.get_screen(
"main_screen"
).ids.main_screen_label.text = "Main Screen Updated"
class MainScreen(Screen):
pass
class SecondScreen(Screen):
def on_pre_enter(self):
self.ids.second_screen_label.text = "Second Screen Updated"
if __name__ == "__main__":
MainApp().run()
app.kv
NavigationLayout:
MDNavigationDrawer:
NavigationDrawerSubheader:
text: "Menu:"
NavigationDrawerIconButton:
icon: "battery"
text: "Main Screen"
on_release:
screen_manager.current = "main_screen"
NavigationDrawerIconButton:
icon: "battery"
text: "Second Screen"
on_release:
screen_manager.current = "second_screen"
BoxLayout:
id: box1
orientation: "vertical"
MDToolbar:
title: "My App"
md_bg_color: app.theme_cls.primary_color
left_action_items:
[["menu", lambda x: app.root.toggle_nav_drawer()]]
ScreenManager:
id: screen_manager
MainScreen:
SecondScreen:
<MainScreen>:
name: "main_screen"
BoxLayout:
size_hint: .8, .8
pos_hint: {"center_x": .5, "center_y": .5}
spacing: dp(100)
orientation: "vertical"
MDLabel:
id: main_screen_label
text: "Main Screen Default"
<SecondScreen>:
name: "second_screen"
FloatLayout:
id: float
size_hint: .8, .8
pos_hint: {"center_x": .5, "center_y": .5}
spacing: dp(100)
orientation: "vertical"
MDLabel:
id: second_screen_label
text: "Second Screen Default"

Categories