Change screen with .kv language on button click - python-3.x

I know kivy and kivymd but I don't know the functions yet. I want to change the screen of my app on button click, how can I do that? Thank you.
Below is my code
from kivy.lang import Builder
from kivymd.app import MDApp
KV = '''
Screen:
MDLabel:
text: "Material Design"
font_style: "H3"
theme_text_color: "Secondary"
pos_hint: {"center_y": 0.9}
halign: "center"
MDRaisedButton:
text: "Start"
md_bg_color: 0, 0.26, 0.27, 1
pos_hint: {"center_x": 0.5, "center_y": 0.2}
on_press: root.manager.current = "main"
MDRectangleFlatButton:
text: "Exit"
md_bg_color: rgba(197, 232, 204, 1)
text_color: 0, 0.26, 0.27, 1
pos_hint: {"center_x": 0.5, "center_y": 0.1}
Screen:
name: "main"
MDLabel:
text: "School guru"
font_style: "H3"
pos_hint: {"center_y": 0.8}
halign: "center"
'''
class MaterialDesign(MDApp):
def build(self):
return Builder.load_string(KV)
MaterialDesign().run()
Please try to edit my code and show how to do it.
Once again, thank you.

First, if you are going to switch between Screens, you need a ScreenManager.
Here is a modified version of your code that works:
from kivy.lang import Builder
from kivymd.app import MDApp
KV = '''
ScreenManager:
Screen:
MDLabel:
text: "Material Design"
font_style: "H3"
theme_text_color: "Secondary"
pos_hint: {"center_y": 0.9}
halign: "center"
MDRaisedButton:
text: "Start"
md_bg_color: 0, 0.26, 0.27, 1
pos_hint: {"center_x": 0.5, "center_y": 0.2}
on_press: root.current = "main"
MDRectangleFlatButton:
text: "Exit"
md_bg_color: rgba(197, 232, 204, 1)
text_color: 0, 0.26, 0.27, 1
pos_hint: {"center_x": 0.5, "center_y": 0.1}
Screen:
name: "main"
MDLabel:
text: "School guru"
font_style: "H3"
pos_hint: {"center_y": 0.8}
halign: "center"
'''
class MaterialDesign(MDApp):
def build(self):
return Builder.load_string(KV)
MaterialDesign().run()
So the ScreenManager becomes the root of the App, and its children are the Screens. The on_press property of the MDRaisedButton is now just root.current = "main".

Related

How to add widget by a function in kivymd

Hiii,
I am a beginner in kivymd and python.
I am working on my new app and got stuck at this point.. where i want to add some widgets by using a function but it throws an error..
i don't know what's wrong with it ..
it gave me this error :
File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.9/site-packages/kivy/uix/gridlayout.py", line 307, in on_children raise GridLayoutException( kivy.uix.gridlayout.GridLayoutException: Too many children in GridLayout. Increase rows/cols!
i just want to add one widget but it saying "too many children in GridLayout"
i can't understand what it means
please explain me this.
import kivymd
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivymd.uix.button import MDRaisedButton
from kivymd.uix.screenmanager import MDScreenManager
from kivymd.uix.screen import MDScreen
from kivymd.uix.relativelayout import MDRelativeLayout
from kivymd.uix.card import MDCard
from kivymd.uix.label import MDLabel
from kivy.core.text import LabelBase
kv = '''
bseb_layout:
screen_mngr: screen_mngr
MDScreenManager:
id: screen_mngr
subjects: subjects
chapter_list: chapter_list
#======================================================
#===================subjects===========================
MDScreen:
id: subjects
name: "subjects"
MDLabel:
text: "Question Bank\\n10 Years"
font_size: "30dp"
pos_hint: {"x": 0.1, "y": 0.42}
MDLabel:
text: "Chapter-wise"
font_size: "20dp"
pos_hint: {"x": 0.1, "y": 0.35}
MDCard:
size_hint: 0.45,0.4
pos_hint: {"x":0.025, "y": 0.4}
md_bg_color: 1,0,1,0.2
radius: 25,25,25,25
on_press: app.func()
on_press: root.screen_mngr.current = "chapter_list"
MDRelativeLayout:
MDLabel:
text: "Physics"
font_size: "25dp"
pos_hint: {"x": 0.2, "y": 0}
MDCard:
size_hint: 0.45,0.3
pos_hint: {"x":0.525, "y": 0.5}
md_bg_color: 1,0,1,0.2
radius: 25,25,25,25
on_press: print('Works')
MDRelativeLayout:
MDLabel:
text: "chemistry"
font_size: "25dp"
pos_hint: {"x": 0.2, "y": 0}
MDCard:
size_hint: 0.45,0.3
pos_hint: {"x":0.025, "y": 0.05}
md_bg_color: 1,0,1,0.2
radius: 25,25,25,25
on_press: print('Works')
MDRelativeLayout:
MDLabel:
text: "English"
font_size: "25dp"
pos_hint: {"x": 0.2, "y": 0}
MDCard:
size_hint: 0.45,0.4
pos_hint: {"x":0.525, "y": 0.05}
md_bg_color: 1,0,1,0.2
radius: 25,25,25,25
on_press: print('Works')
MDRelativeLayout:
MDLabel:
text: "Biology"
font_size: "25dp"
pos_hint: {"x": 0.2, "y": 0}
#===================subjects-END=======================
#=================chapter_list=========================
MDScreen:
id: chapter_list
name: "chapter_list"
chapter_grid: chapter_grid
FitImage:
source: "grad3.jpg"
MDLabel:
text: "Phsycis\\nChapters"
font_size: "30dp"
pos_hint: {"x": 0.1, "y": 0.42}
MDScrollView:
id: location
md_bg_color: 1,1,1,0.1
size_hint: 0.95,0.03
pos_hint: {"center_x":0.5,"y": 0.83}
do_scroll_x: True
do_scroll_y: False
radius: 25,25,25,25
bar_color: 214/255, 99/255, 168/255,0.5
bar_inactive_color: 1,1,1,0
MDGridLayout:
id: chapter_grid
adaptive_height: True
adaptive_width: True
cols: 1
rows:1
padding: ("20dp","0dp")
MDCard:
size_hint: None,None
size: "350dp","15dp"
md_bg_color: 1,0,1,0.2
MDRelativeLayout:
MDLabel:
text: "Physics > 2023 > Chapters"
font_size: "15dp"
pos_hint: {"x": 0.0, "center_y": 0.5}
MDScrollView:
md_bg_color: 1,1,1,0.2
size_hint: 0.95,0.8
pos_hint: {"center_x":0.5,"y": 0.02}
do_scroll_x: False
radius: 25,25,25,25
bar_color: 214/255, 99/255, 168/255,0.5
bar_inactive_color: 1,1,1,0
MDGridLayout:
adaptive_height: True
adaptive_width: True
cols: 1
padding: ("11dp","10dp")
spacing: "10dp"
MDCard:
size_hint: None,None
size: "320dp", "60dp"
md_bg_color: 1,0,1,0.2
radius: 25,25,25,25
on_press: print('Works')
MDRelativeLayout:
MDLabel:
text: "Electrostatics"
font_size: "20dp"
pos_hint: {"x": 0.1, "center_y": 0.5}
#==================chapter_list-END====================
'''
class bseb_layout(FloatLayout):
pass
class bseb(MDApp):
def func(self):
self.root.ids.screen_mngr.chapter_list.chapter_grid.add_widget(
MDCard(
MDRelativeLayout(
MDLabel(
text="chapter_2",
font_size="20dp",
theme_text_color="Custom",
text_color=(1,1,1,1),
pos_hint={"x":0.1,"center_y":0.5}
)
),
size_hint=(None,None),
size=("320dp","60dp"),
md_bg_color=(1,1,1,0.2),
radius=(25,25,25,25),
)
)
def build(self):
return Builder.load_string(kv)
if __name__=="__main__":
bseb().run()
Any help is much appreciated...
Thank you.
Your code:
MDGridLayout:
id: chapter_grid
adaptive_height: True
adaptive_width: True
cols: 1
rows:1
Limits that MDGridLayout to exactly one column and one row. That means that it can only have one child widget. If you want to allow more than one row, then remove the rows: 1, or, if you want more than one column, remove the cols: 1. But you cannot remove both. You must specify one of rows: or cols:.

The home_switch function keeps returning that there is no screen with name "home"

from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.core.window import Window
from kivy.config import Config
from kivy.utils import platform
from kivy.properties import ObjectProperty
import sqlite3
from kivymd.uix.fitimage import FitImage
Window.size = (360, 780)
class Start_Screen(Screen):
pass
class Sign_Up(Screen):
def signup(self):
signup_password = self.signup_password.text
signup_password_re = self.signup_password_re.text
if signup_password != "" and signup_password_re != "":
if signup_password == signup_password_re:
print ("correct")
sm.home_switch()
else:
print ("incorrect")
else:
print ("empty")
class Log_In(Screen):
pass
class Home(Screen):
pass
class WindowManager(ScreenManager):
def home_switch(self, *args):
self.current = "home"
sm = WindowManager()
class MainApp(MDApp):
def build(self):
self.theme_cls.primary_palette = "Yellow"
return Builder.load_file('Vocate.kv')
MainApp().run()
and this is the .kv file
WindowManager:
Start_Screen:
name: "start"
Sign_Up:
name: "sign up"
Log_In:
name: "log in"
Home:
name: "home"
<Sign_Up>:
name: "sign up"
id: signup
signup_phone: signup_phone
signup_password: signup_password
signup_password_re: signup_password_re
MDFloatLayout:
Image:
source: 'images/Vocate.png'
size_hint: 0.6, 0.6
pos_hint: {"center_x": 0.2, "center_y": 0.95}
FloatLayout:
cols: 1
MDTextField:
id: signup_phone
hint_text: "Phone Number"
required: True
font_size: 20
size_hint: 0.9, 0.09
pos_hint: {"center_x": 0.5, "center_y": 0.725}
multiline: False
helper_text: "This Field is Required"
helper_text_mode: "on_error"
MDTextField:
id: signup_password
hint_text: "Password"
required: True
password: True
font_size: 20
size_hint: 0.9, 0.09
pos_hint: {"center_x": 0.5, "center_y": 0.625}
multiline: False
helper_text: "This Field is Required"
helper_text_mode: "on_error"
MDTextField:
id: signup_password_re
hint_text: "Password"
required: True
password: True
font_size: 20
size_hint: 0.9, 0.09
pos_hint: {"center_x": 0.5, "center_y": 0.525}
multiline: False
helper_text: "This Field is Required"
helper_text_mode: "on_error"
MDFillRoundFlatButton:
theme_text_color: "Custom"
md_bg_color: 250/255, 205/255, 62/255, 1
font_size: 20
text_color: 254.99/255, 255/255, 255/255, 1
size_hint: 0.9, 0.06
pos_hint: {"center_x": 0.5, "center_y": 0.425}
text: "Sign Up"
bold: True
on_release:
root.signup()
MDFlatButton:
text: "Go Back"
bold: True
theme_text_color: "Custom"
text_color: 250/255, 205/255, 62/255, 1
font_size: 20
size_hint: 0.9, 0.06
pos_hint: {"center_x": 0.5, "center_y": 0.34}
on_release:
app.root.current = "start"
root.manager.transition.direction = "right"
<Home>:
name: 'home'
MDFloatLayout:
cols: 1
MDBoxLayout:
orientation: "vertical"
MDBottomNavigation:
MDBottomNavigationItem:
name: "home screen"
icon: "home"
MDBoxLayout:
orientation: "vertical"
MDToolbar:
title: "Home"
md_bg_color: 250/255, 205/255, 62/255, 1
specific_text_color: 255/255, 255/255, 255/255, 1
MDLabel:
text: "test 1"
halign: "center"
MDBottomNavigationItem:
name: "courses"
icon: "book-open-variant"
MDFloatLayout:
cols: 2
MDBoxLayout:
orientation: "horizontal"
MDBoxLayout:
orientation: "vertical"
MDToolbar:
md_bg_color: 250/255, 205/255, 62/255, 1
FitImage:
source: "images/coding.png"
FitImage:
source: "images/python.png"
FitImage:
source: "images/gsuite.png"
FitImage:
source: "images/english.png"
FitImage:
source: "images/vocate.png"
FitImage:
source: "images/vocate.png"
MDBoxLayout:
orientation: "vertical"
MDToolbar:
md_bg_color: 250/255, 205/255, 62/255, 1
MDLabel:
text: "Coding - 1"
MDLabel:
text: "Coding - 2"
MDLabel:
text: "Gsuite"
MDLabel:
text: "English"
MDLabel:
text: "Woodworking"
MDLabel:
text: "Customer Service"
MDBoxLayout:
orientation: "vertical"
MDToolbar:
title: "Courses"
md_bg_color: 250/255, 205/255, 62/255, 1
specific_text_color: 255/255, 255/255, 255/255, 1
Button:
background_color: 0, 0, 0, 0
Button:
background_color: 0, 0, 0, 0
Button:
background_color: 0, 0, 0, 0
Button:
background_color: 0, 0, 0, 0
Button:
background_color: 0, 0, 0, 0
Button:
background_color: 0, 0, 0, 0
MDBottomNavigationItem:
name: "more"
icon: "dots-horizontal"
MDBoxLayout:
orientation: "vertical"
MDToolbar:
title: "More"
md_bg_color: 250/255, 205/255, 62/255, 1
specific_text_color: 255/255, 255/255, 255/255, 1
MDLabel:
text: "test 3"
halign: "center"
I've tried changing "WindowManager:" to "" but it returns a blank screen. Some questions similar to this I've seen say to remove the builder and rename the file so I've tried "Main.kv" and "MainApp.kv" and both return the same blank screen.
Thanks in advance!
*Note: I've removed some of the code so that it's clearer so some of the screens and some other stuff that won't interfere here aren't included
The main problem is the line sm = WindowManager() is creating the WindowManager before the Vocate.kv file is loaded. As a result, the sm has no Screens.
A way to fix that is to eliminate the sm = WindowManager() line completely, and use the manager property of a Screen to access the WindowManager. Like this:
class Sign_Up(Screen):
def signup(self):
signup_password = self.signup_password.text
signup_password_re = self.signup_password_re.text
if signup_password != "" and signup_password_re != "":
if signup_password == signup_password_re:
print ("correct")
self.manager.home_switch() # use manager property
else:
print ("incorrect")
else:
print ("empty")

Blank White space overlapping KivyMD ImageList from MDToolbar

I am practicing KivyMD ImageList, and the imagelist is been overlayed by a blank space coming from MDBottomAppbar.
Here's what it looks like:
Please how do I remove the white blank space that is above MDBottomAppbar and overlapping Imagelist. Here's my code.. Thanks in Advance!
from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.core.window import Window
Window.size = (330, 500)
kv = '''
<MyTile#SmartTileWithLabel>
size_hint_y: None
height: "240dp"
BoxLayout:
orientation: "vertical"
md_bg_color: (240/255, 240/255, 240/255, 1)
MDToolbar:
id: success_screen_toolbar
title: "Project"
right_action_items: [["progress-check", lambda x: x]]
ScrollView:
size_hint_y: None
size: "280dp", "360dp"
pos_hint: {"center_x": .5, "center_y": .6}
height: root.height - success_screen_toolbar.height - dp(90)
y: root.height - success_screen_toolbar.height - dp(50)
elevation: 8
MDGridLayout:
cols: 1
adaptive_height: True
padding: dp(4), dp(4)
spacing: dp(4)
MyTile:
source: "C:/Users/HP USER/Downloads/bella_baron.jpg"
text: "[size=26]Cat 1[/size]\\n[size=14]cat-1.jpg[/size]"
MyTile:
source: "C:/Users/HP USER/Downloads/bella_baron.jpg"
text: "[size=26]Cat 2[/size]\\n[size=14]cat-2.jpg[/size]"
tile_text_color: app.theme_cls.accent_color
MyTile:
source: "C:/Users/HP USER/Downloads/bella_baron.jpg"
text: "[size=26][color=#ffffff]Cat 3[/color][/size]\\n[size=14]cat-3.jpg[/size]"
tile_text_color: app.theme_cls.accent_color
MDBottomAppBar:
MDToolbar:
id: success_screen_bottomappbar
icon: "magnify"
on_action_button: x
type: 'bottom'
mode: 'center'
elevation: '8dp'
left_action_items: [["calendar-text", lambda x: x], ["account-group", lambda x: x]]
right_action_items: [["magnify", lambda x: x], ["menu", lambda x: x]]
'''
class Main(MDApp):
def build(self):
return Builder.load_string(kv)
Main().run()
Use the following please, I have changed a few things and a few main widgets in your code because they added additional space that was not being used and that caused the white space in the body of the app, so here is the code by jbsidis:
from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
Window.size = (330, 500)
kv = '''
#:import MDTextField kivymd.uix.textfield.MDTextField
<MyTile#SmartTileWithLabel>
size_hint_y: None
height: "240dp"
<S>:
MDTextFieldRound:
pos_hint: {"center_x": .5, "center_y": .95}
normal_color : [1,1,1,.1]
color_active : [1,1,1,1]
size_hint: .8, .07
hint_text : 'Search a product...'
icon_left : 'magnify'
Screen:
FloatLayout:
BoxLayout:
id: m5
pos_hint: {"center_x": .5, "center_y": .371} #this will change if you change this Window.size = (330, 500)
orientation: "vertical"
ScrollView:
MDGridLayout:
cols: 1
adaptive_height: True
padding: dp(4), dp(4)
spacing: dp(4)
MyTile:
source: "Photos/pro.jpg"
text: "[size=26]jbsidis[/size]\\n[size=14]cat-1.jpg[/size]"
MyTile:
source: "Photos/pro.jpg"
text: "[size=26]jbsidis[/size]\\n[size=14]cat-2.jpg[/size]"
tile_text_color: app.theme_cls.accent_color
MyTile:
source: "Photos/pro.jpg"
text: "[size=26][color=#ffffff]jbsidis[/color][/size]\\n[size=14]cat-3.jpg[/size]"
tile_text_color: app.theme_cls.accent_color
MyTile:
source: "a11.png"
text: ""
tile_text_color: [0,0,0,0]
FloatLayout:
AnchorLayout:
pos_hint: {"center_x": .5, "center_y": .9}
MDTextButton:
halign: "center"
text: "\\n\\n\\n\\n\\n\\n\\nLoading more...\\n\\n"
MDSpinner:
size_hint: .1,.1
MDToolbar:
id: success_screen_toolbar
title: "Project"
pos_hint: {"top": 1}
right_action_items: [["progress-check", lambda: print(6)]]
MDBottomAppBar:
MDToolbar:
id: success_screen_bottomappbar
icon: "magnify"
on_action_button: root.add_widget(app.sbar())
type: 'bottom'
mode: 'center'
#elevation: '8dp'
left_action_items: [["calendar-text", lambda: print(6)], ["account-group", lambda: print(6)]]
right_action_items: [["magnify", lambda: print(6)], ["menu", lambda: print(6)]]
'''
class blanks1(BoxLayout):
pass
class S(FloatLayout):
pass
class Main(MDApp):
def build(self):
return Builder.load_string(kv)
def sbar(self):
self.root.ids.success_screen_toolbar.md_bg_color=[1,1,1,1]
return S()
Main().run()
I also added the function to add the search bar on top when we press the magnified glass icon at the bottom bar, I added an empty image at the bottom of the pictures so you can show your app is loading more results, hope this is helpful for you and everybody, greetings from El Salvador, picture (something else, the a11.png image is a blank transparent png image, the last should be an empty image to extend the scroll area otherwise the bottom bar will hide it, this is something that most operating systems implement but we cannot know because we don't see the source code, watch my youtube channel: https://www.youtube.com/channel/UCIMmPyY7XjWHk1AHlR_UdWQ ):

Kivymd on_release Button the action for next step with MDCard does not work

I'm trying to make the button click on_release: app.proximo () have the action to go to the next card MDFloatLayout, but I'm not getting it, could someone help me how could it work?
Below the main.py file, where to start the app, main.kv file, where is the main app and finally the dashboard.kv file where I am calling my card inside the app
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
class DashBoard(Screen):
pass
class FirstScreen(Screen):
pass
class Lavanderia(MDApp):
def build(self):
self.title="Texto Titulo"
self.theme_cls.primary_palette = "LightBlue"
return Builder.load_file("main.kv")
def proximo(self):
self.root.ids.carousel.load_next(mode="proximo")# Próximo Card
def fecharApp(self):
self.stop()# Fecha Aplicativo
Lavanderia().run()
#:import rgba kivy.utils.get_color_from_hex
#: include dashboard.kv
#: include firstscreen.kv
NavigationLayout:
id: nav_layout
ScreenManager:
Screen:
BoxLayout:
orientation:'vertical'
MDToolbar:
title: app.title
elevation:10
left_action_items:[["menu", lambda x: nav_drawer.set_state()]]
ScreenManager:
id: screen_manager
DashBoard:
id:dashboard
name:"dashboard"
FirstScreen:
id:first_screen
name:"first_screen"
MDNavigationDrawer:
id: nav_drawer
BoxLayout:
orientation:'vertical'
padding:"8dp"
spacing:"8dp"
Image:
pos_hint:{"x":.24,"y":.0}
size_hint: None, None
size:dp(146), dp(158)
source:"images/logo.png"
ScrollView:
MDList:
OneLineIconListItem:
text:"Tela 1"
on_release:
screen_manager.current = "dashboard"
nav_drawer.set_state()
IconLeftWidget:
icon:"dishwasher"
OneLineIconListItem:
text:"Tela 2"
on_release:
screen_manager.current = "first_screen"
nav_drawer.set_state()
IconLeftWidget:
icon:"dishwasher"
<DashBoard>:
MDFloatLayout:
MDCard:
size_hint: dp(.45), dp(.8)
pos_hint:{"center_x": .5, "center_y": .5}
Carousel:
id:carousel
MDFloatLayout:
MDTextField:
hint_text:"Texto 1"
size_hint_x:dp(.8)
pos_hint:{"center_x": .5, "center_y": .48}
MDRaisedButton:
text: "Proximo"
size_hint_x:dp(.8)
pos_hint:{"center_x":.5,"center_y":.2}
on_release: app.proximo() # Proximo step
MDFloatLayout:
MDLabel:
text:"Texto Final Conclusão"
theme_text_color:"Custom"
bold:True
pos_hint:{"center_x":.68,"center_y":.5}
font_style:"H5"
MDRaisedButton:
text: "Fechar Aplicativo"
text_color:rgba("#00FF69")
size_hint_x:dp(.8)
pos_hint:{"center_x":.5,"center_y":.4}
md_bg_color:rgba("#333333")
on_release: app.fecharApp() #fechar Aplicativo
You are trying to ger the carrousel through the root screen, but it is inside the dashboard screen.
So you will have to navigate there first, and only then you can call your function.
def proximo(self):
dashboard = self.root.ids.dashboard
carousel = dashboard.ids.carousel
carousel.load_next(mode="proximo")
# Same as
# self.root.ids.dashboard.ids.carousel.load_next(mode="proximo")

How to change text of label in a screen when I press a button in another screen in Kivy

I am new to Kivy, so I have a question that I want to develop two screen in Kivy, the first one is about entering user details and passwords (no authentication, at this moment) and then second screen displays the user name (entered at the previous screen) and displays user name along with other widgets.
The basic idea is to get the value of a user form userlogin screen and display it to another screen.
Here is my code,
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import StringProperty # #UnresolvedImport
class LoginScreen(Screen):
text = StringProperty()
def change_text(self):
self.text = ''
self.manager.current = 'StocksGUIScreen'
class StocksGUIScreen(Screen):
label_text = StringProperty('')
#pass
class PortfolioUIApp(App):
#user_name = StringProperty()
pass
PortfolioUIApp().run()
Here is my .kv file:
ScreenManager:
id: screen_manager
LoginScreen:
id: login_screen
name: 'LoginScreen'
manager: screen_manager
StocksGUIScreen:
id: stocks_gui
name: 'StocksGUIScreen'
manager: screen_manager
label_text: login_screen.text
<LoginScreen>:
BoxLayout:
orientation: 'vertical'
BoxLayout:
Label:
text: "User Name"
size_hint: 0.3,0.2
#pos: root.x, root.top-self.height
color: 0,1,0,1
TextInput:
id: user_name
size_hint: 0.3,0.2
#on_text: root.user_name
BoxLayout:
Label:
text: "Password"
size_hint: 0.3,0.2
TextInput:
id: pass_word
password: True
size_hint: 0.3,0.2
BoxLayout:
Button:
text: 'Log In'
size_hint: 0.3,0.2
on_press:
#root.manager.current = 'StocksGUIScreen'
root.label_text: user_name
root.change_text()
Button:
text: 'Cancel'
size_hint: 0.3,0.2
on_release: app.stop()
<StocksGUIScreen>:
BoxLayout:
orientation: 'vertical'
BoxLayout:
orientation: 'horizontal'
TextInput:
size_hint_x: 50
id: ticker_search
Button:
text: "Go"
size_hint_x: 25
on_press: root.search_stock()
id: search_box
Label:
text: root.label_text
size_hint_x: 25
BoxLayout:
height: "10dp"
size_hint_y: 5
Label:
size_hint_x: .2
size_hint_y: .1
text: "Advice"
color: [1,0,0,1]
text_size: self.size
halign: 'left'
valign: 'top'
ScrollView:
size: self.size
GridLayout:
id: layout_content
size_hint_y: None
cols: 1
row_default_height: '20dp'
row_force_default: True
spacing: 0, 0
padding: 0, 0
color: [1,0,0,1]
Label:
text: "Lorem ipsum dolor sit amet"*10
id: advice_message
text_size: self.size
halign: 'left'
valign: 'top'
BoxLayout:
height: "10dp"
size_hint_y: 10
Label:
size_hint_x: .2
size_hint_y: .1
text: "Graphical Stuff"
id: graphical_stuff
text_size: self.size
halign: 'left'
valign: 'top'
AsyncImage:
source: "abc.png"
id: graphical_stuff
allow_stretch: True
keep_ratio: False
pos: 200,300
size: root.width*0.5,root.height*0.2
BoxLayout:
orientation: 'horizontal'
Button:
text: 'My settings button'
Button:
text: 'Back to Log in screen'
on_press: root.manager.current = 'LoginScreen'
What is wrong in this code, any help/guidance would be highly appreciated plz
If you want this setup, you can set an attribute on the first screen. Access it by id in your second screen.
from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.lang import Builder
from kivy.properties import StringProperty
class Screen1(Screen):
username = StringProperty("")
class Screen2(Screen):
username = StringProperty("")
root = Builder.load_string('''
<Screen1>:
BoxLayout:
orientation: "vertical"
TextInput:
id: username
Button:
text: "Login"
on_release: root.username = username.text; root.manager.current = "screen2"
<Screen2>:
name: "screen2"
Label:
text: root.username
ScreenManager:
Screen1:
id: loginscreen
Screen2:
username: loginscreen.username
''')
class MyApp(App):
def build(self):
return root

Resources