I have already made a login button that takes the user to the main homepage screen. However, I am designing a registration form and I am struggling with being making a submit button that clears all the textfield and creates a pop up notifying the user that they have submitted. I don't need it to be functional as of yet I just need to have something to show what the process will eventually look like.
I'm really new to Kivy and KivyMD and so I'm sure this is quite an easy thing to do, but I am struggling with it - any help would be hugely appreciated.
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:
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"
MDLabel:
text: "Case Title"
font_size: 15
size_hint_x: None
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.875, "center_x": 0.115}
MDTextFieldRound:
id: CaseTitle
hint_text: "Info Here"
icon_right: "Case Title"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.863, "center_x": 0.155}
MDLabel:
text: "Customer"
font_size: 15
size_hint_x: None
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.791, "center_x": 0.115}
MDTextFieldRound:
id: Customer
hint_text: "Info Here"
mode:"rectangle"
icon_right: "Customer"
size_hint_x: None
width: 275
font_size: 14
pos_hint: {"top": 0.779, "center_x": 0.155}
MDLabel:
text: "Customer Forename"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.707, "center_x": 0.115}
MDTextFieldRound:
id: Customer Forename
hint_text: "Info here"
mode:"rectangle"
icon_right: "Customer"
size_hint_x: None
width: 137.5
font_size: 14
pos_hint: {"top": 0.687, "center_x": 0.121}
MDLabel:
text: "Customer Surname"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.707, "center_x": 0.210}
MDTextFieldRound:
id: Customer Forename
hint_text: "Info Here"
mode:"rectangle"
icon_right: "Customer"
size_hint_x: None
width: 137.5
font_size: 14
pos_hint: {"top": 0.687, "center_x": 0.210}
MDLabel:
text: "Alternative email"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.620, "center_x": 0.115}
MDTextFieldRound:
id: Alternative Email
hint_text: "Info Here"
mode:"rectangle"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.600, "center_x": 0.155}
MDLabel:
text: "Alternative Phone"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.533, "center_x": 0.115}
MDTextFieldRound:
id: Alternative phone
hint_text: "Info Here"
mode:"rectangle"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.513, "center_x": 0.155}
MDLabel:
text: "Case Source"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.449, "center_x": 0.115}
MDTextFieldRound:
id: Case Source
hint_text: "Info Here"
mode:"rectangle"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.437, "center_x": 0.155}
MDLabel:
text: "Case Source"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.449, "center_x": 0.115}
MDTextFieldRound:
id: Case Source
hint_text: "Info Here"
mode:"rectangle"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.437, "center_x": 0.155}
MDLabel:
text: "Case Priority"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.365, "center_x": 0.115}
MDTextFieldRound:
id: Case Priority
hint_text: "Info Here"
mode:"rectangle"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.353, "center_x": 0.155}
MDLabel:
text: "Case Category"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.281, "center_x": 0.115}
MDTextFieldRound:
id: Case Category
hint_text: "Info Here"
mode:"rectangle"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.269, "center_x": 0.155}
MDLabel:
text: "Case Description"
font_size: 15
size_hint_x: None
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.883, "center_x": 0.360}
MDTextFieldRound:
id: CaseDescription
hint_text: "Info Here"
icon_right: "Case Title"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.863, "center_x": 0.400}
MDLabel:
text: "Possible Cases"
font_size: 15
size_hint_x: None
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.799, "center_x": 0.360}
MDTextFieldRound:
id: PossibleCases
hint_text: "Info Here"
icon_right: "Case Title"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.780, "center_x": 0.400}
MDLabel:
text: "Knowledge Helper"
font_size: 15
size_hint_x: None
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.715, "center_x": 0.360}
MDTextFieldRound:
id: KnowledgeHelper
hint_text: "Info Here"
icon_right: "Case Title"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.697, "center_x": 0.400}
MDRoundFlatButton:
text: "Submit"
font_size: 12
pos_hint: {"center_y": 0.400,"center_x": 0.5}
on_release:
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
In your kv, just call a method that does the Popup.
Define the method in your Homepage class:
class Homepage(Screen):
Login = ObjectProperty()
nav_drawer = ObjectProperty()
def submit(self):
Popup(title='Submitted', content=Label(text='submitted'), size_hint=(0.5, 0.5)).open()
sm = self.ids.screen_manager
scr = sm.get_screen('Create Case')
for child in scr.children:
if isinstance(child, TextInput):
child.text = ''
and call it from kv:
MDRoundFlatButton:
text: "Submit"
font_size: 12
pos_hint: {"center_y": 0.400,"center_x": 0.5}
on_release: root.submit()
Related
Still struggling with this drop-down list I am trying to make for my create case page. Keep running into error after error. A very kind person has been helping me and fixed the last error I had, but I now have another. Again, I know what it's in reference to roughly - but I don't understand why my Homepage class has no attribute 'screen'.
It is likely to be something very obvious, but I can't see it.
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.textinput import TextInput
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, OneLineIconListItem
from kivy.properties import ObjectProperty
from kivymd.uix.menu import MDDropdownMenu
from kivy.metrics import dp
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 IconListItem(OneLineIconListItem):
icon = StringProperty()
class Homepage(Screen):
Login = ObjectProperty()
nav_drawer = ObjectProperty()
def __init__(self, **kw):
super().__init__(**kw)
self.menu = None
self.menu_items = None
def submit(self):
Popup(title='Submitted', content=Label(text='Case has been submitted and will be reviewed'),
size_hint=(0.5, 0.5)).open()
sm = self.ids.screen_manager
scr = sm.get_screen('Create Case')
for child in scr.children:
if isinstance(child, TextInput):
child.text = ''
def dropdown(self):
self.menu_items = [
{
"viewclass": "IconListItem",
"icon": "git",
"text": f"Item {i}",
"height": dp(56),
"on_release": lambda x=f"Item {i}": self.set_item(x),
} for i in range(5)
]
self.menu = MDDropdownMenu(
caller=self.screen.ids.drop_item,
items=self.menu,
position="center",
width_mult=4,
)
self.menu.bind()
def set_item(self, text_item):
self.screen.ids.drop_item.set_item(text_item)
self.menu.dismiss()
def build(self):
return self.screen
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: "Create Case Version 2.0"
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 Version 2.0"
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"
MDLabel:
text: "Case Title"
font_size: 15
size_hint_x: None
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.875, "center_x": 0.115}
MDTextFieldRound:
id: CaseTitle
hint_text: "Info Here"
icon_right: "Case Title"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.863, "center_x": 0.155}
MDLabel:
text: "Customer"
font_size: 15
size_hint_x: None
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.791, "center_x": 0.115}
MDTextFieldRound:
id: Customer
hint_text: "Info Here"
mode:"rectangle"
icon_right: "Customer"
size_hint_x: None
width: 275
font_size: 14
pos_hint: {"top": 0.779, "center_x": 0.155}
MDLabel:
text: "Customer Forename"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.707, "center_x": 0.115}
MDTextFieldRound:
id: Customer Forename
hint_text: "Info here"
mode:"rectangle"
icon_right: "Customer"
size_hint_x: None
width: 137.5
font_size: 14
pos_hint: {"top": 0.687, "center_x": 0.121}
MDLabel:
text: "Customer Surname"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.707, "center_x": 0.210}
MDTextFieldRound:
id: Customer Forename
hint_text: "Info Here"
mode:"rectangle"
icon_right: "Customer"
size_hint_x: None
width: 137.5
font_size: 14
pos_hint: {"top": 0.687, "center_x": 0.210}
MDLabel:
text: "Alternative email"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.620, "center_x": 0.115}
MDTextFieldRound:
id: Alternative Email
hint_text: "Info Here"
mode:"rectangle"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.600, "center_x": 0.155}
MDLabel:
text: "Alternative Phone"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.533, "center_x": 0.115}
MDTextFieldRound:
id: Alternative phone
hint_text: "Info Here"
mode:"rectangle"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.513, "center_x": 0.155}
MDLabel:
text: "Case Source"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.449, "center_x": 0.115}
MDTextFieldRound:
id: Case Source
hint_text: "Info Here"
mode:"rectangle"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.437, "center_x": 0.155}
MDLabel:
text: "Case Priority"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.365, "center_x": 0.115}
MDTextFieldRound:
id: Case Priority
hint_text: "Info Here"
mode:"rectangle"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.353, "center_x": 0.155}
MDLabel:
text: "Case Category"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.281, "center_x": 0.115}
MDLabel:
text: "Case Description"
font_size: 15
size_hint_x: None
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.883, "center_x": 0.360}
MDTextFieldRound:
id: CaseDescription
hint_text: "Info Here"
icon_right: "Case Title"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.863, "center_x": 0.400}
MDLabel:
text: "Possible Cases"
font_size: 15
size_hint_x: None
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.799, "center_x": 0.360}
MDTextFieldRound:
id: PossibleCases
hint_text: "Info Here"
icon_right: "Case Title"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.780, "center_x": 0.400}
MDLabel:
text: "Knowledge Helper"
font_size: 15
size_hint_x: None
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.715, "center_x": 0.360}
MDTextFieldRound:
id: KnowledgeHelper
hint_text: "Info Here"
icon_right: "Case Title"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.697, "center_x": 0.400}
MDRoundFlatButton:
text: "Submit"
font_size: 12
pos_hint: {"center_y": 0.400,"center_x": 0.5}
on_release: root.submit()
Screen:
name: "Create Case Version 2.0"
MDLabel:
text: "Case Title"
font_size: 15
size_hint_x: None
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.875, "center_x": 0.180}
MDTextFieldRound:
id: CaseTitle
hint_text: "Info Here"
icon_right: "Case Title"
size_hint_x: None
multiline: True
height: 4000
width: 275
font_size: 15
pos_hint: {"top": 0.863, "center_x": 0.220}
MDLabel:
text: "Customer"
font_size: 15
size_hint_x: None
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.791, "center_x": 0.180}
MDTextFieldRound:
id: Customer
hint_text: "Info Here"
mode:"rectangle"
icon_right: "Customer"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.779, "center_x": 0.220}
MDLabel:
text: "Customer Forename"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.707, "center_x": 0.180}
MDTextFieldRound:
id: Customer Forename
hint_text: "Info here"
mode:"rectangle"
icon_right: "Customer"
size_hint_x: None
width: 275
font_size: 14
pos_hint: {"top": 0.687, "center_x": 0.220}
MDLabel:
text: "Customer Surname"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.623, "center_x": 0.180}
MDTextFieldRound:
id: Customer Surname
hint_text: "Info Here"
mode:"rectangle"
icon_right: "Customer"
size_hint_x: None
width: 275
font_size: 14
pos_hint: {"top": 0.595, "center_x": 0.220}
MDLabel:
text: "Alternative email"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.620, "center_x": 0.115}
MDTextFieldRound:
id: Alternative Email
hint_text: "Info Here"
mode:"rectangle"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.600, "center_x": 0.155}
MDLabel:
text: "Alternative Phone"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.533, "center_x": 0.115}
MDTextFieldRound:
id: Alternative phone
hint_text: "Info Here"
mode:"rectangle"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.513, "center_x": 0.155}
MDLabel:
text: "Case Source"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.449, "center_x": 0.115}
MDTextFieldRound:
id: Case Source
hint_text: "Info Here"
mode:"rectangle"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.437, "center_x": 0.155}
MDLabel:
text: "Case Priority"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.365, "center_x": 0.115}
MDTextFieldRound:
id: Case Priority
hint_text: "Info Here"
mode:"rectangle"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.353, "center_x": 0.155}
MDLabel:
text: "Case Category"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.281, "center_x": 0.115}
MDDropDownItem:
id: drop_item
pos_hint: {"center_y": .269, "center_x": .155}
text: 'testing'
on_release: root.dropdown()
MDLabel:
text: "Possible Cases"
font_size: 15
size_hint_x: None
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.799, "center_x": 0.360}
MDTextFieldRound:
id: PossibleCases
hint_text: "Info Here"
icon_right: "Case Title"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.780, "center_x": 0.400}
ThreeLineListItem:
text: "Knowledge Helper"
font_size: 15
secondary_text: "This is a multi-line label where you can"
tertiary_text: "fit more text than usual"
width: 275
size_hint_x: None
pos_hint: {"top": 0.915, "center_x": 0.645}
MDLabel:
text: "Case Description"
font_size: 15
size_hint_x: None
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.883, "center_x": 0.745}
MDTextField:
id: Case Description
hint_text: "Info Here"
icon_right: "Case Title"
size_hint_x: None
width: 275
multiline: True
font_size: 15
pos_hint: {"top": 0.863, "center_x": 0.790}
MDRoundFlatButton:
text: "Submit"
font_size: 12
pos_hint: {"center_y": 0.400,"center_x": 0.5}
on_release: root.submit()
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
The error message is clear. The Homepage has no attribute names screen. It has a child widget that is a Screen, but that does not create an attribute named screen. The fix for your error is to remove references to self.screen in Homepage. Here are some suggested changes to your dropdown() method that removed these references:
def dropdown(self):
self.menu_items = [
{
"viewclass": "IconListItem",
"icon": "git",
"text": f"Item {i}",
"height": dp(56),
"on_release": lambda x=f"Item {i}": self.set_item(x),
} for i in range(5)
]
self.menu = MDDropdownMenu(
# caller=self.screen.ids.drop_item,
caller = self.ids.drop_item,
items=self.menu_items, # corrected
position="center",
width_mult=4,
)
# self.menu.bind()
self.menu.open() # open the Dropdown
def set_item(self, text_item):
self.ids.drop_item.set_item(text_item) # eliminated screen reference
self.menu.dismiss()
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
I am trying to make a dropdown list in KivyMD. The documentation for this wasn't particuarly helpful - I've added a mockup of what I am trying to create
Any Help would be massively appreciated. I've tried to look at tutorials but I am stuck.
...............................................................................
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.label import Label
from kivy.uix.popup import Popup
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.textinput import TextInput
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 kivymd.uix.menu import MDDropdownMenu
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 Homepage(Screen):
Login = ObjectProperty()
nav_drawer = ObjectProperty()
def submit(self):
Popup(title='Submitted', content=Label(text='Case has been submitted and will be reviewed'), size_hint=(0.5, 0.5)).open()
sm = self.ids.screen_manager
scr = sm.get_screen('Create Case')
for child in scr.children:
if isinstance(child, TextInput):
child.text = ''
def dropdown(self):
self.menu_list = [
{
"viewclass": "OneLineListItem",
"text": "Example 1",
"on_release": lambda x="Example 1": self.test1
},
{
"viewclass": "OneLineListItem",
"text": "Example 2",
"on_release": lambda x="Example 1": self.test2
}
]
self.menu = MDDropdownMenu(
caller = self.ids.dropmenu,
items = self.menu_list,
width_mult = 4
)
self.dropmenu.open()
def test1(self):
print("test1")
def test1(self):
print("test2")
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:
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"
MDLabel:
text: "Case Title"
font_size: 15
size_hint_x: None
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.875, "center_x": 0.115}
MDTextFieldRound:
id: CaseTitle
hint_text: "Info Here"
icon_right: "Case Title"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.863, "center_x": 0.155}
MDLabel:
text: "Customer"
font_size: 15
size_hint_x: None
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.791, "center_x": 0.115}
MDTextFieldRound:
id: Customer
hint_text: "Info Here"
mode:"rectangle"
icon_right: "Customer"
size_hint_x: None
width: 275
font_size: 14
pos_hint: {"top": 0.779, "center_x": 0.155}
MDLabel:
text: "Customer Forename"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.707, "center_x": 0.115}
MDTextFieldRound:
id: Customer Forename
hint_text: "Info here"
mode:"rectangle"
icon_right: "Customer"
size_hint_x: None
width: 137.5
font_size: 14
pos_hint: {"top": 0.687, "center_x": 0.121}
MDLabel:
text: "Customer Surname"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.707, "center_x": 0.210}
MDTextFieldRound:
id: Customer Forename
hint_text: "Info Here"
mode:"rectangle"
icon_right: "Customer"
size_hint_x: None
width: 137.5
font_size: 14
pos_hint: {"top": 0.687, "center_x": 0.210}
MDLabel:
text: "Alternative email"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.620, "center_x": 0.115}
MDTextFieldRound:
id: Alternative Email
hint_text: "Info Here"
mode:"rectangle"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.600, "center_x": 0.155}
MDLabel:
text: "Alternative Phone"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.533, "center_x": 0.115}
MDTextFieldRound:
id: Alternative phone
hint_text: "Info Here"
mode:"rectangle"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.513, "center_x": 0.155}
MDLabel:
text: "Case Source"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.449, "center_x": 0.115}
MDTextFieldRound:
id: Case Source
hint_text: "Info Here"
mode:"rectangle"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.437, "center_x": 0.155}
MDLabel:
text: "Case Priority"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.365, "center_x": 0.115}
MDTextFieldRound:
id: Case Priority
hint_text: "Info Here"
mode:"rectangle"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.353, "center_x": 0.155}
MDLabel:
text: "Case Category"
font_size: 15
size_hint_x: None
multiline: False
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.281, "center_x": 0.115}
MDLabel:
text: "Case Description"
font_size: 15
size_hint_x: None
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.883, "center_x": 0.360}
MDTextFieldRound:
id: CaseDescription
hint_text: "Info Here"
icon_right: "Case Title"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.863, "center_x": 0.400}
MDLabel:
text: "Possible Cases"
font_size: 15
size_hint_x: None
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.799, "center_x": 0.360}
MDTextFieldRound:
id: PossibleCases
hint_text: "Info Here"
icon_right: "Case Title"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.780, "center_x": 0.400}
MDLabel:
text: "Knowledge Helper"
font_size: 15
size_hint_x: None
bold: True
color: 0.204, 0.204, 0.204, 1
pos_hint: {"center_y": 0.715, "center_x": 0.360}
MDTextFieldRound:
id: KnowledgeHelper
hint_text: "Info Here"
icon_right: "Case Title"
size_hint_x: None
width: 275
font_size: 15
pos_hint: {"top": 0.697, "center_x": 0.400}
MDRoundFlatButton:
text: "Submit"
font_size: 12
pos_hint: {"center_y": 0.400,"center_x": 0.5}
on_release: root.submit()
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
I think you're looking for the KivyMD Menu widget. Here's an example from the
docs:
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.menu import MDDropdownMenu
KV = '''
MDScreen:
MDRaisedButton:
id: button
text: "PRESS ME"
pos_hint: {"center_x": .5, "center_y": .5}
on_release: app.menu.open()
'''
class Test(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.screen = Builder.load_string(KV)
menu_items = [
{
"text": f"Item {i}",
"viewclass": "OneLineListItem",
"on_release": lambda x=f"Item {i}": self.menu_callback(x),
} for i in range(5)
]
self.menu = MDDropdownMenu(
caller=self.screen.ids.button,
items=menu_items,
width_mult=4,
)
def menu_callback(self, text_item):
print(text_item)
def build(self):
return self.screen
Test().run()
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 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.