I am trying to restructure the kivymd project file but I am not able to place the navigation bar at the place at which it is normally. I cannot see why this is happening (see pictures and code below).
This is how it looks like
and this is how it should look like
I am using the three files app.py, app.kv and labels.py (for injecting labels from a *.py file).
# app.kv
# -*- coding: utf-8 -*-
import os
import kivy.app
from kivy.lang import Builder
from application.kivymd.list import BaseListItem
from application.kivymd.material_resources import DEVICE_TYPE
from application.kivymd.navigationdrawer import MDNavigationDrawer, NavigationDrawerHeaderBase
from application.kivymd.theming import ThemeManager
# User defined imports
from kivy.core.window import Window
Window.fullscreen = "auto"
class HackedDemoNavDrawer(MDNavigationDrawer):
# DO NOT USE
def add_widget(self, widget, index=0):
if issubclass(widget.__class__, BaseListItem):
self._list.add_widget(widget, index)
if len(self._list.children) == 1:
widget._active = True
self.active_item = widget
# widget.bind(on_release=lambda x: self.panel.toggle_state())
widget.bind(on_release=lambda x: x._set_active(True, list=self))
elif issubclass(widget.__class__, NavigationDrawerHeaderBase):
self._header_container.add_widget(widget)
else:
super(MDNavigationDrawer, self).add_widget(widget, index)
class MainApp(kivy.app.App):
theme_cls = ThemeManager()
title = "Application"
def build(self):
main_widget = Builder.load_file(
os.path.join(os.path.dirname(__file__), "./app.kv")
)
# self.theme_cls.theme_style = 'Dark'
main_widget.ids.text_field_error.bind(
on_text_validate=self.set_error_message,
on_focus=self.set_error_message)
self.bottom_navigation_remove_mobile(main_widget)
return main_widget
def bottom_navigation_remove_mobile(self, widget):
# Removes some items from bottom-navigation demo when on mobile
if DEVICE_TYPE == 'mobile':
widget.ids.bottom_navigation_demo.remove_widget(widget.ids.bottom_navigation_desktop_2)
if DEVICE_TYPE == 'mobile' or DEVICE_TYPE == 'tablet':
widget.ids.bottom_navigation_demo.remove_widget(widget.ids.bottom_navigation_desktop_1)
def set_error_message(self, *args):
if len(self.root.ids.text_field_error.text) == 2:
self.root.ids.text_field_error.error = True
else:
self.root.ids.text_field_error.error = False
def on_pause(self):
return True
def on_stop(self):
pass
if __name__ == '__main__':
MainApp().run()
This is the app.kv file which is injected into the app.py file
# app.kv
#:import Toolbar application.kivymd.toolbar.Toolbar
#:import MDNavigationDrawer application.kivymd.navigationdrawer.MDNavigationDrawer
#:import NavigationLayout application.kivymd.navigationdrawer.NavigationLayout
#:import NavigationDrawerDivider application.kivymd.navigationdrawer.NavigationDrawerDivider
#:import NavigationDrawerToolbar application.kivymd.navigationdrawer.NavigationDrawerToolbar
#:import NavigationDrawerSubheader application.kivymd.navigationdrawer.NavigationDrawerSubheader
#:import MDCheckbox application.kivymd.selectioncontrols.MDCheckbox
#:import MDSwitch application.kivymd.selectioncontrols.MDSwitch
#:import MDTextField application.kivymd.textfields.MDTextField
#:import MDThemePicker application.kivymd.theme_picker.MDThemePicker
#:import labels application.labels
NavigationLayout:
id: nav_layout
MDNavigationDrawer:
id: nav_drawer
NavigationDrawerToolbar:
title: labels.NAVIGATION
NavigationDrawerIconButton:
icon: 'checkbox-blank-circle'
text: labels.DASHBOARD
on_release: app.root.ids.scr_mngr.current = 'dashboard'
NavigationDrawerIconButton:
icon: 'checkbox-blank-circle'
text: labels.SYSTEM_INSPECTOR
on_release: app.root.ids.scr_mngr.current = 'system_inspector'
NavigationDrawerIconButton:
icon: 'checkbox-blank-circle'
text: labels.SYSTEM_PARAMETERS
on_release: app.root.ids.scr_mngr.current = 'system_parameters'
BoxLayout:
orientation: 'vertical'
halign: "center"
Toolbar:
id: toolbar
title: labels.APPLICATION_NAME
md_bg_color: app.theme_cls.primary_color
background_palette: 'Primary'
background_hue: '500'
left_action_items: [['menu', lambda x: app.root.toggle_nav_drawer()]]
#right_action_items: [['dots-vertical', lambda x: app.root.toggle_nav_drawer()]]
ScreenManager:
id: scr_mngr
Screen:
name: 'dashboard'
MDLabel:
font_style: 'Body1'
theme_text_color: 'Primary'
text: "This is the dashboard!"
size_hint_x:None
width: '250dp'
halign: "center"
pos_hint: {"center_x": 0.50, "center_y": 0.75}
MDCheckbox:
id: grp_chkbox_1
group: 'test'
size_hint: None, None
size: dp(48), dp(48)
pos_hint: {'center_x': 0.25, 'center_y': 0.5}
MDCheckbox:
id: grp_chkbox_2
group: 'test'
size_hint: None, None
size: dp(48), dp(48)
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
MDSwitch:
size_hint: None, None
size: dp(36), dp(48)
pos_hint: {'center_x': 0.75, 'center_y': 0.5}
_active: False
Screen:
name: 'system_inspector'
MDLabel:
font_style: 'Body1'
theme_text_color: 'Primary'
text: "This is the system_inspector page!"
size_hint_x:None
width: '250dp'
halign: "center"
pos_hint: {"center_x": 0.50, "center_y": 0.75}
MDCheckbox:
id: grp_chkbox_1
group: 'test'
size_hint: None, None
size: dp(48), dp(48)
pos_hint: {'center_x': 0.25, 'center_y': 0.5}
MDCheckbox:
id: grp_chkbox_2
group: 'test'
size_hint: None, None
size: dp(48), dp(48)
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
MDSwitch:
size_hint: None, None
size: dp(36), dp(48)
pos_hint: {'center_x': 0.75, 'center_y': 0.5}
_active: False
Screen:
name: 'system_parameters'
BoxLayout:
orientation: "horizontal"
BoxLayout:
orientation: 'vertical'
size_hint_y: None
height: self.minimum_height
padding: dp(48)
spacing: 10
MDTextField:
hint_text: "No helper text"
input_filter: "float"
MDTextField:
hint_text: "Helper text on focus"
helper_text: "This will disappear when you click off"
helper_text_mode: "on_focus"
input_filter: "int"
MDTextField:
hint_text: "No helper text"
MDTextField:
hint_text: "Helper text on focus"
helper_text: "This will disappear when you click off"
helper_text_mode: "on_focus"
BoxLayout:
orientation: 'vertical'
size_hint_y: None
height: self.minimum_height
padding: dp(48)
spacing: 10
MDTextField:
hint_text: "No helper text"
MDTextField:
hint_text: "Helper text on focus"
helper_text: "This will disappear when you click off"
helper_text_mode: "on_focus"
MDTextField:
hint_text: "No helper text"
MDTextField:
hint_text: "Helper text on focus"
helper_text: "This will disappear when you click off"
helper_text_mode: "on_focus"
Screen:
name: 'textfields'
ScrollView:
BoxLayout:
orientation: 'vertical'
size_hint_y: None
height: self.minimum_height
padding: dp(48)
spacing: 10
MDTextField:
hint_text: "No helper text"
MDTextField:
hint_text: "Helper text on focus"
helper_text: "This will disappear when you click off"
helper_text_mode: "on_focus"
MDTextField:
hint_text: "Persistent helper text"
helper_text: "Text is always here"
helper_text_mode: "persistent"
MDTextField:
id: text_field_error
hint_text: "Helper text on error (Hit Enter with two characters here)"
helper_text: "Two is my least favorite number"
helper_text_mode: "on_error"
MDTextField:
hint_text: "Max text length = 10"
max_text_length: 10
MDTextField:
hint_text: "required = True"
required: True
helper_text_mode: "on_error"
MDTextField:
multiline: True
hint_text: "Multi-line text"
helper_text: "Messages are also supported here"
helper_text_mode: "persistent"
MDTextField:
hint_text: "color_mode = \'accent\'"
color_mode: 'accent'
MDTextField:
hint_text: "color_mode = \'custom\'"
color_mode: 'custom'
helper_text_mode: "on_focus"
helper_text: "Color is defined by \'line_color_focus\' property"
line_color_focus: self.theme_cls.opposite_bg_normal # This is the color used by the textfield
MDTextField:
hint_text: "disabled = True"
disabled: True
Screen:
name: 'nav_drawer'
HackedDemoNavDrawer:
# NavigationDrawerToolbar:
# title: "Navigation Drawer Widgets"
NavigationDrawerIconButton:
icon: 'checkbox-blank-circle'
text: "Badge text ---->"
badge_text: "99+"
NavigationDrawerIconButton:
active_color_type: 'accent'
text: "Accent active color"
NavigationDrawerIconButton:
active_color_type: 'custom'
text: "Custom active color"
active_color: [1, 0, 1, 1]
NavigationDrawerIconButton:
use_active: False
text: "Use active = False"
NavigationDrawerIconButton:
text: "Different icon"
icon: 'alarm'
NavigationDrawerDivider:
NavigationDrawerSubheader:
text: "NavigationDrawerSubheader"
NavigationDrawerIconButton:
text: "NavigationDrawerDivider \/"
NavigationDrawerDivider:
Here is the labels.py file for injecting the labels into the kv file.
# labels.py
APPLICATION_NAME = "Application"
NAVIGATION = "Navigation"
DASHBOARD = "Dashboard"
SYSTEM_INSPECTOR = "System Inspector"
SYSTEM_PARAMETERS = "System Parameters"
Solution
You might have a kv file with a name, main.kv
With KivyMD installed on my machine, and some minor changes, the KivyMD App ran fine.
Example
app.py
# app.py
# -*- coding: utf-8 -*-
import os
from kivy.app import App
from kivy.lang import Builder
from kivymd.list import BaseListItem
from kivymd.material_resources import DEVICE_TYPE
from kivymd.navigationdrawer import MDNavigationDrawer, NavigationDrawerHeaderBase
from kivymd.theming import ThemeManager
# User defined imports
from kivy.core.window import Window
# Window.fullscreen = "auto"
class HackedDemoNavDrawer(MDNavigationDrawer):
# DO NOT USE
def add_widget(self, widget, index=0):
if issubclass(widget.__class__, BaseListItem):
self._list.add_widget(widget, index)
if len(self._list.children) == 1:
widget._active = True
self.active_item = widget
# widget.bind(on_release=lambda x: self.panel.toggle_state())
widget.bind(on_release=lambda x: x._set_active(True, list=self))
elif issubclass(widget.__class__, NavigationDrawerHeaderBase):
self._header_container.add_widget(widget)
else:
super(MDNavigationDrawer, self).add_widget(widget, index)
class MainApp(App):
theme_cls = ThemeManager()
title = "Application"
def build(self):
main_widget = Builder.load_file(
os.path.join(os.path.dirname(__file__), "./app.kv")
)
# self.theme_cls.theme_style = 'Dark'
main_widget.ids.text_field_error.bind(
on_text_validate=self.set_error_message,
on_focus=self.set_error_message)
self.bottom_navigation_remove_mobile(main_widget)
return main_widget
def bottom_navigation_remove_mobile(self, widget):
# Removes some items from bottom-navigation demo when on mobile
if DEVICE_TYPE == 'mobile':
widget.ids.bottom_navigation_demo.remove_widget(widget.ids.bottom_navigation_desktop_2)
if DEVICE_TYPE == 'mobile' or DEVICE_TYPE == 'tablet':
widget.ids.bottom_navigation_demo.remove_widget(widget.ids.bottom_navigation_desktop_1)
def set_error_message(self, *args):
if len(self.root.ids.text_field_error.text) == 2:
self.root.ids.text_field_error.error = True
else:
self.root.ids.text_field_error.error = False
def on_pause(self):
return True
def on_stop(self):
pass
if __name__ == '__main__':
MainApp().run()
labels.py
# labels.py
APPLICATION_NAME = "Application"
NAVIGATION = "Navigation"
DASHBOARD = "Dashboard"
SYSTEM_INSPECTOR = "System Inspector"
SYSTEM_PARAMETERS = "System Parameters"
app.kv
# app.kv
#:kivy 1.11.0
#:import Toolbar kivymd.toolbar.Toolbar
#:import MDNavigationDrawer kivymd.navigationdrawer.MDNavigationDrawer
#:import NavigationLayout kivymd.navigationdrawer.NavigationLayout
#:import NavigationDrawerDivider kivymd.navigationdrawer.NavigationDrawerDivider
#:import NavigationDrawerToolbar kivymd.navigationdrawer.NavigationDrawerToolbar
#:import NavigationDrawerSubheader kivymd.navigationdrawer.NavigationDrawerSubheader
#:import MDCheckbox kivymd.selectioncontrols.MDCheckbox
#:import MDSwitch kivymd.selectioncontrols.MDSwitch
#:import MDTextField kivymd.textfields.MDTextField
#:import MDThemePicker kivymd.theme_picker.MDThemePicker
#:import labels labels
NavigationLayout:
id: nav_layout
MDNavigationDrawer:
id: nav_drawer
NavigationDrawerToolbar:
title: labels.NAVIGATION
NavigationDrawerIconButton:
icon: 'checkbox-blank-circle'
text: labels.DASHBOARD
on_release: app.root.ids.scr_mngr.current = 'dashboard'
NavigationDrawerIconButton:
icon: 'checkbox-blank-circle'
text: labels.SYSTEM_INSPECTOR
on_release: app.root.ids.scr_mngr.current = 'system_inspector'
NavigationDrawerIconButton:
icon: 'checkbox-blank-circle'
text: labels.SYSTEM_PARAMETERS
on_release: app.root.ids.scr_mngr.current = 'system_parameters'
BoxLayout:
orientation: 'vertical'
halign: "center"
Toolbar:
id: toolbar
title: labels.APPLICATION_NAME
md_bg_color: app.theme_cls.primary_color
background_palette: 'Primary'
background_hue: '500'
left_action_items: [['menu', lambda x: app.root.toggle_nav_drawer()]]
#right_action_items: [['dots-vertical', lambda x: app.root.toggle_nav_drawer()]]
ScreenManager:
id: scr_mngr
Screen:
name: 'dashboard'
MDLabel:
font_style: 'Body1'
theme_text_color: 'Primary'
text: "This is the dashboard!"
size_hint_x:None
width: '250dp'
halign: "center"
pos_hint: {"center_x": 0.50, "center_y": 0.75}
MDCheckbox:
id: grp_chkbox_1
group: 'test'
size_hint: None, None
size: dp(48), dp(48)
pos_hint: {'center_x': 0.25, 'center_y': 0.5}
MDCheckbox:
id: grp_chkbox_2
group: 'test'
size_hint: None, None
size: dp(48), dp(48)
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
MDSwitch:
size_hint: None, None
size: dp(36), dp(48)
pos_hint: {'center_x': 0.75, 'center_y': 0.5}
_active: False
Screen:
name: 'system_inspector'
MDLabel:
font_style: 'Body1'
theme_text_color: 'Primary'
text: "This is the system_inspector page!"
size_hint_x:None
width: '250dp'
halign: "center"
pos_hint: {"center_x": 0.50, "center_y": 0.75}
MDCheckbox:
id: grp_chkbox_1
group: 'test'
size_hint: None, None
size: dp(48), dp(48)
pos_hint: {'center_x': 0.25, 'center_y': 0.5}
MDCheckbox:
id: grp_chkbox_2
group: 'test'
size_hint: None, None
size: dp(48), dp(48)
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
MDSwitch:
size_hint: None, None
size: dp(36), dp(48)
pos_hint: {'center_x': 0.75, 'center_y': 0.5}
_active: False
Screen:
name: 'system_parameters'
BoxLayout:
orientation: "horizontal"
BoxLayout:
orientation: 'vertical'
size_hint_y: None
height: self.minimum_height
padding: dp(48)
spacing: 10
MDTextField:
hint_text: "No helper text"
input_filter: "float"
MDTextField:
hint_text: "Helper text on focus"
helper_text: "This will disappear when you click off"
helper_text_mode: "on_focus"
input_filter: "int"
MDTextField:
hint_text: "No helper text"
MDTextField:
hint_text: "Helper text on focus"
helper_text: "This will disappear when you click off"
helper_text_mode: "on_focus"
BoxLayout:
orientation: 'vertical'
size_hint_y: None
height: self.minimum_height
padding: dp(48)
spacing: 10
MDTextField:
hint_text: "No helper text"
MDTextField:
hint_text: "Helper text on focus"
helper_text: "This will disappear when you click off"
helper_text_mode: "on_focus"
MDTextField:
hint_text: "No helper text"
MDTextField:
hint_text: "Helper text on focus"
helper_text: "This will disappear when you click off"
helper_text_mode: "on_focus"
Screen:
name: 'textfields'
ScrollView:
BoxLayout:
orientation: 'vertical'
size_hint_y: None
height: self.minimum_height
padding: dp(48)
spacing: 10
MDTextField:
hint_text: "No helper text"
MDTextField:
hint_text: "Helper text on focus"
helper_text: "This will disappear when you click off"
helper_text_mode: "on_focus"
MDTextField:
hint_text: "Persistent helper text"
helper_text: "Text is always here"
helper_text_mode: "persistent"
MDTextField:
id: text_field_error
hint_text: "Helper text on error (Hit Enter with two characters here)"
helper_text: "Two is my least favorite number"
helper_text_mode: "on_error"
MDTextField:
hint_text: "Max text length = 10"
max_text_length: 10
MDTextField:
hint_text: "required = True"
required: True
helper_text_mode: "on_error"
MDTextField:
multiline: True
hint_text: "Multi-line text"
helper_text: "Messages are also supported here"
helper_text_mode: "persistent"
MDTextField:
hint_text: "color_mode = \'accent\'"
color_mode: 'accent'
MDTextField:
hint_text: "color_mode = \'custom\'"
color_mode: 'custom'
helper_text_mode: "on_focus"
helper_text: "Color is defined by \'line_color_focus\' property"
line_color_focus: self.theme_cls.opposite_bg_normal # This is the color used by the textfield
MDTextField:
hint_text: "disabled = True"
disabled: True
Screen:
name: 'nav_drawer'
HackedDemoNavDrawer:
# NavigationDrawerToolbar:
# title: "Navigation Drawer Widgets"
NavigationDrawerIconButton:
icon: 'checkbox-blank-circle'
text: "Badge text ---->"
badge_text: "99+"
NavigationDrawerIconButton:
active_color_type: 'accent'
text: "Accent active color"
NavigationDrawerIconButton:
active_color_type: 'custom'
text: "Custom active color"
active_color: [1, 0, 1, 1]
NavigationDrawerIconButton:
use_active: False
text: "Use active = False"
NavigationDrawerIconButton:
text: "Different icon"
icon: 'alarm'
NavigationDrawerDivider:
NavigationDrawerSubheader:
text: "NavigationDrawerSubheader"
NavigationDrawerIconButton:
text: "NavigationDrawerDivider \/"
NavigationDrawerDivider:
Output
Related
I created an app with a NavigationDrawer 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: 'Screen_*principal') when I click in the arrow-left in the MDTopAppBar in Login_*screen but nothing that I did worked.
KV file:
#: import RiseInTransition kivy.uix.screenmanager.RiseInTransition
#: import SlideTransition kivy.uix.screenmanager.SlideTransition
<CustomizeScreen#MDBoxLayout>
orientation: 'vertical'
text: ''
padding: 100
MDLabel:
text: root.text
halign: 'center'
pos_hint: {'center_x': .5}
MDFillRoundFlatButton:
text: 'Regresar'
pos_hint: {'center_x': .5}
on_release:
app.root.current = 'screen_principal'
app.root.transition = SlideTransition(direction= 'left' )
<DrawerClickableItem#MDNavigationDrawerItem>
focus_color: "#e7e4c0"
text_color: "#4a4939"
icon_color: "#4a4939"
ripple_color: "#c5bdd2"
selected_color: "#0c6c4d"
<DrawerLabelItem#MDNavigationDrawerItem>
text_color: "#4a4939"
icon_color: "#4a4939"
focus_behavior: False
selected_color: "#4a4939"
_no_ripple_effect: True
<Screen_principal>:
MDNavigationLayout:
MDScreen:
MDTopAppBar:
title: "Navigation Drawer"
elevation: 4
pos_hint: {"top": 1}
md_bg_color: "#e7e4c0"
specific_text_color: "#4a4939"
left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]
MDNavigationDrawer:
id: nav_drawer
radius: (0, 16, 16, 0)
MDNavigationDrawerMenu:
MDNavigationDrawerHeader:
title: "LOCATE YOU"
source: "icono.png"
spacing: "4dp"
padding: "12dp", 0, 0, "56dp"
DrawerClickableItem:
icon: "login"
text_right_color: "#4a4939"
text: "Iniciar Sesion"
on_press:
root.manager.current = "Login_screen"
DrawerClickableItem:
icon: "new-box"
text: "Registrarse"
text_right_color: "#4a4939"
on_press:
root.current = "screen_register"
DrawerClickableItem:
icon: "information-outline"
text: "Informacion"
text_right_color: "#4a4939"
on_press:
root.current = "scr1"
DrawerClickableItem:
icon: "microsoft-teams"
text: "Equipo"
text_right_color: "#4a4939"
on_press:
root.current = "scr1"
<Login_screen>:
MDBoxLayout:
orientation: 'vertical'
MDTopAppBar:
title: "Login"
md_bg_color: "#e7e4c0"
left_action_items: [["arrow-left", lambda *args : setattr(screen_manager, "current", "Screen_principal")]]
MDBoxLayout:
orientation: 'vertical'
size_hint_y: 0.3
Image:
source: 'usuario.png'
pos_hint: {'center_x': 0.5}
MDLabel:
text: 'Iniciar Sesion'
halign: 'center'
color: '#FF5A1E'
font_size: '35dp'
bold: True
pos_hint: {'center_y': .5}
MDBoxLayout:
orientation: 'vertical'
size_hint_y: 0.3
spacing: '12dp'
MDTextField:
id: user
hint_text: "Nombre de usuario"
icon_right: "account"
font_size: '25dp'
icon_right_color: '#FF5A1E'
pos_hint: {'center_x': 0.5}
MDTextField:
id: password
password:True
icon_right: "key-variant"
font_size: '25dp'
icon_right_color: '#FF5A1E'
hint_text: "Contrasena"
pos_hint: {'center_x': 0.5}
MDLabel:
id: signal_login
text: ''
halign: 'center'
color: '#FF5A1E'
font_size: '15dp'
MDBoxLayout:
orientation: 'vertical'
size_hint_y:0.4
spacing: '25dp'
MDFillRoundFlatButton:
id: bt_ingresar
text: 'Ingresar'
pos_hint: {'center_x':0.5}
on_release:
x = app.login_data()
root.current = 'open_screen' if x == True else None
MDFillRoundFlatButton:
md_bg_color: 1,1,1,1
Python file:
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager
from kivy.lang import Builder
from kivy.uix.gridlayout import GridLayout
from kivy.core.window import Window
import requests
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.properties import BooleanProperty, ObjectProperty
Builder.load_file('diseno.kv')
class Screen_principal(Screen):
pass
class Login_screen(Screen):
pass
class TestApp(MDApp):
def build(self):
# Create the screen manager
sm = ScreenManager()
sm.add_widget(Screen_principal(name='Screen_principal'))
sm.add_widget(Login_screen(name='Login_screen'))
return sm
if __name__ == '__main__':
TestApp().run()
I tried a lot of things but nothing work, if someone could fix it I would be very grateful
In your kv, change:
left_action_items: [["arrow-left", lambda *args : setattr(screen_manager, "current", "Screen_principal")]]
to:
left_action_items: [["arrow-left", lambda *args : setattr(root.manager, "current", "Screen_principal")]]
This change accesses the ScreenManager by referencing root.manager, and the root in that context is the Login_screen.
I'm building a registration form screen in Kivy. I have some other basic screens as part of the nav bar. However, this is the first one I'm really adding some content to. I have some round text fields that I've just thrown on there to get an idea of spacing, but currently when I reopen the NAV the bar the text field buttons appear in front of it and I want it to be behind.
from kivy import Config
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.image import Image
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivymd.app import MDApp
from kivy.uix.boxlayout import BoxLayout
from kivymd.theming import ThemableBehavior
from kivymd.uix.navigationdrawer import MDNavigationLayout, MDNavigationDrawer
from kivymd.uix.list import MDList, OneLineAvatarListItem, ILeftBody
from kivy.properties import ObjectProperty
from kivy.uix.widget import Widget
Config.set('input', 'mouse', 'mouse,disable_multitouch')
class DrawerList(ThemableBehavior, MDList, Screen):
pass
class NavLayout(MDNavigationLayout):
pass
class Login(Screen):
nav_drawer = ObjectProperty()
class Nav(Screen):
Login = ObjectProperty()
ScreenManager = ObjectProperty
class Homepage(Screen):
Login = ObjectProperty()
nav_drawer = ObjectProperty()
class AddNewCustomer(Screen):
nav_drawer = ObjectProperty()
class AddNewStaff(Screen):
pass
class ScreenManager(ScreenManager):
Login = ObjectProperty()
class ContentNavigationDrawer(BoxLayout):
pass
class MainApp(MDApp):
def build(self):
# Window.maximize()
# self.theme_cls.theme_style = "Light"
# self.theme_cls.primary_hue = "900"
login = Builder.load_file("login.kv")
return login
if __name__ == "__main__":
MainApp().run()
ScreenManager:
Login:
Homepage:
AddNewCustomer:
Nav:
DrawerList:
<Login>:
name: "login"
MDCard:
size_hint: None, None
size: 700, 800
pos_hint: {"center_x": 0.5, "center_y": 0.5}
elevation: 10
padding: 25
spacing: 25
orientation: 'vertical'
Image:
source: 'images/asteria.png'
size: 100, 200
MDLabel:
id: login
font_size: 40
halign: 'center'
size_hint_y: None
height: self.texture_size[1]
padding_y: 15
MDTextFieldRound:
id: user
hint_text: "username"
icon_right: "account"
size_hint_x: None
width: 200
font_size: 18
pos_hint: {"center_x": 0.5}
MDTextFieldRound:
id: password
hint_text: "password"
icon_right: "eye-off"
size_hint_x: None
width: 200
font_size: 18
pos_hint: {"center_x": 0.5}
password: True
MDRoundFlatButton:
text: "LOGIN"
font_size: 12
pos_hint: {"center_x": 0.5}
on_release:
root.manager.current = "homepage"
root.manager.transition.direction = "left"
MDRoundFlatButton:
text: "CLEAR"
font_size: 12
pos_hint: {"center_x": 0.5}
on_press: app.clear()
Widget:
size_hint_y: None
height: 50
<Nav>:
ScrollView:
MDList:
OneLineListItem:
text: "Add New Customer"
on_release:
root.nav_drawer.set_state("close")
root.screen_manager.current = "addNewCustomer"
root.screen_manager.transition.direction = "right"
OneLineListItem:
text: "Add New Staff"
font_size: 15
halign: "center"
on_release:
root.nav_drawer.set_state("close")
root.screen_manager.current = "addNewStaff"
OneLineListItem:
text: "Case List"
font_size: 15
halign: "center"
on_release:
root.nav_drawer.set_state("close")
root.screen_manager.current = "caseList"
OneLineListItem:
text: "Create Case"
font_size: 15
halign: "center"
on_release:
root.nav_drawer.set_state("close")
root.screen_manager.transition.direction = "left"
root.screen_manager.current = "Create Case"
OneLineListItem:
text: "Log Out"
font_size: 15
halign: "center"
on_release:
root.nav_drawer.set_state("close")
root.manager.current = "login"
root.manager.transition.direction = "right"
<Homepage>:
name: "homepage"
Screen:
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: 'Navigation'
left_action_items: [['menu', lambda x: nav_drawer.set_state('toggle')]]
right_action_items: [["images/asteria.png", lambda x: None]]
elevation:5
Widget:
MDNavigationDrawer:
id: nav_drawer
Nav:
screen_manager: screen_manager
nav_drawer: nav_drawer
manager: root.manager
MDNavigationLayout:
ScreenManager:
id: screen_manager
Screen:
name: "Homepage"
MDLabel:
text: "Welcome Home"
halign: "center"
Screen:
name: "addNewCustomer"
MDLabel:
text: "Add New customer"
halign: "center"
Screen:
name: "addNewStaff"
MDLabel:
text: "Add New Staff"
halign: "center"
Screen:
name: "caseList"
MDLabel:
text: "Case List"
halign: "center"
Screen:
name: "casePool"
MDLabel:
text: "Case Pool"
halign: "center"
Screen:
name: "Create Case"
MDTextFieldRound:
id: user
hint_text: "Case Title"
icon_right: "Case Title"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.875, "center_x": 0.155}
MDTextFieldRound:
id: user2
hint_text: "e.g. Matthew Inwards School of Swag"
mode:"rectangle"
icon_right: "Customer"
size_hint_x: None
width: 275
font_size: 14
pos_hint: {"top": 0.779, "center_x": 0.155}
Change the position of the navigation bar in the homepage:
ScreenManager:
Login:
Homepage:
AddNewCustomer:
Nav:
DrawerList:
<Login>:
name: "login"
MDCard:
size_hint: None, None
size: 700, 800
pos_hint: {"center_x": 0.5, "center_y": 0.5}
elevation: 10
padding: 25
spacing: 25
orientation: 'vertical'
Image:
source: 'images/asteria.png'
size: 100, 200
MDLabel:
id: login
font_size: 40
halign: 'center'
size_hint_y: None
height: self.texture_size[1]
padding_y: 15
MDTextFieldRound:
id: user
hint_text: "username"
icon_right: "account"
size_hint_x: None
width: 200
font_size: 18
pos_hint: {"center_x": 0.5}
MDTextFieldRound:
id: password
hint_text: "password"
icon_right: "eye-off"
size_hint_x: None
width: 200
font_size: 18
pos_hint: {"center_x": 0.5}
password: True
MDRoundFlatButton:
text: "LOGIN"
font_size: 12
pos_hint: {"center_x": 0.5}
on_release:
root.manager.current = "homepage"
root.manager.transition.direction = "left"
MDRoundFlatButton:
text: "CLEAR"
font_size: 12
pos_hint: {"center_x": 0.5}
on_press: app.clear()
Widget:
size_hint_y: None
height: 50
<Nav>:
ScrollView:
MDList:
OneLineListItem:
text: "Add New Customer"
on_release:
root.nav_drawer.set_state("close")
root.screen_manager.current = "addNewCustomer"
root.screen_manager.transition.direction = "right"
OneLineListItem:
text: "Add New Staff"
font_size: 15
halign: "center"
on_release:
root.nav_drawer.set_state("close")
root.screen_manager.current = "addNewStaff"
OneLineListItem:
text: "Case List"
font_size: 15
halign: "center"
on_release:
root.nav_drawer.set_state("close")
root.screen_manager.current = "caseList"
OneLineListItem:
text: "Create Case"
font_size: 15
halign: "center"
on_release:
root.nav_drawer.set_state("close")
root.screen_manager.transition.direction = "left"
root.screen_manager.current = "Create Case"
OneLineListItem:
text: "Log Out"
font_size: 15
halign: "center"
on_release:
root.nav_drawer.set_state("close")
root.manager.current = "login"
root.manager.transition.direction = "right"
<Homepage>:
name: "homepage"
Screen:
MDNavigationLayout:
ScreenManager:
id: screen_manager
Screen:
name: "Homepage"
MDLabel:
text: "Welcome Home"
halign: "center"
Screen:
name: "addNewCustomer"
MDLabel:
text: "Add New customer"
halign: "center"
Screen:
name: "addNewStaff"
MDLabel:
text: "Add New Staff"
halign: "center"
Screen:
name: "caseList"
MDLabel:
text: "Case List"
halign: "center"
Screen:
name: "casePool"
MDLabel:
text: "Case Pool"
halign: "center"
Screen:
name: "Create Case"
MDTextFieldRound:
id: user
hint_text: "Case Title"
icon_right: "Case Title"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.875, "center_x": 0.155}
MDTextFieldRound:
id: user2
hint_text: "e.g. Matthew Inwards School of Swag"
mode:"rectangle"
icon_right: "Customer"
size_hint_x: None
width: 275
font_size: 14
pos_hint: {"top": 0.779, "center_x": 0.155}
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: 'Navigation'
left_action_items: [['menu', lambda x: nav_drawer.set_state('open')]]
right_action_items: [["images/asteria.png", lambda x: None]]
elevation:5
Widget:
MDNavigationDrawer:
id: nav_drawer
Nav:
screen_manager: screen_manager
nav_drawer: nav_drawer
manager: root.manager
How can I change the mouse pointer to 'hand' when hovering over a button present in a screen along with a different widget in kivy python?
I tried creating two functions in a python file. But it changes the mouse cursor for the entire page. How can I change the cursor for just the button present on the page?
#Kivy design file
<Loginpage>:
name: "login"
Image:
source: 'background.jpg'
allow_stretch: True
keep_ratio: False
Screen:
MDCard:
size_hint: None,None
size: 400, 550
pos_hint: {"center_x":0.5,"center_y":0.5}
elevation: 10
padding: 25
spacing: 25
orientation: 'vertical'
MDLabel:
id: welcome_label
text: "Welcome"
font_size: 40
halign: 'center'
size_hint_y: None
height: self.texture_size[1]
padding_y:15
font_style: 'H2'
MDTextField:
id: uid
hint_text: "Username"
icon_right: "account"
size_hint_x: None
width: 200
font_size: 20
pos_hint: {"center_x": 0.5}
MDTextField:
id: pwd
hint_text: "Password"
icon_right: "lock"
size_hint_x: None
width: 200
font_size: 20
pos_hint: {"center_x": 0.5}
password: True
MDRoundFlatButton:
text: "LOG IN"
font_size: 12
pos_hint: {"center_x": 0.5}
on_press: root.logger()
MDRoundFlatButton:
text: "CLEAR"
font_size: 12
pos_hint: {"center_x": 0.5}
on_press: root.clear()
MDLabel:
id: login_label
text: "Invalid Login"
font_size: 14
halign: 'center'
size_hint_y: None
height: self.texture_size[1]
pos_hint: {"center_x": 0.5}
padding_y:15
theme_text_color:'Custom'
text_color: 1,0,0,1
Widget:
size_hint_y: None
height: 10
Python File:
class Loginpage(Screen):
def enter(self, *args):
Window.set_system_cursor('hand')
class Loginpage(Screen):
def enter(self, *args):
Window.set_system_cursor('hand')
def leave(self, *args):
Window.set_system_cursor('arrow')
def logger(self):
'''code to login'''
I'm stuck in this problem, I'm using a dialog so that the user can change his password but i cant access the textfield text from the DashboardGerente class...
main.py
class Content(BoxLayout):
old_pass_tf = ObjectProperty()
new_pass_tf = ObjectProperty()
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.app = MDApp.get_running_app()
def show_passnew(self,widget):
if widget.state == "normal":
self.ids.new_pass.password = True
else:
self.ids.new_pass.password = False
def show_passold(self,widget):
if widget.state == "normal":
self.ids.old_pass.password = True
else:
self.ids.old_pass.password = False
class DashboardGerente(Screen):
dialog = None
cont = Content()
def Logout(self):
wm = MDApp.get_running_app().root
wm.current = "Login"
def teste(self):
print(Login.username)
def show_alert_dialog(self):
if not self.dialog:
self.dialog = MDDialog(
title = "Alterar palavra-passe",
text = "Palavra-passe antiga",
content_cls=Content(),
type="custom",
buttons=[
MDFlatButton(
text="Cancelar",
text_color=rgba(0,0,0,1),
on_release = self.cancelar
),
MDFlatButton(
text="Alterar",
text_color = rgba(0,0,0,1),
on_release = self.change_pass
),
],
)
self.dialog.open()
def cancelar(self,obj):
self.dialog.dismiss()
def change_pass(self, obj):
#i want to access the textfield text here
pass
.kv file
<Content>:
orientation: "vertical"
spacing: "12dp"
size_hint_y: None
height: "120dp"
id: content_dialog_change_pass
MDTextField:
id: old_pass
hint_text: "Palavra-passe antiga"
mode: "rectangle"
required: False
color: 1, 0, 1, 1
line_color_focus: 0.5, 0, 1, 1
icon_right: "shield-key"
size_hint: .9, None
password: True
GridLayout:
cols:2
MDCheckbox:
on_press: root.show_passold(self)
size_hint: None, None
height: 5
width: 20
MDLabel:
text: "Mostrar Palavra-passe"
font_size: "13px"
color: 0,0,0,.4
MDTextField:
id: new_pass
hint_text: "Palavra-passe nova"
mode: "rectangle"
required: False
color: 1, 0, 1, 1
line_color_focus: 0.5, 0, 1, 1
icon_right: "shield-key"
size_hint: .9, None
password: True
GridLayout:
cols:2
MDCheckbox:
on_press: root.show_passnew(self)
size_hint: None, None
height: 10
width: 20
MDLabel:
text: "Mostrar Palavra-passe"
font_size: "13px"
color: 0,0,0,.4
<DashboardGerente#FloatLayout>:
name: "DashboardGerente"
MDBottomNavigation:
panel_color: get_color_from_hex("#c300ff")
text_color: 0,0,0,1
MDBottomNavigationItem: ## Perfil do utilizador
name: 'Profile'
text: 'Perfil'
icon: 'account'
badge_icon: "numeric-10"
MDCard:
border_radius: 20
radius: [15]
size_hint:None, None
size: 300, 400
pos_hint: {"center_x": 0.5, "center_y": 0.5}
elevation: 10
orientation: "vertical"
AnchorLayout:
anchor_x: "left"
anchor_y: "top"
MDIconButton:
id:logout
text_color: 0,0,0,1
on_release: root.Logout(), root.teste()
icon: "account-arrow-left"
GridLayout:
cols: 2
padding: 40,0,0,0
MDLabel:
text: "ID: "
MDLabel:
text: "5000"
MDLabel:
text: "Nome: "
MDLabel:
text: "nome"
MDLabel:
text: "Cargo: "
MDLabel:
text: "cargo"
MDLabel:
text: "Salário: "
MDLabel:
text: "1000"
MDLabel:
text: "Secção: "
MDLabel:
text: "Frutaria"
MDRaisedButton:
text: "Mudar palavra-passe"
md_bg_color: 0.5, 0, 1, 1
on_press: root.teste()
pos_hint: {"center_x": 0.5, "center_y": 0.5}
on_release: root.show_alert_dialog()
MDBottomNavigationItem:
name: 'Providers'
text: 'Fornecedores'
icon: 'truck'
MDLabel:
text: 'Mail'
halign: 'center'
MDBottomNavigationItem:
name: 'Products'
text: 'Produtos'
icon: 'shopping'
badge_icon: "numeric-10"
MDLabel:
text: 'Mail'
halign: 'center'
MDBottomNavigationItem:
name: 'Employes'
text: 'Empregados'
icon: 'account-multiple'
badge_icon: "numeric-10"
MDRaisedButton:
text: "Adicionar funcionários"
md_bg_color: 0.5, 0, 1, 1
pos_hint: {"center_x": 0.5, "center_y": 0.5}
MDBottomNavigationItem:
name: 'Sections'
text: 'Secções'
icon: 'storefront'
badge_icon: "numeric-10"
MDLabel:
text: 'Mail'
halign: 'center'
This are my project files that matter for now, how can i get the .text value from the old_pass textfield in the DashboardGerente class?
I would suggest as follows,
Create a property, say, user_input = StringProperty("") in your App's subclass. then in kv lang of Content class,
MDTextField:
id: old_pass
on_text: app.user_input = self.text
Now use this in any method of DashboardGerente as MDApp.get_running_app().user_input
I am currently building a mobile app with Python and KivyMD. On my application I have an MDExpansionPanel which is called in the on_pre_enter method of the ScreenManager. Naturally this will cause that every time I enter to the same page, the widget will be drawn once again.
Is there a way to delete the previous widget before drawing the new one? I've tried several approaches however I have had no success.
The code for a minimal reproducible example is the following:
Python code:
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.app import MDApp
from kivymd.uix.expansionpanel import MDExpansionPanel, MDExpansionPanelOneLine
from kivy.uix.boxlayout import BoxLayout
class MyContentAliment(BoxLayout):
pass
class MyContentCasetas(BoxLayout):
pass
class MyContentGasolina(BoxLayout):
pass
class MyContentHosped(BoxLayout):
pass
class MyContentVarios(BoxLayout):
pass
class LoginWindow(Screen):
pass
class TravelManagerWindow(Screen):
panel_container = ObjectProperty(None)
# EXPANSION PANEL PARA SOLICITAR GV
def set_expansion_panel(self):
# FOOD PANEL
self.ids.panel_container.add_widget(MDExpansionPanel(icon="food", content=MyContentAliment(),
panel_cls=MDExpansionPanelOneLine(text="Alimentacion")))
# CASETAS PANEL
self.ids.panel_container.add_widget(MDExpansionPanel(icon="food", content=MyContentCasetas(),
panel_cls=MDExpansionPanelOneLine(text="Casetas")))
# GAS PANEL
self.ids.panel_container.add_widget(MDExpansionPanel(icon="food", content=MyContentGasolina(),
panel_cls=MDExpansionPanelOneLine(text="Gasolina")))
# HOSPEDAJE PANEL
self.ids.panel_container.add_widget(MDExpansionPanel(icon="food", content=MyContentHosped(),
panel_cls=MDExpansionPanelOneLine(text="Hospedaje")))
# VARIOS PANEL
self.ids.panel_container.add_widget(MDExpansionPanel(icon="food", content=MyContentVarios(),
panel_cls=MDExpansionPanelOneLine(text="Varios")))
### WINDOW MANAGER ################################
class WindowManager(ScreenManager):
pass
ScreenManager().add_widget(LoginWindow(name='login'))
ScreenManager().add_widget(TravelManagerWindow(name='travelManager'))
class reprodExample(MDApp):
def build(self):
self.theme_cls.primary_palette = "Teal"
return WindowManager()
if __name__ == "__main__":
reprodExample().run()
Kivy code:
:
LoginWindow:
TravelManagerWindow:
<LoginWindow>:
name: 'login'
MDRaisedButton:
text: 'Enter'
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
size_hint: None, None
on_release:
root.manager.transition.direction = 'up'
root.manager.current = 'travelManager'
<TravelManagerWindow>:
name:'travelManager'
on_pre_enter: root.set_expansion_panel()
MDRaisedButton:
text: 'Back'
pos_hint: {'center_x': 0.5, 'center_y': 0.85}
size_hint: None, None
on_release:
root.manager.transition.direction = 'down'
root.manager.current = 'login'
BoxLayout:
orientation: 'vertical'
size_hint:1,0.85
pos_hint: {"center_x": 0.5, "center_y":0.37}
adaptive_height:True
height: self.minimum_height
ScrollView:
adaptive_height:True
GridLayout:
size_hint_y: None
cols: 1
row_default_height: root.height*0.10
height: self.minimum_height
BoxLayout:
adaptive_height: True
orientation: 'horizontal'
GridLayout:
id: panel_container
size_hint_x: 0.6
cols: 1
adaptive_height: True
BoxLayout:
size_hint_x: 0.05
MDCard:
id: resumen_solicitud
size_hint: None, None
size: "250dp", "300dp"
pos_hint: {"top": 0.9, "center_x": .5}
elevation: 0.1
MDBoxLayout:
orientation: 'vertical'
canvas.before:
Color:
rgba: 0.8, 0.8, 0.8, 1
Rectangle:
pos: self.pos
size: self.size
MDLabel:
text: 'Monto Total Solicitado'
font_style: 'Button'
halign: 'center'
font_size: (root.width**2 + root.height**2) / 15.5**4
size_hint_y: 0.2
MDSeparator:
height: "1dp"
MDTextField:
id: suma_solic_viaje
text: "$ 0.00"
bold: True
line_color_normal: app.theme_cls.primary_color
halign: "center"
size_hint_x: 0.8
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
MDSeparator:
height: "1dp"
BoxLayout:
id: expense_graph
halign: 'center'
<MyContentAliment>:
adaptive_height: True
MDBoxLayout:
orientation:'horizontal'
adaptive_height:True
size_hint_x:self.width
pos_hint: {"center_x":0.5, "center_y":0.5}
spacing: dp(10)
padding_horizontal: dp(10)
MDLabel:
text: 'Monto:'
multiline: 'True'
halign: 'center'
pos_hint: {"x":0, "top":0.5}
size_hint_x: 0.15
font_style: 'Button'
font_size: 19
MDTextField:
id: monto_aliment_viaje
hint_text: 'Monto a solicitar'
pos_hint: {"x":0, "top":0.5}
halign: 'left'
size_hint_x: 0.3
helper_text: 'Ingresar el monto a solicitar'
helper_text_mode: 'on_focus'
write_tab: False
required: True
MDRaisedButton:
id: boton_aliment_viaje
pos_hint: {"x":0, "top":0.5}
text:'Ingresar Gasto'
### CASETAS
<MyContentCasetas>:
adaptive_height: True
MDBoxLayout:
orientation:'horizontal'
adaptive_height:True
size_hint_x:self.width
pos_hint: {"center_x":0.5, "center_y":0.5}
spacing: dp(10)
padding_horizontal: dp(10)
MDLabel:
text: 'Monto:'
multiline: 'True'
halign: 'center'
pos_hint: {"x":0, "top":0.5}
size_hint_x: 0.15
font_style: 'Button'
font_size: 19
MDTextField:
id: monto_casetas_viaje
hint_text: 'Monto a solicitar'
pos_hint: {"x":0, "top":0.5}
halign: 'left'
size_hint_x: 0.3
helper_text: 'Ingresar el monto a solicitar'
helper_text_mode: 'on_focus'
write_tab: False
input_filter: 'float'
required: True
MDRaisedButton:
id: boton_casetas_viaje
pos_hint: {"x":0, "top":0.5}
text:'Ingresar Gasto'
BoxLayout:
size_hint_x: 0.05
### GASOLINA
<MyContentGasolina>:
adaptive_height: True
MDBoxLayout:
orientation:'horizontal'
adaptive_height:True
size_hint_x:self.width
pos_hint: {"center_x":0.5, "center_y":0.5}
spacing: dp(10)
padding_horizontal: dp(10)
MDLabel:
text: 'Monto:'
multiline: 'True'
halign: 'center'
pos_hint: {"x":0, "top":0.5}
size_hint_x: 0.15
font_style: 'Button'
font_size: 19
MDTextField:
id: monto_gas_viaje
hint_text: 'Monto a solicitar'
pos_hint: {"x":0, "top":0.5}
halign: 'left'
size_hint_x: 0.3
helper_text: 'Ingresar el monto a solicitar'
helper_text_mode: 'on_focus'
write_tab: False
input_filter: 'float'
required: True
MDRaisedButton:
id: boton_gas_viaje
pos_hint: {"x":0, "top":0.5}
text:'Ingresar Gasto'
BoxLayout:
size_hint_x: 0.05
Thanks a lot in advance.