KivyMD DatePicker save multiple dates - python

I want that you can add and choose multiple dates. I tried to send a number with the button click button_nr but I don't know how I can pass it to the on_save function, so that I can change the self.ids.date_label(button_nr).text . If somebody has a better idea how to save multiple dates I'm open for suggestions.
Thank you
def on_save(self, instance, value, date_range):
date_value = datetime.datetime.strptime(str(value), "%Y-%m-%d")
date_value_eu = date_value.strftime("%d.%m.%Y")
self.ids.date_label1.text = str(date_value_eu)
def show_date_picker(self, button_nr):
print(button_nr)
date_dialog = MDDatePicker()
date_dialog.bind(on_save=self.on_save, on_cancel=self.on_cancel)
date_dialog.open()
The kv file part
MDRaisedButton:
on_release: root.show_date_picker(1)

You can use my code, I have designed this app in order to have a recycleview plus a search bar, the recycle list item in this example is moved into the pick date function and it changes the text of the list item widget (check the picture), here is the code by jbsidis (I am posting this after I commented your question):
from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivymd.uix.list import OneLineIconListItem
from kivy.properties import (
StringProperty,
BooleanProperty,
ObjectProperty,
NumericProperty,
ListProperty,
OptionProperty,
)
Window.size = (330, 660)
kv = '''
#:import MDTextField kivymd.uix.textfield.MDTextField
<MyTile#SmartTileWithLabel>
size_hint_y: None
height: "240dp"
<S>:
MDTextFieldRound:
pos_hint: {"center_x": .5, "center_y": .95}
normal_color : [1,1,1,.1]
color_active : [1,1,1,1]
size_hint: .8, .07
hint_text : 'Search a product...'
icon_left : 'magnify'
<JBSIDIS>
on_release: app.show_date_picker(root)
IconLeftWidget:
icon: "Photos/pro.jpg"
Screen:
name: "pantalla1"
MDToolbar:
id: tb1
title: "jbsidis"
pos_hint: {"top": 1}
md_bg_color: (.1, .1, .5, 1)
GridLayout:
cols: 2
padding: dp(20)
FloatLayout:
MDTextFieldRound:
id: search_field
pos_hint: {"center_x": .5, "center_y": .87}
hint_text : 'jbsidis...'
icon_left : 'magnify'
normal_color : (100, 100, 50, .5)
color_active : (255, 255, 255, 0)
on_text: app.search(root.ids.rvvv9,self.text)
BoxLayout:
id: m5
spacing: dp(10)
#padding: dp(20)
pos_hint: {"center_x": .5, "center_y": .32}
orientation: "vertical"
BoxLayout:
id: s_res9
size_hint_y: None
height: self.minimum_height
pos_hint: {"center_x": .5, "center_y": .4}
orientation: "vertical"
RecycleView:
id: rvvv9
key_viewclass: 'viewclass'
key_size: 'height'
RecycleBoxLayout:
padding: dp(10)
spacing: dp(5)
default_size: None, dp(75)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
MDToolbar:
id: success_screen_toolbar
title: "jbsidis"
pos_hint: {"top": 1}
right_action_items: [["progress-check", lambda: print(6)]]
MDBottomAppBar:
MDToolbar:
id: success_screen_bottomappbar
icon: "magnify"
on_action_button: app.eq(root.ids.search_field,True)
type: 'bottom'
mode: 'center'
#elevation: '8dp'
left_action_items: [["calendar-text", lambda x: print(6)], ["account-group", lambda x: print(6)]]
right_action_items: [["magnify", lambda x: app.eq(root.ids.search_field,True)], ["menu", lambda x: print(6)]]
'''
authors="jbsidis"
class JBSIDIS(OneLineIconListItem):
datex=StringProperty()
pass
class blanks1(BoxLayout):
pass
class S(FloatLayout):
pass
authors4="jbsidis"
books="""
Austen, Jane (72159) jbsidis
Du Bois, W. E. B. (William Edward Burghardt) (7364) jbsidis
Stowe, Harriet Beecher (7362) jbsidis
Vatsyayana (7343) jbsidis
Schopenhauer, Arthur (7317) jbsidis
Foote, Mary Hallock (7308) jbsidis
Bhide, Shivaram Parashuram (7256) jbsidis
Indrajit, Bhagavanlal (7256) jbsidis
"""
import time
import datetime
class Main(MDApp):
def build(self):
self.theme_cls.primary_palette = "Green"
return Builder.load_string(kv)
def get_date(self, date):
print(date)
t=str(date).split("-")
text="[b]"+str(t[-1])+"/"+str(t[1])+"/"+str(t[0])
year=time.strftime("%Y")
mes=time.strftime("%m")
dia=time.strftime("%d")
bx.text=text
afecha=str(t[-1])+"/"+str(t[1])+"/"+str(t[0])
return date
def show_date_picker(self,b):
from kivymd.uix.picker import MDDatePicker
global bx
bx=b
yearx=time.strftime("%Y")
mes=time.strftime("%m")
dia=time.strftime("%d")
current=time.strftime("%Y:%m:%d")
min_date = datetime.datetime.strptime(current, '%Y:%m:%d').date()
date_dialog = MDDatePicker(
callback=self.get_date,
min_date=min_date
)
date_dialog.open()
def all_pdfs(self):
return books.splitlines()
def eq(self,a,b):
a.focus=True
def search(self,a,b):
a.data=[]
for x in self.all_pdfs():
if b in str(x):
a.data.append(
{
"viewclass": "JBSIDIS",
"markup": True,
"text": "[b][size=33]jbsidis: [/b][/size]"+str(x)
}
)
pass
Main().run()
This is one of many ways you can do things using kivy and kivymd, jbsidis, everything is possible with Python and Kivy, here is the image, greetings from El Salvador:

Related

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.

Toolbar text does not change in kivyMD and text goes beyond the boundaries

from kivymd.app import MDApp
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.properties import ObjectProperty
class ContentNavigationDrawer(BoxLayout):
"""This class manage the navigation drawer contents"""
screen_manager = ObjectProperty()
nav_drawer = ObjectProperty()
class MyApp(MDApp):
def build(self):
"""Return the main kivy file and set themes """
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = 'BlueGray'
self.theme_cls.accent_palette = "Teal"
return Builder.load_file("main.kv")
MyApp().run()
and the kv file
<ContentNavigationDrawer>:
MDBoxLayout:
orientation: "vertical"
spacing: dp("8")
padding: dp("8")
ScrollView:
MDList:
OneLineIconListItem:
text: "hexagon"
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = "mainScreen"
IconLeftWidget:
icon: 'hexagon'
MDScreen:
MDToolbar:
id: toolbar
title: "Hexagon"
md_bg_color: app.theme_cls.primary_color
specific_text_color: 1, 1, 1, 1
pos_hint: {"top": 1}
elevation: 9
icon: 'account-circle'
left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]
MDNavigationLayout:
x: toolbar.height
ScreenManager:
id: screen_manager
MDScreen:
name: "mainScreen"
BoxLayout:
orientation: 'vertical'
size: root.width, root.height
ScrollView:
do_scroll_x: True
do_scroll_y: True
size_hint: (1, .85)
bar_width: 10
bar_color: (1, 0, 0 ,1)
scroll_type: ["bars", "content"]
pos_hint: {'top': 1.0 - toolbar.height/float(root.height)}
GridLayout:
id: labels_layout
size_hint_y: None
height:self.minimum_height
size_hint_x: None
width: self.minimum_width
cols: 1
spacing: "5dp"
padding: "5dp"
MDLabel:
id: big_text_label
text: "asldklsa\nasdsaf\nssss\n"
width: self.texture_size[1]
height: self.texture_size[1]
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
screen_manager: screen_manager
nav_drawer: nav_drawer
test_screen_shot
I want to change the color of the toolbar I tried almost everything to do it
like md_bg_color: app.theme_cls.primary_color and md_bg_color: .2, .2, .2, 1
And the text goes beyond the boundaries even of the app if i add some widgets to get back
text to its area the it goes under the toolbar
Using:
Python 3.9, dev kivyMD 2.0, editor pycharm
Use:
from kivymd.app import MDApp
from kivy.lang import Builder
KV = """
<ContentNavigationDrawer>:
MDBoxLayout:
orientation: "vertical"
spacing: dp("8")
padding: dp("8")
ScrollView:
MDList:
OneLineIconListItem:
divider: None
text: "hexagon"
on_press:
app.root.ids.nav_drawer.set_state("toggle")
#root.screen_manager.current = "mainScreen"
IconLeftWidget:
icon: 'account'
## BoxLayout:
## canvas.before:
## Color:
## rgba: [1,.3,0,1]
## Line:
## width: dp(1)
## rounded_rectangle:
## (self.x, self.y, self.width-dp(20), dp(43),\
## dp(12),dp(12),dp(12),dp(12),\
## dp(50))
OneLineIconListItem:
divider: None
text: "hexagon"
on_press:
app.root.ids.nav_drawer.set_state("toggle")
#root.screen_manager.current = "mainScreen"
IconLeftWidget:
icon: 'hexagon'
OneLineIconListItem:
divider: None
text: "Edit"
on_press:
app.root.ids.nav_drawer.set_state("toggle")
#root.screen_manager.current = "mainScreen"
IconLeftWidget:
icon: 'pencil'
OneLineIconListItem:
divider: None
text: "Home"
on_press:
app.root.ids.nav_drawer.set_state("toggle")
#root.screen_manager.current = "mainScreen"
IconLeftWidget:
icon: 'home'
OneLineIconListItem:
divider: None
text: "Likes"
on_press:
app.root.ids.nav_drawer.set_state("toggle")
#root.screen_manager.current = "mainScreen"
IconLeftWidget:
icon: 'star'
OneLineIconListItem:
divider: None
text: "hexagon"
on_press:
app.root.ids.nav_drawer.set_state("toggle")
#root.screen_manager.current = "mainScreen"
IconLeftWidget:
icon: 'hexagon'
OneLineIconListItem:
divider: None
text: "Search"
on_press:
app.root.ids.nav_drawer.set_state("toggle")
#root.screen_manager.current = "mainScreen"
IconLeftWidget:
icon: 'magnify'
Screen:
MDToolbar:
id: toolbar
pos_hint: {"top": 1}
title: "Hexagon"
md_bg_color: [1,0,0,1]
specific_text_color: [1, 1, 1, 1]
elevation: 0
left_action_items: [["menu", lambda x: nav_drawer.set_state("toggle")]]
right_action_items: [["account-circle", lambda x: print(222)]]
ScreenManager:
id: screen_manager
MDScreen:
name: "mainScreen"
FloatLayout:
BoxLayout:
id: m5
pos_hint: {"center_x": .5, "center_y": .38} #this will change if you change this Window.size = (330, 500)
orientation: "vertical"
ScrollView:
do_scroll_x: False #True
do_scroll_y: True
#size_hint: (1, .85)
bar_width: 10
bar_color: (1, 0, 0 ,1)
scroll_type: ["bars", "content"]
pos_hint: {'top': 1.0 - toolbar.height/float(root.height)}
GridLayout:
id: labels_layout
size_hint_y: None
height:self.minimum_height
size_hint_x: 1
#width: self.minimum_width
cols: 1
spacing: "5dp"
padding: dp(20)
MDLabel:
id: big_text_label
text: "\\n\\n\\n\\n\\n\\nWelcome to this New App.\\nYou will get a lot of benefits.\\n\\njbsidis recommendations are good so we can design in different ways."
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
screen_manager: screen_manager
"""
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
class ContentNavigationDrawer(BoxLayout):
"""This class manage the navigation drawer contents"""
screen_manager = ObjectProperty()
#nav_drawer = ObjectProperty()
class WeatherApp(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
pass
def build(self):
self.screen = Builder.load_string(KV)
return self.screen
WeatherApp().run()
Pictures:

MDSelectionList properties cannot be modified

I am using an MDSelectionList inside a tab with an MDNavigationRail to switch between screens, however, when I try to customize the MDSelectionList, none of the visual changes apply. I thought that maybe it is a problem with the widget itself but then I separated it and the property changes worked. How can I make the changes apply to the selection list? (I am using kivymd 0.104.2.dev0 from the master branch)
My Code:
from kivy.config import Config
Config.set('graphics', 'width', '850')
Config.set('graphics', 'height', '530')
Config.set('graphics', 'minimum_width', '850')
Config.set('graphics', 'minimum_height', '530')
from kivy.lang import Builder
from kivymd.uix.card import MDCard
from kivymd.uix.tab import MDTabsBase
from kivymd.app import MDApp
class SettingsTab(MDCard, MDTabsBase):
pass
class Example(MDApp):
def __init__(self, **kwargs):
super(Example, self).__init__(**kwargs)
self.kv = Builder.load_string('''
#:kivy 2.0.0
<SettingsTab>:
orientation: "vertical"
size_hint: .95, .95
pos_hint: {"center_x": .5, "center_y": .5}
border_radius: 5
radius: [5]
elevation: 20
BoxLayout:
MDNavigationRail:
color_active: app.theme_cls.primary_color
MDNavigationRailItem:
icon: "list-status"
on_release:
screens.current = "downloads_screen"
MDNavigationRailItem:
icon: "cog"
on_release:
screens.current = "settings"
MDNavigationRailItem:
icon: "information"
on_release:
screens.current = "activity_log"
ScreenManager:
id: screens
Screen:
name: "downloads_screen"
Screen:
name: "activity_log"
Screen:
name: "settings"
MDTabs:
SettingsTab:
title: "DOWNLOAD"
MDSelectionList:
overlay_color: 1, 0, 0, .5
icon_bg_color: 0, 0, 0, 0
icon_check_color: 0, 0, 0, 0
OneLineIconListItem:
text: "Just me!"
IconLeftWidget:
icon: "circle-outline"
pos_hint: {"center_x": .5, "center_y": .5}
OneLineIconListItem:
text: "Just me!"
IconLeftWidget:
icon: "circle-outline"
OneLineIconListItem:
text: "Just me!"
IconLeftWidget:
icon: "circle-outline"
OneLineIconListItem:
text: "Just me!"
IconLeftWidget:
icon: "circle-outline"
SettingsTab:
title: "COLOR"
SettingsTab:
title: "INFO"''')
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Blue"
self.theme_cls.accent_palette = "Teal"
return self.kv
if __name__ == '__main__':
Example().run()
Try something like this.
from kivy.lang import Builder
from kivymd.uix.card import MDCard
from kivymd.uix.tab import MDTabsBase
from kivymd.app import MDApp
kv = ("""
<Check#ILeftBodyTouch+MDCheckbox>:
group: 'group'
size_hint: None, None
size: dp(48), dp(48)
<SettingsTab>:
orientation: "vertical"
size_hint: .95, .95
pos_hint: {"center_x": .5, "center_y": .5}
border_radius: 5
radius: [5,]
elevation: 20
BoxLayout:
MDNavigationRail:
color_active: app.theme_cls.primary_color
MDNavigationRailItem:
icon: "cog"
on_release:
screens.current = "settings"
ScreenManager:
id: screens
Screen:
name: "settings"
MDTabs:
SettingsTab:
title: "DOWNLOAD"
MDList:
spacing: 10
OneLineAvatarIconListItem:
text: "Just me!"
Check:
OneLineAvatarIconListItem:
text: "Just me!"
Check:
OneLineAvatarIconListItem:
text: "Just me!"
Check:
""")
class SettingsTab(MDCard, MDTabsBase):
pass
class Example(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.kv = Builder.load_string(kv)
def build(self):
return self.kv
if __name__ == '__main__':
Example().run()

Reference a input box id on kivy screen

I think this problem is easy to solve, but i dont have experience enough to do this.
So, I want to reference the input text (MDTextField on kv file) for the variable "name" that is inside the press() function, so that I can write inside the popup.
I'm learning kivymd for a college project, so, discount the errors and unnecessary informations :/
Here is my kv file
Screen:
id: screen1
NavigationLayout:
id: navigation
ScreenManager:
id: screen_manager
Screen:
id: fact
name: 'fact'
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: "Menu"
elevation: 10
left_action_items: [['menu', lambda x: nav_drawer.set_state()]]
Widget:
MDLabel:
text: "Fact Checker"
font_size: 56
font_name: "MontserratBold"
pos_hint: {'center_x': 0.75, 'center_y': 0.7}
theme_text_color: "Custom"
text_color: (53/255, 183/255, 1, 1)
MDTextField:
id: input_box
hint_text: "Type here"
font_size: 18
pos_hint: {'center_x': 0.5, 'center_y': 0.45}
size_hint_x: None
width: 500
multiline: True
helper_text: "Type the title of the news"
helper_text_mode: "on_focus"
theme_text_color: "Custom"
text_color: (53/255, 183/255, 1, 1)
line_color_normal: 1, 1, 1, 1
MDRectangleFlatButton:
text: "Check"
pos_hint: {'center_x': 0.5, 'center_y': 0.25}
theme_text_color: "Custom"
text_color: (1, 1, 1, 1)
on_press: app.press()
MDLabel:
markup: True
text: "<Developed by: [color=#cfd0d1]Caio Barreto and Gabriel Queiroz[/color]>"
font_name: "Montserrat"
theme_text_color: "Custom"
text_color: (53/255, 183/255, 1, 0.5)
pos_hint: {'center_x': 0.74, 'center_y': 0.1}
Screen:
name: 'discord'
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: "Menu"
elevation: 10
left_action_items: [['menu', lambda x: nav_drawer.set_state()]]
Widget:
MDLabel:
text: 'teste1'
Screen:
name: 'add'
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: "Menu"
elevation: 10
left_action_items: [['menu', lambda x: nav_drawer.set_state()]]
Widget:
MDLabel:
text: 'teste2'
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
ScrollView:
DrawerList:
id: md_list
MDList:
OneLineIconListItem:
text: "Menu"
on_press: screen_manager.current = "fact"
IconLeftWidget:
icon: "home"
OneLineIconListItem:
text: "Discord"
on_press: screen_manager.current = "discord"
IconLeftWidget:
icon: "discord"
OneLineIconListItem:
text: "Add more news"
on_press: screen_manager.current = "add"
IconLeftWidget:
icon: "circle-edit-outline"
Here is my python code:
from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivymd.uix.dialog import MDDialog
from kivymd.uix.textfield import MDTextField
from kivymd.uix.button import MDRectangleFlatButton, MDFlatButton
from kivymd.uix.navigationdrawer import NavigationLayout
from kivymd.uix.list import MDList
from kivymd.uix.boxlayout import BoxLayout
from kivymd.theming import ThemableBehavior
from kivy.core.text import LabelBase
from kivy.uix.widget import Widget
from helper import username_input
from kivymd.uix.screen import Screen
LabelBase.register(name="MontserratBold", fn_regular="font/Montserrat-Bold.ttf")
LabelBase.register(name="Montserrat", fn_regular="font/Montserrat-Medium.ttf")
class FactCheckerApp(MDApp):
class ContentNavigationDrawer(BoxLayout):
pass
class DrawerList(ThemableBehavior, MDList):
pass
def build(self):
self.file = Builder.load_file('projeto.kv')
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "LightBlue"
print(self.file.Screen.ids.)
return self.file
def press(self):
name = self.screen1.navigation.screen_manager.fact.input_box.text
close_button = MDFlatButton(text='Close', on_release=self.close_dialog)
self.dialog = MDDialog(title='Answer', text=name, size_hint=(0.5, 1), buttons=[close_button])
self.dialog.open()
def close_dialog(self, obj):
self.dialog.dismiss()
if __name__ == '__main__':
FactCheckerApp().run()
To access the MDTextField, you can just reference it through its id, which is defined in the widget that is the root of the kv rule where the id is defined (in this case, in the Screen). Try changing:
name = self.screen1.navigation.screen_manager.fact.input_box.text
to:
name = self.root.ids.input_box.text
in the press() method.

MDLabels stacked in one place (one above another)

I've some problems with multiple MDLabels in BoxLayout (that is contains by AnchorLayout), so all the MDLabel objects are stacked in one place on the screen!
I dont know how to make them centered and grouped like a list (with spacing and e.g.)
Please, help me with solving that problem!
Thanks a lot and sorry for bad english.
There is my main.py
from kivy.app import App
from kivymd.theming import ThemeManager
from kivymd.label import MDLabel
from kivy.uix.screenmanager import Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.metrics import dp, sp, pt
def toast(text):
from kivymd.toast.kivytoast import toast
toast(text)
class MyScreen(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.menu_items = [
{
"viewclass": "MDMenuItem",
"text": "text%d" % i,
"callback": self.callback,
}
for i in range(1, 3)
]
self.menu_button = None
def change_variable(self, value):
print("\nvalue=", value)
self.VARIABLE = value
print("\tself.VARIABLE=", self.VARIABLE)
def callback(self, *args):
toast(args[0])
class MainApp(App):
title = "KivyMD MDDropdownMenu Demo"
theme_cls = ThemeManager()
def build(self):
return MyScreen()
if __name__ == "__main__":
MainApp().run()
And there is my main.kv file contains:
#:import MDDropdownMenu kivymd.menus.MDDropdownMenu
#:import MDRaisedButton kivymd.button.MDRaisedButton
#:import MDLabel kivymd.label.MDLabel
<MDMenuItem>:
on_release:
app.root.change_variable(self.text)
app.root.menu_button.text = self.text
<MyScreen>:
name: 'myscrn'
AnchorLayout:
anchor_y: 'center'
BoxLayout:
orientation: 'vertical'
size_hint: 0.1, 0.5
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
spacing: dp(10)
MDRaisedButton:
id: mainbutton
size_hint: None, None
size: 3 * dp(48), dp(48)
text: 'MDButton1'
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
opposite_colors: True
on_release:
root.menu_button = mainbutton
MDDropdownMenu(items=root.menu_items, width_mult=4).open(self)
MDRaisedButton:
id: secondbutton
size_hint: None, None
size: 3 * dp(48), dp(48)
text: 'MDButton2'
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
opposite_colors: True
on_release:
root.menu_button = secondbutton
MDDropdownMenu(items=root.menu_items, width_mult=4).open(self)
AnchorLayout:
anchor_y: 'top'
BoxLayout:
orientation: 'vertical'
size_hint: 0.95, 0.5
padding: [0, 0, 0, 0]
spacing: dp(5)
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
MDLabel:
font_size: dp(12)
text: '123'
MDLabel:
font_size: dp(22)
text: '456'
Woops, looks like a simple mistake. Your indentation on KV Lang is incorrect. You didn't nest your labels into BoxLayout correctly.
AnchorLayout:
anchor_y: 'top'
BoxLayout:
orientation: 'vertical'
size_hint: 0.95, 0.5
padding: [0, 0, 0, 0]
spacing: dp(5)
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
MDLabel:
font_size: dp(12)
text: '123'
MDLabel:
font_size: dp(22)
text: '456'"""

Categories