#python #kivymd
Вопрос:
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.uix.boxlayout import MDBoxLayout
class LoginWindow(Screen):
pass
class HomeWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
class ContentNavigationDrawer(MDBoxLayout):
pass
class main(MDApp):
def __init__(self, **kwargs):
self.title = "APP"
super().__init__(**kwargs)
def build(self):
self.theme_cls.theme_style = "Dark"
self.root = Builder.load_file('main.kv')
wm = WindowManager()
wm.add_widget(LoginWindow())
wm.add_widget(HomeWindow())
return wm
if __name__ == '__main__':
main().run()
Вот мой файл ky.
WindowManager:
LoginWindow:
HomeWindow:
<WindowManager>:
id:window_manager
name:'window_manager'
<LoginWindow>:
id:login_window
name:'login_window'
MDCard:
size_hint:None,None
size:300,500
orientation:"vertical"
halign:"center"
pos_hint:{"center_x":0.5, "center_y":0.5}
padding:15
spacing:20
MDLabel:
text:"Welcome!"
halign:"center"
MDTextFieldRound:
id:user
hint_text:"user"
icon_right:"account"
size_hint:None, None
width:250
pos_hint:{"center_x":0.5, "center_y":0.5}
halign:"center"
MDTextFieldRound:
id:password
hint_text:"password"
icon_right:"eye-off"
size_hint:None, None
width:250
pos_hint:{"center_x":0.5, "center_y":0.5}
halign:"center"
MDRectangleFlatButton:
text:"Log in"
size_hint:None, None
width:150
pos_hint:{"center_x":0.5, "center_y":0.5}
on_press:root.manager.current="home_window"
<HomeWindow>:
id:home_window
name:'home_window'
MDNavigationLayout:
ScreenManager:
MDScreen:
BoxLayout:
orientation:"vertical"
MDToolbar:
type:"top"
title: "Navigation Drawer"
elevation: 10
left_action_items: [['menu', lambda x: nav_drawer.set_state("open")]]
Widget:
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
<ContentNavigationDrawer>:
id:content_drawer
orientation:"vertical"
Я новичок.
Когда я практиковал переключатель экрана и навигационный ящик, я ожидал, что в качестве примера будет запущен навигационный ящик, который https://kivymd.readthedocs.io/en/0.104.0/components/navigation-drawer/index.html.
Когда я запускал код, я был сбит с толку проблемой, которая заключалась в том, что
lambda x: nav_drawer.set_state("open")
, возможно, выполнялась дважды.
Ответ №1:
Вы можете удалить один MDNavigationDrawer или использовать мой код:
try:
from jnius import autoclass
python_activity = autoclass('org.kivy.android.PythonActivity')
python_activity.removeLoadingScreen()
except:
pass
try:
import plyer
except:
pass
try:
import android
except:
pass
from time import sleep
from kivymd.icon_definitions import md_icons
from kivymd.uix.list import OneLineIconListItem
from kivymd.app import MDApp
from kivymd.uix.button import MDIconButton
from kivymd.uix.label import MDLabel
from kivymd.uix.card import MDCard
from kivymd.uix.tab import MDTabsBase
from kivymd.uix.dialog import MDDialog
from kivymd.theming import ThemableBehavior
from kivymd.uix.list import OneLineIconListItem, MDList
from kivymd.toast import toast
from kivymd.uix.behaviors import TouchBehavior
from kivymd.uix.filemanager import MDFileManager
from kivymd.uix.button import MDRaisedButton
from kivymd.uix.button import MDFloatingActionButton
from kivymd.uix.button import MDFlatButton
from kivymd.uix.list import OneLineListItem
from kivymd.uix.list import TwoLineIconListItem
from kivymd.uix.list import ThreeLineAvatarListItem
from kivymd.uix.list import OneLineAvatarIconListItem
from kivymd.uix.list import TwoLineAvatarListItem
from kivymd.uix.list import OneLineAvatarListItem
from kivymd.uix.list import ImageLeftWidget
from kivymd.uix.gridlayout import MDGridLayout
from kivymd.uix.list import IconLeftWidget
from kivymd.uix.list import IconRightWidget
from kivymd.uix.textfield import MDTextField
from kivymd.uix.snackbar import Snackbar
from kivymd.uix.bottomsheet import MDListBottomSheet
from kivymd.uix.bottomsheet import MDGridBottomSheet
from kivymd.uix.filemanager import MDFileManager
from kivymd.uix.imagelist import SmartTileWithLabel
from kivymd.uix.card import MDCardSwipe
#import kivymd.uix.behaviors.MagicBehavior
from kivymd.uix.behaviors.magic_behavior import MagicBehavior
#from kivymd.uix.button import MagicButton
import sys
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.animation import Animation
from kivy.graphics.context_instructions import Color
from kivy.lang import Builder
from kivy.properties import (
NumericProperty,
StringProperty,
BooleanProperty,
OptionProperty,
ListProperty,
ObjectProperty,
)
from kivy.metrics import dp
from kivy.metrics import sp
from kivymd.font_definitions import theme_font_styles
from kivymd.theming import ThemableBehavior
from kivymd.uix.label import MDIcon
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.image import Image
from kivy.factory import Factory
from kivy.uix.scatterlayout import ScatterLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.image import Image
from kivy.uix.scrollview import ScrollView
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.uix.screenmanager import ScreenManager,Screen,WipeTransition,FadeTransition,FallOutTransition,RiseInTransition,CardTransition
from kivy.properties import ObjectProperty
from kivy.properties import StringProperty
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.core.audio import SoundLoader
from kivy.metrics import dp
from kivy.uix.bubble import Bubble
from kivy.core.window import Window
from kivy.uix.gridlayout import GridLayout
from kivy.uix.progressbar import ProgressBar
from kivy.setupconfig import USE_SDL2
from kivy.uix.bubble import Bubble
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.label import Label
from kivy.properties import BooleanProperty
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.scatter import Scatter
from kivy.properties import StringProperty, ListProperty
import requests
import time
import shutil
from datetime import datetime
from datetime import date, timedelta
import os
worm = os.getcwd() "/"
Window.softinput_mode = "below_target"
def android_dir(a):
try:
from android.storage import app_storage_path
from android.storage import primary_external_storage_path
from android.storage import secondary_external_storage_path
pt1=app_storage_path() "/"
if a==1:
pt2=primary_external_storage_path() "/"
return pt2
if a==2:
pt3=secondary_external_storage_path() "/"
return pt3
return pt2
except:
return worm
class ContentNavigationDrawer(BoxLayout):
nav_drawer = ObjectProperty()
pass
class DrawerList(ThemableBehavior, MDList):
def set_color_item(self, instance_item):
'''Called when tap on a menu item.'''
# Set the color of the icon and text for the menu item.
for item in self.children:
if item.text_color == self.theme_cls.primary_color:
item.text_color = self.theme_cls.text_color
#self.theme_cls.theme_style="Dark"
break
instance_item.text_color = self.theme_cls.primary_color
Window.softinput_mode = "below_target"
#Window.size=(350,600)
KV = '''
#:import toast kivymd.toast.toast
#:import Snackbar kivymd.uix.snackbar.Snackbar
#:import Clipboard kivy.core.clipboard.Clipboard
#:import MDLabel kivymd.uix.label.MDLabel
#:import MDCard kivymd.uix.card.MDCard
#:import MDTextFieldRound kivymd.uix.textfield.MDTextFieldRound
#:import MDTextButton kivymd.uix.button.MDTextButton
#:import MDIconButton kivymd.uix.button.MDIconButton
#:import MDRaisedButton kivymd.uix.button.MDRaisedButton
#:import Window kivy.core.window.Window
#:import runTouchApp kivy.app.runTouchApp
#:import MDFileManager kivymd.uix.filemanager
#:import Clock kivy.clock.Clock
#:import Button kivy.uix.button.Button
#:import Bubble kivy.uix.bubble.Bubble
#:import * kivy.graphics
#:import ProgressBar kivy.uix.progressbar
#:import OneLineListItem kivymd.uix.list.OneLineListItem
#:import ThreeLineAvatarListItem kivymd.uix.list.ThreeLineAvatarListItem
#:import MagicBehavior kivymd.uix.behaviors.MagicBehavior
####################################################
<On1>:
orientation: "vertical"
border_radius: "-3dp"
ripple_behavior: True
elevation: 20
size: "320dp", "100dp"
size_hint: None, None
pos_hint: {"center_x": .5, "center_y": .5}
#background: app.w "toolbar.png"
AnchorLayout:
pos_hint: {"center_x": .5, "center_y": .5}
blanks1:
FitImage:
source: app.w "bbay.png"
size_hint: .5, None
height: text_box.height 100
MDBoxLayout:
id: text_box
orientation: "vertical"
adaptive_height: True
spacing: "40dp"
padding: 0, "90dp", "90dp", "90dp"
#pos_hint: {"center_x": .5, "center_y": .8}
MDLabel:
id: user20
markup: True
text: "\n\n[b][color=ffffff] Tarkhan Simarik\n[/b] [color=ffffff]Cód. de Vendedor: 5555\n[color=ffffff] Idioma: \n"
#font_style: "H5"
#halign: "center"
size_hint_y: 20
height: self.texture_size[1]
# Menu item in the DrawerList list.
<ItemDrawer>:
canvas.before:
Color:
rgba: (1,1,1, 1)
Rectangle:
##source: app.w "toolbar.png"
pos: self.pos
size: self.size
#theme_text_color: "Custom"
on_release: app.action(str(root.icon)) #toast(str(root.icon)) #self.parent.set_color_item(self)
IconLeftWidget:
id: icon
icon: root.icon
#theme_text_color: "Custom"
#text_color: root.text_color
<ContentNavigationDrawer>:
canvas.before:
Color:
rgba: (0,0,0, 0)
Rectangle:
pos: self.pos
size: self.size
orientation: "vertical"
ScrollView:
padding: dp(10)
default_size_hint: None, None
DrawerList:
id: md_list
size_hint_y: None
MDCard:
orientation: "vertical"
border_radius: "-3dp"
ripple_behavior: True
elevation: 20
size: "320dp", "100dp"
size_hint: None, None
pos_hint: {"center_x": .5, "center_y": .5}
#background: "Photos/pro.jpg"
AnchorLayout:
pos_hint: {"center_x": -.1, "center_y": .5}
FitImage:
source: "Photos/pro.jpg"
size_hint: 1, None
height: text_box.height # 100
MDBoxLayout:
id: text_box
orientation: "vertical"
adaptive_height: True
spacing: "40dp"
padding: 0, "90dp", "90dp", "90dp"
#pos_hint: {"center_x": .5, "center_y": .8}
MDLabel:
id: user20
markup: True
text: "\n\n[b][color=ffffff] JBSIDIS\n[/b] [color=ffffff]------\n[color=ffffff] : \n"
#font_style: "H5"
#halign: "center"
size_hint_y: 20
height: self.texture_size[1]
Screen:
MDToolbar:
id: mt
pos_hint: {"top": 1}
title: app.t
elevation: 10
#md_bg_color: (.1,.80,.5,.3)
font_stye: "H5"
#source: app.w "toolbar.png"
hint_animation: True
left_action_items: [['menu', lambda x: nav_drawer.toggle_nav_drawer()]]
NavigationLayout: #kivy.factory.FactoryException: Unknown class <MDNavigationLayout>
ScreenManager:
id: MainActivity
Screen:
id: SecondActivity
name: "main"
BoxLayout:
id: superbody_jbsidis
orientation: 'vertical'
spacing: dp(20)
MDCard:
id: jbsidis_will_be_card
orientation: "vertical"
border_radius: "-3dp"
ripple_behavior: True
padding: dp(10)
size: "320dp", "250dp"
size_hint: .5, None
pos_hint: {"center_x": .5, "center_y": .5}
background: "Photos/pro.jpg"
TextInput:
markup: True
font_size: dp(40)
size_hint: 1.1,1.1
#font_color: (0,0,0,1)
text: "Created by jbsidis " #open("file.txt").read()
id: bx1
background_color: [0,0,0,0]
foreground_color: [1,1,1,1]
halign: "left"
#multiline: True
hint_text: 'Insert Text'
FloatLayout:
BoxLayout:
pos_hint: {"center_x": 1.2, "center_y": .5}
MDIconButton:
canvas.before:
Color:
rgba: (1,1,1,.5)
Rectangle:
pos: self.pos
size: self.size
icon: "pencil"
on_release: app.root.ids.bx1.focus=True
MDIconButton:
canvas.before:
Color:
rgba: (1,1,1,.5)
Rectangle:
pos: self.pos
size: self.size
icon: "delete"
on_release:
app.root.ids.superbody_jbsidis.remove_widget(app.root.ids.jbsidis_will_be_card)
MDCard:
id: jbsidis_will_be_card2
orientation: "vertical"
border_radius: "-3dp"
ripple_behavior: True
padding: dp(10)
size: "320dp", "250dp"
size_hint: .5, None
pos_hint: {"center_x": .5, "center_y": .5}
background: "Photos/pro.jpg"
TextInput:
markup: True
font_size: dp(40)
size_hint: 1.1,1.1
#font_color: (0,0,0,1)
text: "Created by jbsidis " #open("file.txt").read()
id: bx2
background_color: [0,0,0,0]
foreground_color: [1,1,1,1]
halign: "left"
#multiline: True
hint_text: 'Insert Text'
FloatLayout:
BoxLayout:
pos_hint: {"center_x": 1.2, "center_y": .5}
MDIconButton:
canvas.before:
Color:
rgba: (1,1,1,.5)
Rectangle:
pos: self.pos
size: self.size
icon: "pencil"
on_release: app.root.ids.bx2.focus=True
MDIconButton:
canvas.before:
Color:
rgba: (1,1,1,.5)
Rectangle:
pos: self.pos
size: self.size
icon: "delete"
on_release:
app.root.ids.superbody_jbsidis.remove_widget(app.root.ids.jbsidis_will_be_card2)
Widget:
## BoxLayout:
## id: m5
## spacing: dp(10)
## #padding: dp(20)
## pos_hint: {"center_x": .5, "center_y": .43}
## orientation: "vertical"
## BoxLayout:
## id: s_res9
## size_hint_y: None #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:
## id: jbsidis
## padding: dp(10)
## spacing: dp(1)
## #default_size: None, dp(75)
## default_size_hint: 1, None
## size_hint_y: None
## height: self.minimum_height
## orientation: 'vertical'
Widget:
MDNavigationDrawer:
opening_time: 0.1
swipe_distance: 2
swipe_edge_width: 40
#scrim_color: [.1,.2,.4,1]
#spacing: dp(5)
id: nav_drawer
ContentNavigationDrawer:
id: content_drawer
<Menu1>:
name: "menu1"
## canvas.before:
## Color:
## rgba: (1,1,1, 1)
## Rectangle:
## source: app.w "bgx.png"
## pos: self.pos
## size: self.size
## MDToolbar:
## canvas.before:
## Color:
## rgba: (1,1,1, .8)
## Rectangle:
## source: app.w "toolbar.png"
## pos: self.pos
## size: self.size
## id: mt
## pos_hint: {"top": 1}
## title: app.t
## elevation: 10
## md_bg_color: (.1,.80,.5,.3)
## font_stye: "H5"
## #source: app.w "toolbar.png"
## hint_animation: True
## MDIconButton:
## icon: "menu"
#left_action_items: [['menu', lambda x: print("")]]
BoxLayout:
id: m51
spacing: dp(10)
#padding: dp(20)
pos_hint: {"center_x": .5, "center_y": .43}
orientation: "vertical"
BoxLayout:
id: s_res91
size_hint_y: None #None
height: self.minimum_height
pos_hint: {"center_x": .5, "center_y": .4}
orientation: "vertical"
RecycleView:
id: rvvv91
key_viewclass: 'viewclass'
key_size: 'height'
RecycleBoxLayout:
id: jbsidis1
padding: dp(10)
spacing: dp(1)
#default_size: None, dp(75)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
#######Class Audio List########
<ItemConfirm0>:
source: app.w "bbay.png"
<ItemConfirm>:
markup: True
ItemConfirm0:
'''
from kivymd.uix.list import ImageLeftWidget
class ItemConfirm0(ImageLeftWidget):
pass
class ItemConfirm(OneLineIconListItem):
divider = None
source = StringProperty()
text= StringProperty()
us=None
## def on_release(self):
## b=str(self.text)
## us=self.ids['cy_']
## us.text=b
## us.md_bg_color= [.1,.8,1,.9]
## return 0
def set_icon(self, instance_check):
instance_check.active = True
check_list = instance_check.get_widgets(instance_check.group)
for check in check_list:
if check != instance_check:
check.active = False
pass
##################################
#MenuNaibDrawerChooser
class blanks1(BoxLayout):
pass
class ContentNavigationDrawer(BoxLayout):
pass
class On1(MDCard):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def vn1(self):
user20=self.ids['user20']
print(user20.text)
try:
xx=open(app.w "cookie.dic").read()
f=str(xx).split("]")
z="nn[b][color=ffffff] " str(f[1]) "n[/b] [color=ffffff]Cód. de Vendedor: " str(f[2]) "n[color=ffffff] Idioma: n"
self.ids.user20.text=z
except Exception as e:
print(str(e))
def vn(self):
Clock.schedule_once(lambda x:self.vn1(),5)
pass
class ItemDrawer(OneLineIconListItem):
divider=None
icon = StringProperty()
text_color = ListProperty((0, 0, 0, 1))
class ExpressBay(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.snackbar = None
self.w = worm
self.rr = 10
self.t="jbsidis"
self.sb="15sp" #"15sp"
self.mdc=[0,0,0, .1]
self.tos="hello"
def build(self):
#self.theme_cls.theme_style="Dark"
global app
app=MDApp.get_running_app()
#Clock.schedule_once(lambda x:app.io1(),10)
#Clock.schedule_once(lambda x:app.io2(),10)
return Builder.load_string(KV)
def n1(self,t,m):
try:
plyer.notification.notify(title=t, message=m, app_icon=app.w "xbay.png")
except Exception as e:
print(str(e))
def n(self, title='', message='', app_name='', app_icon='',timeout=10, ticker='', toast=False):
plyer.notification.notify(title=title, message=message, app_name=app_name, app_icon=app_icon,timeout=timeout, ticker=ticker, toast=toast)
app.v(0.2)
def hilo(self,fx):
import threading
#thread = threading.Thread(target = app.sound_list, args=(types,))
thread = threading.Thread(target = fx)
thread.daemon = True
thread.start()
#app.hilo(app.camx())
def se(self,a):
if os.path.isfile(app.w "set.dic")==True and a==0:
h=open(app.w "set.dic").read().split("]")[0]
return str(h)
if os.path.isfile(app.w "set.dic")==True and a==1:
h=open(app.w "set.dic").read().split("]")[1]
return str(h)
return ""
def check(self, obj):
rs = "s"
try:
rs = getinfo(obj)
except:
pass
if rs == "Unknown Error":
toast('Unknown Error')
else:
try:
r = json.loads(rs)
version = str(r['version'])
country = str(r['city']) " " str(r['region']) " " str(r['country_name'])
lit = str(r['latitude'])
long = str(r['longitude'])
self.root.ids.result.text = "n" self.root.ids.target_ip.text "nn" version "nn" country "nn" lit "nn" long
except Exception as e:
version = str("New version")
country = str("New Country") " " str("New City") " " str("New Name")
lit = str("1.232323")
long = str("3437434")
self.root.ids.result.text = "n" self.root.ids.target_ip.text "nn" version "nn" country "nn" lit "nn" long
def tagm(self):
#On1().vn()
try:
xx=open(app.w "cookie.dic").read()
f=str(xx).split("]")
z="nn[b][color=ffffff] " str(f[1]) "n[/b] [color=ffffff]Cód. de Vendedor: " str(f[2]) "n[color=ffffff] Idioma: n"
#self.ids.user20.text=z
self.root.ids.content_drawer.ids.user20.text=z
app.root.ids.rvvv9.data=[]
#fffapp.root.ids.rvvv9.data.append({"viewclass": "Operas","markup": True}) #.data.append({"viewclass": "VenderRecargas","markup": True})
## self.root.ids.rvvv9.data.append({"viewclass": "pp1","markup": True})
## self.root.ids.rvvv9.data.append({"viewclass": "Operas","markup": True})
except Exception as e:
print(str(e))
def v(self,s):
try:
plyer.vibrator.vibrate(s)
except Exception as e:
print(str(e))
def action(self,a):
man=['chat-processing', 'shield-airplane-outline', 'history', 'bitcoin', 'bell-ring-outline', 'face-profile', 'credit-card-plus-outline', 'account-circle-outline', 'pencil', 'logout']
if a==man[-1]:
app.root.ids.nav_drawer.toggle_nav_drawer()
app.notti(title="",text="Sesión finalizada")
#app.root.ids.MainActivity.current = "loginpage"
def on_start(self):
#self.theme_cls.theme_style="Dark"
icons_item = {
"chat-processing": "[color=999999]jbsidis1",
"shield-airplane-outline": "[color=999999]jbsidis2",
"history": "[color=999999]jbsidis3",
"bitcoin": "[color=999999]jbsidis4",
"bell-ring-outline": "[color=999999]jbsidis5",
"face-profile": "[color=999999]jbsidis6",
"credit-card-plus-outline": "[color=999999]jbsidis7",
"account-circle-outline": "[color=999999]jbsidis8",
"pencil": "[color=999999]jbsidis9",
"logout": "[color=999999]jbsidis10"
}
#app.root.ids.MainActivity.add_widget(Login())
#app.root.ids.MainActivity.current = "login"
#app.root.ids.MainActivity.add_widget(LoginPage())
#app.root.ids.MainActivity.add_widget(Menu1())
#app.root.ids.MainActivity.add_widget(RegisterPage())
#app.root.ids.MainActivity.add_widget(Camera1())
#app.root.ids.MainActivity.current = "login"
for icon_name in icons_item.keys():
self.root.ids.content_drawer.ids.md_list.add_widget(
ItemDrawer(icon=icon_name, text=icons_item[icon_name])
)
def notti(self,title="",text=""):
self.dialog=MDDialog(
title=title,
text="[color=000000]" text,
radius=[self.rr,self.rr,self.rr,self.rr],
buttons=[
MDFlatButton(
text="ACEPTAR",
text_color=[0,1,0,1],
on_release=lambda x:(self.dialog.dismiss(force=True))),
],
)
self.dialog.open()
ExpressBay().run()