Skip to content

Instantly share code, notes, and snippets.

@foxmask
Created September 25, 2020 18:31
Show Gist options
  • Save foxmask/96257c9ff62761062c6648ac281127e2 to your computer and use it in GitHub Desktop.
Save foxmask/96257c9ff62761062c6648ac281127e2 to your computer and use it in GitHub Desktop.
MDNavigationDrawer + ScrollView
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty, ListProperty
from kivymd.app import MDApp
from kivymd.theming import ThemableBehavior
from kivymd.uix.list import OneLineIconListItem, MDList, OneLineListItem
import sqlite3
KV = '''
# Menu item in the DrawerList list.
<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: None
height: avatar.height
Image:
id: avatar
size_hint: None, None
size: "56dp", "56dp"
source: "data/logo/kivy-icon-256.png"
MDLabel:
text: "KivyMD library"
font_style: "Button"
size_hint_y: None
height: self.texture_size[1]
MDLabel:
text: "[email protected]"
font_style: "Caption"
size_hint_y: None
height: self.texture_size[1]
ScrollView:
DrawerList:
id: md_list
Screen:
NavigationLayout:
ScreenManager:
Screen:
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: "Nuyseu"
elevation: 10
left_action_items: [['menu', lambda x: nav_drawer.toggle_nav_drawer()]]
Widget:
ScrollView:
size_hint_y: 0.90
MDList:
id: container
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
id: content_drawer
'''
# MDNavigationDrawer
class ContentNavigationDrawer(BoxLayout):
pass
class ItemDrawer(OneLineIconListItem):
icon = StringProperty()
text_color = ListProperty((0, 0, 0, 1))
class DrawerList(ThemableBehavior, MDList):
def set_color_item(self, instance_item):
for item in self.children:
if item.text_color == self.theme_cls.primary_color:
item.text_color = self.theme_cls.text_color
break
instance_item.text_color = self.theme_cls.primary_color
class DbCon:
def __init__(self):
self.db = sqlite3.connect("nyuseu.sqlite3")
self.c = self.db.cursor()
def get_folders(self):
self.c.execute("SELECT * FROM nyuseu_folders")
return self.c.fetchall()
def get_articles(self):
self.c.execute("SELECT * FROM nyuseu_articles")
return self.c.fetchall()
def get_articles_from_folder(self, folder_id):
self.c.execute("SELECT * FROM nyuseu_articles where folder_id=?", folder_id)
return self.c.fetchall()
class TestNyuseuApp(MDApp):
def build(self):
return Builder.load_string(KV)
def on_start(self):
db = DbCon()
folders = db.get_folders()
articles = db.get_articles()
for item in folders:
self.root.ids.content_drawer.ids.md_list.add_widget(
ItemDrawer(icon='folder', text=item[1])
)
for item in articles:
self.root.ids.container.add_widget(
OneLineListItem(text=item[1])
)
TestNyuseuApp().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment