Kivymd not able to keep label and textfield in same row - python

I have created a form with kivymd label and textfield but somehow the labels and the textfields are not in proper alignment with each other. The mdlabel is aligning a little above the row of mdtextfield with pos_hint function. My codes are below:
#.py
from kivymd.app import MDApp
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.screenmanager import Screen
from kivy.properties import *
class Main(Screen):
pass
class MyApp(MDApp):
def build(self):
return Main()
MyApp().run()
#.kv
#:import Vector kivy.vector.Vector
<Main>:
MDNavigationLayout:
ScreenManager:
id: screen_manager
Screen:
name: "challan"
MDBoxLayout:
orientation: 'vertical'
MDToolbar:
title: "My App"
left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]
MDLabel:
text:'Date'
pos_hint: {"center_x": .5, "top": 1}
theme_text_color:'Primary'
MDTextField:
id:date
hint_text: "Date"
pos_hint: {"center_x": .5, "top": 1}
size_hint:(None,None)
width: 300
MDLabel:
text:'Challan No.'
pos_hint: {"center_x": .5, "top": 1}
theme_text_color:'Primary'
MDTextField:
id: challan
hint_text: "Challan No."
pos_hint: {"center_x": .5, "top": 1}
size_hint:(None,None)
width:300
MDLabel:
text:'Quantity'
pos_hint: {"center_x": .5, "top": 1}
theme_text_color:'Primary'
MDTextField:
id:qty
hint_text: "Quantity"
pos_hint: {"center_x": .5, "top": 1}
size_hint:(None,None)
width: 300
MDLabel:
text:'From Location'
pos_hint: {"center_x": .5, "top": 1}
theme_text_color:'Primary'
MDTextField:
id: flo
hint_text: "From Location"
pos_hint: {"center_x": .5, "top": 1}
size_hint:(None,None)
width:300
Screen:
name: "Message"
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: "My App"
left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]
MDLabel:
text:"Email: "
# font_size: (root.width**2 + root.height**2) / 17**4
pos_hint: {"x":0.1, "top":0.9}
size_hint: 0.25, 0.15
MDTextField:
id:namee
hint_text: "Name"
helper_text:"forget"
pos_hint : {'center_x':0.0 ,'center_y':0.0}
size_hint_x : None
width: 300
MDNavigationDrawer:
id:nav_drawer
opening_transition:'out_bounce'
opening_time:1
BoxLayout:
orientation:'vertical'
MDRectangleFlatIconButton:
icon: "android"
size_hint_x: 1
text: 'Android'
on_release:
screen_manager.current = "challan"
MDRectangleFlatIconButton:
icon: "magnify"
size_hint_x: 1
text: 'Search'
on_release:
screen_manager.current = "Message"
Widget:
I want to keep the label and the text field in same row in alignment to each other.

The MDBoxLayout design intent is to place each child in a separate row when orientation is vertical. If you want items aligned horizontally, you can use MDBoxLayout with orientation of horizontal (which is the default).
In your case, one way to accomplish your goal is to put the pairs of MDLabel and MDTextInput each in their own horizontal MDBoxLayout. For example:
Screen:
name: "challan"
MDBoxLayout:
orientation: 'vertical'
MDToolbar:
title: "My App"
left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]
MDBoxLayout:
MDLabel:
text:'Date'
pos_hint: {"center_y": .5}
theme_text_color:'Primary'
MDTextField:
id:date
hint_text: "Date"
pos_hint: {"center_y": .5}
size_hint:(None,None)
width: 300
MDBoxLayout:
MDLabel:
text:'Challan No.'
pos_hint: {"center_y": .5}
theme_text_color:'Primary'
MDTextField:
id: challan
hint_text: "Challan No."
pos_hint: {"center_y": .5}
size_hint:(None,None)
width:300
Similarly, for the rest of the items that you want on their own row.

Related

How should i call a new screen?

I have a ScreenManager problem (Lack of knowledge).
I can't call another screen from a button on my RecycleView.
Note that I have 2 ScreenManager, on the first I login and I am directed to other screens of another ScreenManager.
Note in the line that contains "# <<<< HERE" the way I call the new screen.
import json
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.properties import ObjectProperty
from kivy.uix.relativelayout import RelativeLayout
from random import sample, choice
from string import ascii_lowercase
from kivy.uix.screenmanager import Screen
from kivy.uix.boxlayout import BoxLayout
sample_images = [
"https://cdn.neemo.com.br/uploads/settings_webdelivery/logo/5591/No-Image.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/5/5f/SL_Bundala_NP_asv2020-01_img08.jpg/640px-SL_Bundala_NP_asv2020-01_img08.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Palaeobranchiostoma_hamatotergum.jpg/482px-Palaeobranchiostoma_hamatotergum.jpg",
"https://upload.wikimedia.org/wikipedia/commons/thumb/a/ac/No_image_available.svg/1024px-No_image_available.svg.png",
]
KV = '''
<ContentNavigationDrawer>:
orientation: 'vertical'
FloatLayout:
size_hint_y: None
height: "200dp"
BoxLayout:
id: box_image
x: root.x
pos_hint: {"top": 1}
FloatLayout:
# Imagem de fundo
FitImage:
pos_hint: {'center_x': .5, 'center_y': .5}
source: "./images/menu.png"
# Imagem do usuário
FitImage:
pos_hint: {'center_x': .5, 'center_y': .5}
source: "./images/user.png"
size_hint: None, None
width: dp(150)
height: dp(150)
radius: [99, 99, 99, 99]
MDLabel:
text: "Coletor de dados Postgres"
size_hint_y: None
height: self.texture_size[1]
# Define a posição do Label
x: root.x + 10
y: root.height - box_image.height + dp(10)
ScrollView:
MDList:
OneLineIconListItem:
text: "Tela Inicial"
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = 'tela_item'
IconLeftWidget:
icon: "view-list"
## Tamanho do ícone user_font_size: "72sp"
OneLineIconListItem:
text: "Outra Tela"
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = 'tela_signup'
IconLeftWidget:
icon: "database-export-outline"
OneLineIconListItem:
text: "Imagem maior"
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = 'tela_login'
IconLeftWidget:
icon: "database-export-outline"
<MainScreen>:
MDToolbar:
id: toolbar
pos_hint: {"top": 1}
elevation: 10
title: "Pesquise e compre."
left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]
right_action_items: [['lightbulb-outline', lambda x: app.color()], ['cart', lambda x: app.compras()]]
MDNavigationLayout:
x: toolbar.height
ScreenManager:
id: screen_manager
ScreenRecycleView
TelaLogin
TelaSingUp
TelaItem
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
screen_manager: screen_manager
nav_drawer: nav_drawer
<MyImageCard>:
name: 'my_image_card'
AsyncImage:
source: root.source
Label:
size_hint_y: None
height: dp(48)
text: root.text
color: 'black'
canvas:
Color:
rgba: 0, 0, 0, .3
Rectangle:
size: self.size
MDFlatButton:
text: "New Screen Here"
increment_width: "164dp"
#app.root.ids.sm.get_screen('main').ids.screenmanager.current = 'new_screen'
on_release:
root.screen_manager.current = 'tela_item' # <<<< HERE
<TelaItem>:
name: 'tela_item'
FloatLayout:
orientation: "vertical"
id: body_tela_item
FitImage:
pos_hint: {'center_x': .5, 'center_y': .85}
source: "./images/eu.jpeg"
size_hint: None, None
width: dp(90)
height: dp(90)
radius: [99, 99, 99, 99]
MDRaisedButton:
text: 'Add'
size_hint: .9, None
pos_hint: {'center_x': .5, 'center_y': .5}
on_release: root.singup()
MDRaisedButton:
text: 'Back'
size_hint: .9, None
pos_hint: {'center_x': .5, 'center_y': .3}
on_release: root.singup()
<ScreenRecycleView>:
name: 'recycle_view'
FloatLayout:
MDBoxLayout:
pos_hint: {'center_x': 0.5, 'center_y': 0.4}
RecycleView:
id: rv
viewclass: "MyImageCard"
RecycleGridLayout:
cols: 2
row_default_height: (self.width - self.cols*self.spacing[0]) / self.cols
padding: dp(4), dp(4)
spacing: dp(4)
size_hint_y: None
height: self.minimum_height
default_size_hint: 1, None
default_size: None, dp(500)
<TelaLogin>:
name: 'tela_login'
FloatLayout:
orientation: "vertical"
id: body_tela_login
FitImage:
pos_hint: {'center_x': .5, 'center_y': .85}
source: "./images/eu.jpeg"
size_hint: None, None
width: dp(90)
height: dp(90)
radius: [99, 99, 99, 99]
MDRaisedButton:
text: 'Login'
size_hint: .9, None
pos_hint: {'center_x': .5, 'center_y': .5}
on_release: root.login()
<TelaSingUp>:
name: 'tela_singup'
id: body_tela_singup
FloatLayout:
MDIconButton:
icon: "arrow-left-bold"
pos_hint: {"center_x": .1, "center_y": .9}
on_release: app.root.current = 'tela_login'
FitImage:
pos_hint: {'center_x': .5, 'center_y': .8}
source: "./images/user.png"
size_hint: None, None
width: dp(150)
height: dp(150)
radius: [99, 99, 99, 99]
MDRaisedButton:
text: 'Cadastrar'
size_hint: .9, None
pos_hint: {'center_x': .5, 'center_y': .1}
on_release: root.singup()
ScreenManager:
id: sm:
TelaLogin:
name: 'tela_login'
MainScreen:
name: 'main'
'''
class ContentNavigationDrawer(BoxLayout):
screen_manager = ObjectProperty()
nav_drawer = ObjectProperty()
class MainScreen(Screen):
pass
class TelaSingUp(Screen):
pass
class TelaItem(Screen):
pass
class TelaLogin(Screen):
def login(self):
self.manager.current = 'main'
class MyImageCard(RelativeLayout):
text = StringProperty()
source = StringProperty()
class ScreenRecycleView(Screen):
def on_kv_post(self, base_widget):
self.ids.rv.data = [
{
'source': choice(sample_images),
'text': ''.join(sample(ascii_lowercase, 6))
}
for x in range(50)
]
class MyApp(MDApp):
def build(self):
root = Builder.load_string(KV)
return root
MyApp().run()

How to go back to the previous screen from left_action_items of MDToolbar in KivyMD?

I followed the steps in KivyMD documentation to create a NavigationDrawer, so, I created it and everything work correctely, but the problem is to return to the first screen. I want to go back to the first screen (the Screen with name: 'main') when I click in the arrow-left in the MDToolbar, but nothing that I did worked.
KV File:
<ItemDrawer>:
theme_text_color: 'Custom'
on_release: self.parent.set_color_item(self)
IconLeftWidget:
id: icon
icon: root.icon
theme_text_color: 'Custom'
text_color: root.text_color
<ContentNavigationDrawer>:
orientation: 'vertical'
padding: '8dp'
spacing: '8dp'
AnchorLayout:
anchor_x: 'left'
size_hint_y: .3
ScrollView:
MDList:
OneLineIconListItem:
text: 'Reminders'
on_press:
root.nav_drawer.set_state('close')
root.screen_manager.current = 'RemindersWindow'
IconLeftWidget:
icon: 'bookmark-outline'
OneLineIconListItem:
text: 'To Do'
on_press:
root.nav_drawer.set_state('close')
root.screen_manager.current = 'ToDoWindow'
IconLeftWidget:
icon: 'check-outline'
OneLineIconListItem:
text: 'Settings'
on_press:
root.nav_drawer.set_state('close')
root.screen_manager.current = 'SettingsWindow'
IconLeftWidget:
icon: 'cog-outline'
OneLineIconListItem:
text: 'About'
on_press:
root.nav_drawer.set_state('close')
root.screen_manager.current = 'AboutWindow'
IconLeftWidget:
icon: 'information-outline'
Screen:
MDNavigationLayout:
ScreenManager:
id: screen_manager
Screen:
name: 'main'
MDToolbar:
title: 'Remindy'
elevation: 10
pos_hint: {'top': 1}
left_action_items: [["menu", lambda x: nav_drawer.set_state('open')]]
MainScreen:
Screen:
name: 'SettingsWindow'
FloatLayout:
orientation: 'vertical'
MDToolbar:
title: 'Settings'
elevation: 10
pos_hint: {'top': 1}
left_action_items: [["arrow-left", lambda x: x]]
OneLineIconListItem:
text: 'Themes'
pos_hint: {'center_x': .5, 'y': .8}
on_release: app.picker_theme()
IconLeftWidget
icon: 'palette-outline'
OneLineIconListItem:
text: 'Language'
pos_hint: {'center_x': .5, 'y': .7}
on_release:
IconLeftWidget:
icon: 'earth'
Screen:
name: 'RemindersWindow'
FloatLayout:
orientation: 'vertical'
MDToolbar:
elevation: 10
pos_hint: {'top': 1}
left_action_items: [["arrow-left", lambda x: x]]
MDLabel:
text: 'My Reminders'
halign: 'center'
Screen:
name: 'AboutWindow'
FloatLayout:
orientation: 'vertical'
MDToolbar:
elevation: 10
pos_hint: {'top': 1}
left_action_items: [["arrow-left", lambda x: x]]
MDLabel:
text: 'About'
halign: 'center'
Screen:
name: 'ToDoWindow'
FloatLayout:
orientation: 'vertical'
MDToolbar:
elevation: 10
pos_hint: {'top': 1}
left_action_items: [["arrow-left", lambda x: x]]
MDLabel:
text: 'To-Do'
halign: 'center'
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
screen_manager: screen_manager
nav_drawer: nav_drawer
<MainScreen#FloatLayout>:
name: 'MainScreen'
FloatLayout:
orientation: 'vertical'
MDFloatingActionButton:
icon: 'pencil-plus-outline'
pos_hint: {'x': .8, 'y': .1/2}
on_release: root.datacard()
<CardScreen>:
FloatLayout:
orientation: 'vertical'
MDCard:
size_hint: .8, .1
MDLabel:
id: base_label
text: 'New Reminder'
<CardBox>:
orientation: 'vertical'
size_hint: .7, .5
pos_hint: {'center_x': .5, 'center_y': .5}
MDBoxLayout:
size_hint_y: .5
md_bg_color: app.theme_cls.primary_color
MDLabel:
text: 'Add a Reminder'
font_style: 'H6'
theme_text_color: 'Custom'
text_color: 1, 1, 1, 1
halign: 'center'
MDIconButton:
icon: 'window-close'
on_release: root.closecard()
MDFloatLayout:
MDTextField:
id: title_id
hint_text: 'Title'
max_text_length: 15
required: True
helper_text_mode: 'on_error'
helper_text: 'This Field is Required.'
size_hint_x: .8
pos_hint: {'center_x': .5, 'y': .1}
MDFloatLayout:
orientation: 'horizontal'
MDRectangleFlatIconButton:
icon: 'clock-time-four-outline'
text: 'Hour'
pos_hint: {'x': .1, 'y': .3}
on_release: root.picker_hora()
MDRectangleFlatIconButton:
icon: 'calendar-outline'
text: 'Date'
pos_hint: {'x': .6, 'y': .3}
on_release: root.picker_data()
MDFloatLayout:
MDRectangleFlatButton:
text: 'Add'
size_hint_x: .8
pos_hint: {'center_x': .5, 'y': .2}
Python File:
import pickle
from kivy.app import App
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivymd.uix.toolbar import MDToolbar
from kivy.properties import ObjectProperty
from kivymd.uix.list import StringProperty
from kivymd.theming import ThemableBehavior
from kivymd.uix.list import OneLineIconListItem
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.floatlayout import FloatLayout
from kivymd.uix.picker import MDThemePicker
from kivymd.uix.picker import MDTimePicker
from kivymd.uix.picker import MDDatePicker
from kivymd.uix.list import MDList
from kivymd.uix.card import MDCard
class ItemDrawer(OneLineIconListItem):
icon = StringProperty()
class ContentNavigationDrawer(BoxLayout):
screen_manager = ObjectProperty()
nav_drawer = ObjectProperty()
class MainScreen(FloatLayout):
def datacard(self):
self.add_widget(CardBox())
class CardBox(MDCard):
def closecard(self):
self.parent.remove_widget(self)
def picker_hora(self):
time_dialog = MDTimePicker()
time_dialog.open()
def picker_data(self):
date_dialog = MDDatePicker()
date_dialog.open()
def new_card(self):
# Remove and Create a New Card
self.parent.remove_widget(self)
title = self.ids.title_input.text
self.add_widget(CardScreen())
App.get_running_app().root.ids.base_label.text = title
class CardScreen(FloatLayout):
pass
class ReminderApp(MDApp):
Window.size = (400, 600)
def picker_theme(self):
theme_dialog = MDThemePicker()
theme_dialog.open()
def build(self):
return Builder.load_string(KV)
ReminderApp().run()
You need to trigger some action from the button associated to MDToolbar to switch to another screen.
For simple usage you can use the default function setattr as,
left_action_items: [["arrow-left", lambda *args : setattr(screen_manager, "current", "main")]]
Or for advanced usage you can refer to a function from some relevant class say, the App's subclass, as,
left_action_items: [["arrow-left", app.go_home]]
Now in that method,
def go_home(self, *args):
# Access the `ScreenManager` and change the screen.
self.root.ids.screen_manager.current = "main"
# Some other actions.
Also as a side note, you should change the line <MainScreen#FloatLayout> in kvlang with just <MainScreen> as you already defined it in .py. Otherwise a warning may be raised in log.

How to align the top row properly, I tried a few different things not working... image given below

.KV code part
MDGridLayout:
cols: 4
adaptive_height: True
#md_bg_color: app.theme_cls.primary_color
MDIcon:
halign: "center"
icon: 'magnify'
pos_hint: {"x": .00}
size_hint:
MDTextField:
id: symbol_field
hint_text: " Search by STOCK KEY"
pos_hint: {"x": .05}
on_text_validate: root.Pressed()
required: True
helper_text_mode: "on_error"
helper_text: ""
MDIconButton:
halign: "center"
icon: "arrow-right-thick"
pos_hint: {"x": .90}
on_press: root.Pressed()
MDIconButton: #new
halign: 'center'
icon: 'cog'
pos_hint: {'x': .95}
on_press: root.settings()
The weird alignment not able to change it The top bar
It should be like:
'label' [0-0.05] 'text-field' [0.05-0.90] 'button' [0.90-0.95] 'button' [0.95-1]
You can try this one, it is not using MDGridLayout (the nature of the Grid, alingment is really hard to do), in this example I am using FloatLayouts plus BoxLayouts plus canvas:
Screen:
canvas:
Color:
rgba: [0,0,0,.1]
Rectangle:
pos: self.pos
size: self.size
#START
FloatLayout:
BoxLayout:
pos_hint: {"center_x": .5, "center_y": 1.4}
padding: dp(15)
#Control the background here jbsidis
canvas.after:
Color:
rgba: [1,1,1,1]
RoundedRectangle:
pos: self.pos[0],self.pos[1] #-dp(15)
size: self.size[0],dp(80)
radius: [dp(8),dp(8),dp(8),dp(8)]
#source: "jbsidis_background.png"
BoxLayout:
pos_hint: {"center_x": .4, "center_y": 1.0}
size_hint: .8,None
spacing: dp(3)
MDIconButton:
pos_hint: {"center_x": .1, "center_y": .25}
halign: "center"
icon: 'magnify'
FloatLayout:
BoxLayout:
pos_hint: {"center_x": .7, "center_y": .5}
MDTextField:
id: symbol_field
hint_text: " Search by STOCK KEY"
#on_text_validate: root.Pressed()
required: True
helper_text_mode: "on_error"
helper_text: ""
MDIconButton:
halign: "center"
icon: "arrow-right-thick"
#on_press: root.Pressed()
MDIconButton:
halign: 'center'
icon: 'pencil'
#on_press: root.settings()
Here is the image (the one in the left side):

Python Kivymd - AttributeError object has no attribute

Not sure where I'm going wrong, When changing the value of MDSlider it should update a label but I receive the error
the kv is loaded as a string within my py file, if I test out the slider by simply printing the value to the console it works. but when I try to update a label get the following error??
AttributeError: 'Level' object has no attribute 'training_level'
here's my code
from kivy.lang import Builder
from kivymd.uix.screen import MDScreen
from kivymd.app import MDApp
KV = """
ScreenManager:
Profile:
Level:
Routine:
Start:
<Profile>:
name: "Profile"
canvas.before:
Color:
rgba: (1,1,1,1)
Rectangle:
source: "bg.jpg"
size: root.width, root.height
pos: self.pos
MDLabel:
text: "Select a Profile"
font_size: "32"
pos_hint: {"center_x": .5, "center_y": .95}
MDRectangleFlatButton:
text: "Guest"
font_size: "32"
pos_hint: {"center_x": .5, "center_y": .5}
on_release: app.root.current = "Level"
MDBottomAppBar:
MDToolbar:
title: "HIIT"
icon: "account-cog"
type: "bottom"
left_action_items: [["menu", lambda x: x]]
mode: "center"
<Level>:
name: "Level"
canvas.before:
Color:
rgba: (1,1,1,1)
Rectangle:
source: "bg.jpg"
size: root.width, root.height
pos: self.pos
MDLabel:
text: "What is your Level of training?"
font_size: "32"
pos_hint: {"center_x": .5, "center_y": .95}
MDLabel:
id: "training_level"
theme_text_color: "Custom"
text_color: 1,1,1,1
halign: "center"
text: "2"
font_size: "128"
pos_hint: {"center_x": .5, "center_y": .65}
MDSlider:
min: 1
max: 3
value: 2
size: 500, 50
size_hint: None, None
hint: False
step: 1
orientation: "horizontal"
pos_hint: {"center_x": .5, "center_y": .40}
on_value: root.change_height(*args)
MDRectangleFlatButton:
text: "Next"
font_size: "32"
pos_hint: {"center_x": .92, "center_y": .18}
on_release: app.root.current = "Routine"
MDBottomAppBar:
MDToolbar:
title: "HIIT"
icon: "account-cog"
type: "bottom"
left_action_items: [["menu", lambda x: x]]
mode: "center"
<Routine>:
name: "Routine"
canvas.before:
Color:
rgba: (1,1,1,1)
Rectangle:
source: "bg.jpg"
size: root.width, root.height
pos: self.pos
MDLabel:
text: "Select a Routine"
font_size: "32"
pos_hint: {"center_x": .5, "center_y": .95}
MDRectangleFlatButton:
text: "10 Minute Beginner"
font_size: "32"
pos_hint: {"center_x": .5, "center_y": .5}
on_release: app.root.current = "Start"
MDBottomAppBar:
MDToolbar:
title: "HIIT"
icon: "account-cog"
type: "bottom"
left_action_items: [["menu", lambda x: x]]
mode: "center"
<Start>:
name: "Start"
canvas.before:
Color:
rgba: (1,1,1,1)
Rectangle:
source: "bg.jpg"
size: root.width, root.height
pos: self.pos
MDRectangleFlatButton:
text: "Stop"
font_size: "32"
pos_hint: {"center_x": .5, "center_y": .5}
on_release: app.root.current = "Profile"
MDBottomAppBar:
MDToolbar:
title: "HIIT"
icon: "account-cog"
type: "bottom"
left_action_items: [["menu", lambda x: x]]
mode: "center"
"""
class Profile(MDScreen):
pass
class Level(MDScreen):
def change_height(self, *args):
#print(str(round(args[1],1)))
self.training_level.text = str(round(args[1],1))
pass
class Routine(MDScreen):
pass
class Start(MDScreen):
pass
class MainApp(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Blue"
return Builder.load_string(KV)
MainApp().run()

How do I use a variable for the text in an MDLabel?

I'm trying to put the username that I input from an MDTextField into a MDLabel text. In my case, the MDLabel is in a MDNavigationDrawer that comes after the user presses the submit button and goes to the next screen.
All I could think of so far is declaring usn as a string property in the submit screen:
class SubmitScreen(Screen):
usn = StringProperty()
then created a method to set usn to the input of the MDTextfield in my app:
def getname(self):
usn = (self.root.get_screen("menu").ids.userdata.text)
then I set the text in my MDLabel to root.usn
MDLabel:
text: root.usn
font_style: 'Subtitle1'
size_hint_y:None
height: self.texture_size[1]
When I go to the next screen, MDLabel is blank. It doesn't give me an error but it doesn't show what I typed in the MDTextField, it's simply blank.
This is my code in full:
screen_helper = """
ScreenManager:
MenuScreen:
SubmitScreen:
<MenuScreen>:
name: 'menu'
MDTextField:
id : userdata
hint_text: "Enter Username"
helper_text: "or click on forgot username"
#helper_text_mode: "persistent"
helper_text_mode: "on_focus"
icon_right: "account"
icon_right_color: app.theme_cls.primary_color
pos_hint : {'center_x': 0.5, 'center_y': 0.4}
size_hint_x:None
width:250
mode : "rectangle"
MDTextField:
id: userpass
hint_text: "Enter Password"
helper_text: "or click on forgot Password"
#helper_text_mode: "persistent"
password : True
helper_text_mode: "on_focus"
icon_right: "eye-off"
icon_right_color: app.theme_cls.primary_color
pos_hint : {'center_x': 0.5, 'center_y': 0.3}
size_hint_x:None
width:250
mode : "rectangle"
MDLabel:
text: "Company X"
pos_hint: {'center_x':0.5, 'center_y':0.7}
font_style: 'H3'
halign: 'center'
MDRectangleFlatButton:
text: 'Submit'
pos_hint: {'center_x':0.5, 'center_y':0.2}
font_size : 20
on_press:
app.getname()
app.root.current = 'submit'
<SubmitScreen>:
name: 'submit'
NavigationLayout:
ScreenManager:
Screen:
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: "Company X"
left_action_items: [["menu", lambda x : nav_drawer.toggle_nav_drawer()]]
elevation: 11
Widget:
MDFillRoundFlatIconButton:
spacing: '8dp'
padding: '20dp'
text: 'Menu'
pos_hint: {'center_x':0.5, 'center_y':0.9}
icon: 'account-child'
on_press: root.manager.current = 'menu'
MDFillRoundFlatIconButton:
text: 'View Account'
pos_hint: {'center_x':0.5, 'center_y':1}
font_size : 20
icon: 'account-cash'
on_press:
MDBottomAppBar:
md_bg_color: 0, 1, 0, 1
MDToolbar:
title: "by Tech Company"
left_action_items: [["coffee", lambda x : app.navigation_draw0()]]
right_action_items: [["clock", lambda x : app.navigation_draw1()]]
mode: 'free-end'
elevation: 13
type: 'bottom'
icon: 'account-check'
MDNavigationDrawer:
id: nav_drawer
BoxLayout:
orientation: 'vertical'
spacing: '8dp'
padding: '12dp'
Image:
source: 'img.jpg'
MDLabel:
text: root.usn
font_style: 'Subtitle1'
size_hint_y:None
height: self.texture_size[1]
MDLabel:
text: 'email#gmail.com'
font_style: 'Caption'
size_hint_y:None
height: self.texture_size[1]
ScrollView:
MDList:
#OneLineListItem:
OneLineIconListItem:
text: 'Profile'
IconLeftWidget:
icon: 'face-profile'
OneLineIconListItem:
text: 'Upload'
IconLeftWidget:
icon: 'upload'
OneLineIconListItem:
text: 'Logout'
conLeftWidget:
icon: 'logout'
MDRectangleFlatButton:
text: 'Menu'
pos_hint: {'center_x':0.5, 'center_y':0.4}
on_press: root.manager.current = 'menu'
MDRectangleFlatButton:
text: 'View Account'
pos_hint: {'center_x':0.5, 'center_y':0.3}
on_press:
"""
class MenuScreen(Screen):
pass
class SubmitScreen(Screen):
usn = StringProperty()
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(SubmitScreen(name='submit'))
sm.add_widget(UploadScreen(name='upload'))
sm.add_widget(AccountScreen(name='account'))
class DemoApp(MDApp):
def build(self):
screen = Builder.load_string(screen_helper)
self.theme_cls.primary_palette = "Purple"
self.theme_cls.accent_palette = 'Blue'
self.theme_cls.primary_hue = "200"
self.theme_cls.theme_style = "Dark"
return screen
def getname(self):
usn = (self.root.get_screen("menu").ids.userdata.text)
DemoApp().run()
Set an ID to the MDLabel and then in the getname function change the MDLabel text to the input of the MDTextField.
In kv:
MDLabel:
text: "Text before"
id: label
In python:
def getname(self):
usn = (self.root.get_screen("menu").ids.userdata.text)
self.root.ids.label.text = usn

Categories