Kivy - KivyMD Login App Dahsboard Example
berikut ini tampilan source code membangun aplikasi multi platform menggunakan bahasa pemrograman python dan framework kivy + kivymd
perintahnya adalah sebagai berikut:
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock
from kivy.core.window import Window
from kivymd.uix.list import MDList, OneLineListItem
Window.size = (400, 700)
tampilan_awal = '''
MainScreen:
SplashScreen:
LoginScreen:
<SplashScreen>:
name: 'splashscreen'
BoxLayout:
orientation: 'vertical'
MDLabel:
id: judul
text: 'Aplikasi Login'
font_size: '40'
markup: True
halign: 'center'
<LoginScreen>:
name: 'loginscreen'
BoxLayout:
orientation: 'vertical'
padding: '10dp'
spacing: '10dp'
pos_hint: {'center_y':0.5}
adaptive_height: True
MDLabel:
text: 'Login'
font_size: '52'
halign: 'center'
MDTextField
id: username
hint_text: "Username"
text: "user"
MDTextField
id: password
hint_text: "Password"
password:True
text:"kosong"
MDRectangleFlatButton:
id:btnConnect
text: "LOG IN"
font_size: 18
pos_hint: {"center_x": 0.5}
on_press: root.proses_login()
Widget:
Widget:
'''
class MainScreen(ScreenManager):
pass
class SplashScreen(Screen):
pass
class LoginScreen(Screen):
def proses_login(self):
myApp = MDApp.get_running_app()
try:
myApp.window_manager.add_widget(AppScreen(name='appscreen'))
myApp.ganti_layar('appscreen','down')
except:
pass
AppScreen_kv = Builder.load_string('''
<AppScreen>:
name: 'appscreen'
BoxLayout:
orientation: 'vertical'
MDToolbar:
id: toolbar
pos_hint: {"top": 1}
elevation: 10
title: "Router Management"
left_action_items: [["menu", lambda x: nav_draw.set_state()]]
BoxLayout:
orientation: 'vertical'
MDNavigationLayout:
x: toolbar.height
ScreenManager:
id: app_screen_manager
MDNavigationDrawer:
id: nav_draw
orientation: "vertical"
padding: "4dp"
spacing: "4dp"
ScrollView:
MDList:
id:app_menu
Widget:
''')
class AppScreen(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
Clock.schedule_once(self.buatMenuLayar)
def buatMenuLayar(self, *args):
listLayar = [DashboardScreen(name="Dashboard")]
for i in range(len(listLayar)):
self.ids.toolbar.title = listLayar[i].name
self.ids.app_screen_manager.add_widget(listLayar[i])
self.ids.app_menu.add_widget(OneLineListItem(text=listLayar[i].name,on_press=self.gantiScreen))
self.ids.app_menu.add_widget(OneLineListItem(text="Log Out",on_press=self.logOut))
def gantiScreen(self, instance):
self.ids.toolbar.title=instance.text
self.ids.nav_draw.set_state("close")
self.ids.app_screen_manager.current = instance.text
def logOut(self, instance):
myApp = MDApp.get_running_app()
myApp.ganti_layar('loginscreen','up')
self.ids.nav_draw.set_state("close")
def build(self):
self.root_layout = FloatLayout()
self.root_layout.add_widget(AppScreen_kv)
return self.root_layout
dashboard_kv = Builder.load_string('''
<DashboardScreen>:
name: 'dashboardscreen'
MDBoxLayout:
spacing: "5dp"
padding: "10dp"
pos_hint: {"center_x": .5, "center_y": .5}
orientation: 'vertical'
GridLayout:
cols: 2
rows: 3
spacing:'5dp'
MDCard:
size_hint: 0.3, 0.3
orientation: 'vertical'
padding: '15dp'
spacing: '15dp'
md_bg_color: [0.121, .227, 0.576, 0.7]
radius: [16, ]
MDLabel:
id: jumlahuserdhcp
text: "0"
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
font_style: 'H3'
halign: 'center'
MDLabel:
id: keteranganuserdhcp
text: 'User DHCP'
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
font_style: 'Subtitle2'
halign: 'center'
MDCard:
size_hint: 0.3, 0.3
orientation: 'vertical'
padding: '15dp'
spacing: '15dp'
md_bg_color: [0.121, .227, 0.576, 0.7]
radius: [16, ]
MDLabel:
id: jumlahuserpppoe
text: "0"
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
font_style: 'H3'
halign: 'center'
MDLabel:
id: keteranganuserpppoe
text: 'User PPPOE'
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
font_style: 'Subtitle2'
halign: 'center'
MDCard:
size_hint: 0.3, 0.3
orientation: 'vertical'
padding: '15dp'
spacing: '15dp'
md_bg_color: [0.121, .227, 0.576, 0.7]
radius: [16, ]
MDLabel:
id: jumlahuserhospot
text: "0"
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
font_style: 'H3'
halign: 'center'
MDLabel:
id: keteranganuserhospot
text: 'User Hotspot'
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
font_style: 'Subtitle2'
halign: 'center'
''')
class DashboardScreen(Screen):
def on_enter(self):
print('Halaman Dashboard')
def build(self):
self.root_layout = FloatLayout()
self.root_layout.add_widget(dashboard_kv)
return self.root_layout
class MainApp(MDApp):
def build(self):
self.window_manager = Builder.load_string(tampilan_awal)
Clock.schedule_once(lambda dt: self.ganti_layar('loginscreen'), 3)
return self.window_manager
def ganti_layar(self,namascreen,arah='down'):
self.window_manager.transition.direction = arah
self.window_manager.current = namascreen
if __name__ == '__main__':
MainApp().run()