Created
May 17, 2021 13:28
-
-
Save CoutinhoElias/e4231906f65de4493ebca02db3ff45e6 to your computer and use it in GitHub Desktop.
multi_screen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' MAIN FILE''' | |
from kivy.lang import Builder | |
from kivy.uix.boxlayout import BoxLayout | |
from kivy.properties import ObjectProperty | |
from kivy.properties import StringProperty | |
from kivy.uix.screenmanager import Screen | |
from kivymd.uix.card import MDCardSwipe | |
from kivymd.uix.list import TwoLineListItem, OneLineListItem | |
import json, requests | |
from kivy.core.window import Window | |
from models import Codigos, Produto, Contagem | |
# Imports para firebase-admin | |
import firebase_admin | |
from firebase_admin import credentials | |
from firebase_admin import auth | |
from firebase_admin import db | |
from kivymd.app import MDApp | |
class ContentNavigationDrawer(BoxLayout): | |
screen_manager = ObjectProperty() | |
nav_drawer = ObjectProperty() | |
class SwipeToDeleteItem(MDCardSwipe): | |
text = StringProperty() | |
class ScreenContagem(Screen): | |
def on_kv_post(self, base_widget): | |
q = Produto.select(Produto.cdchamada, Produto.nmproduto) | |
for row in q.dicts()[:20]: | |
items = SwipeToDeleteItem(text=str(row['cdchamada'])) | |
self.ids.produtos_da_contagem.add_widget(items) | |
def remove_item(self, instance): | |
self.ids.produtos_da_contagem.remove_widget(instance) | |
class ScreenImportarDados(Screen): | |
def importa_produtos_nuvem(self): | |
# Usando REQUESTS FUNCIONA | |
self.url = "https://inventarioshop-8318f-default-rtdb.firebaseio.com/.json/?auth=gdtejdlLSHhgjFHG" | |
resp = requests.get(url = self.url, json = {'create':True}) | |
data = resp.json() | |
for produto, detalhes in data.items(): | |
for registro, dict_detalhe in detalhes.items(): | |
print(registro, dict_detalhe['codigos']) | |
for tipo, descricao in dict_detalhe['codigos'].items(): | |
if tipo == 'EAN13': | |
print(descricao) | |
Codigos.create( | |
dscodigo = descricao, | |
idproduto = dict_detalhe['idproduto'], | |
tpcodigo = tipo | |
) | |
else: | |
Codigos.create( | |
dscodigo = descricao, | |
idproduto = dict_detalhe['idproduto'], | |
tpcodigo = tipo | |
) | |
Produto.create( | |
cdchamada = descricao, | |
idproduto = dict_detalhe['idproduto'], | |
nmproduto = dict_detalhe['nmproduto'], | |
stativo = dict_detalhe['stativo'] | |
) | |
self.manager.current = 'tela_digitada' | |
class ScreenDigitado(Screen): | |
# Define o foco no objeto que eu desejo | |
def on_kv_post(self, obj): | |
self.ids.codigo_digitado.focus = True | |
self.ids.quantidade_digitada.text = '0' | |
self.recreate_tables() | |
def somar(self): | |
self.ids.quantidade_digitada.text = str(float(self.ids.quantidade_digitada.text) + 1) | |
def subtrair(self): | |
if (float(self.ids.quantidade_digitada.text) - 1) >= 0: | |
self.ids.quantidade_digitada.text = str(float(self.ids.quantidade_digitada.text) - 1) | |
def pesquisa_produto(self): | |
self.ids.codigo_digitado.text = self.ids.codigo_digitado.text.strip().zfill(6) | |
join_cadastros = Produto.select(). \ | |
join(Codigos, on=(Codigos.idproduto_id == Produto.idproduto)). \ | |
where(Codigos.dscodigo == self.ids.codigo_digitado.text) | |
try: | |
consulta = join_cadastros.get() | |
contagem = Contagem.select().where(Contagem.idproduto == consulta.idproduto).get() | |
self.ids.quantidade_digitada.text = str(contagem.quantidade) | |
self.ids.bt_salva_edita.text = 'Editar' | |
except: | |
self.ids.bt_salva_edita.text = 'Salvar' | |
print('DEU ERRADO! 7897144000583') | |
if join_cadastros.count() > 0: | |
consulta = join_cadastros.get() | |
self.ids.lbl_descricao_produto.text = consulta.nmproduto | |
else: | |
print('Produto não encontrado!') | |
self.ids.lbl_descricao_produto.text = 'Produto não encontrado!' | |
def salvar(self): | |
produto = Produto.select(). \ | |
join(Codigos, on=(Codigos.idproduto_id == Produto.idproduto)). \ | |
where(Codigos.dscodigo == self.ids.codigo_digitado.text) | |
consulta = produto.get() | |
try: | |
if (self.ids.bt_salva_edita.text) == 'Salvar': | |
Contagem.create( | |
idproduto = consulta.idproduto, | |
quantidade = self.ids.quantidade_digitada.text | |
) | |
else: | |
contagem = Contagem.update( | |
idproduto = consulta.idproduto, | |
quantidade = self.ids.quantidade_digitada.text | |
) | |
contagem.execute() | |
self.ids.lbl_descricao_produto.text = '' | |
self.ids.codigo_digitado.text = '' | |
except: | |
self.ids.lbl_descricao_produto.text = 'Produto não encontrado!' | |
self.ids.quantidade_digitada.text = '0' | |
def recreate_tables(self): | |
Codigos.create_table() | |
class TestNavigationDrawer(MDApp): | |
def build(self): | |
self.theme_cls.primary_palette = 'LightBlue' | |
return Builder.load_string(KV) | |
#return Builder.load_file('main.kv') | |
TestNavigationDrawer().run() | |
'''KV FILE''' | |
<ContentNavigationDrawer>: | |
orientation: "vertical" | |
FloatLayout: | |
size_hint_y: None | |
height: "200dp" | |
BoxLayout: | |
id: box_image | |
x: root.x | |
pos_hint: {"top": 1} | |
FloatLayout: | |
# Imagem de fundo | |
FitImage: | |
pos_hint: {'center_x': .5, 'center_y': .5} | |
source: "menu.png" | |
# Imagem do usuário | |
FitImage: | |
pos_hint: {'center_x': .5, 'center_y': .5} | |
source: "python.png" | |
size_hint: None, None | |
width: dp(150) | |
height: dp(150) | |
radius: [99, 99, 99, 99] | |
MDLabel: | |
text: "Header Text" | |
size_hint_y: None | |
height: self.texture_size[1] | |
# Define a posição do Label | |
x: root.x + 10 | |
y: root.height - box_image.height + dp(10) | |
ScrollView: | |
MDList: | |
OneLineListItem: | |
text: "Contagem" | |
on_press: | |
root.nav_drawer.set_state("close") | |
root.screen_manager.current = 'tela_contagem2' | |
root.screen_manager.transition.direction = 'left' | |
OneLineListItem: | |
text: "Contagem" | |
on_press: | |
root.nav_drawer.set_state("close") | |
root.screen_manager.current = 'tela_contagem' | |
root.screen_manager.transition.direction = 'left' | |
OneLineListItem: | |
text: "Lançar/Editar Manualmente" | |
on_press: | |
root.nav_drawer.set_state("close") | |
root.screen_manager.current = 'tela_digitada' | |
root.screen_manager.transition.direction = 'left' | |
OneLineListItem: | |
text: "Importando cadastros" | |
on_press: | |
root.nav_drawer.set_state("close") | |
root.screen_manager.current = 'tela_importacao' | |
root.screen_manager.transition.direction = 'right' | |
OneLineListItem: | |
text: "Lendo códigos" | |
on_press: | |
root.nav_drawer.set_state("close") | |
root.screen_manager.current = 'tela_cam' | |
root.screen_manager.transition.direction = 'right' | |
# Criamos ItemImage com (TwoLineAvatarListItem) para que possamos usar em qualquer lista com qualquer imagen. | |
<ItemImage>: | |
AsyncImageLeftWidget: | |
source: root.source | |
canvas: | |
# Campo criado herdando de MDTextField | |
<CampoTexto@MDTextField>: | |
size_hint: .9, None | |
mode: "rectangle" | |
# Botão criado herdando de MDRaisedButton | |
<BotaoRedondo@MDFillRoundFlatButton>: | |
size_hint: .9, None | |
md_bg_color: app.theme_cls.primary_dark | |
#bg_hint_color: app.theme_cls.primary_light | |
#color: app.theme_cls.primary_dark | |
# Campo criado herdando de MDTextFieldRound | |
<CampoArredondado@MDTextFieldRound>: | |
font_size: '25sp' | |
normal_color: app.theme_cls.primary_dark | |
color_active: app.theme_cls.primary_dark | |
halign: 'center' | |
multiline: False | |
Screen: | |
MDToolbar: | |
id: toolbar | |
pos_hint: {"top": 1} | |
elevation: 10 | |
title: "App Zazá" | |
left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]] | |
MDNavigationLayout: | |
x: toolbar.height | |
ScreenManager: | |
id: screen_manager | |
ScreenContagem | |
ScreenContagem2 | |
ScreenDigitado | |
ScreenImportarDados | |
MDNavigationDrawer: | |
id: nav_drawer | |
ContentNavigationDrawer: | |
screen_manager: screen_manager | |
nav_drawer: nav_drawer | |
#Isso pode demorar muito.Portanto, ao clicar em IMPORTAR aguarde até que o aplicativo mude de tela.' | |
<ScreenImportarDados> | |
name: 'tela_importacao' | |
id: 'tela_importacao' | |
FloatLayout: | |
orientation: 'vertical' | |
MDLabel: | |
id: lbl_aviso | |
text: 'A T E N Ç Ã O' | |
halign: "center" | |
font_size: '30sp' | |
pos_hint: {'center_x': 0.5, 'center_y': 0.8} | |
MDLabel: | |
id: lbl_aviso | |
text: 'Você está prestes a recuperar muitos dados.' | |
#halign: "center" | |
font_size: '30sp' | |
pos_hint: {'center_x': 0.6, 'center_y': 0.5} | |
BotaoRedondo: | |
id: bt_importar | |
text: 'Importar' | |
pos_hint: {'center_x': .5, 'center_y': .2} | |
size_hint: .9, .09 | |
on_release: root.importa_produtos_nuvem() | |
#Para usar com a Screen <ScreenContagem> | |
<SwipeToDeleteItem>: | |
size_hint_y: None | |
height: content.height | |
MDCardSwipeLayerBox: | |
padding: "8dp" | |
MDIconButton: | |
icon: "trash-can" | |
pos_hint: {"center_y": .5} | |
#on_release: root.remove_item(root) | |
#on_release: app.root.ids.screen_manager.get_screen('tela_contagem').ids | |
on_release: print(app.root.ids.screen_manager.get_screen('tela_contagem').ids.tela_contagem.remove_item(root)) | |
MDCardSwipeFrontBox: | |
OneLineListItem: | |
id: content | |
text: root.text | |
_no_ripple_effect: True | |
# Screen to be accessed. *(two) | |
<ScreenContagem> | |
name: 'tela_contagem' | |
id: 'tela_contagem' | |
FloatLayout: | |
orientation: 'vertical' | |
CampoArredondado: | |
id: codigo_digitado | |
hint_text: 'Código' | |
input_type: 'number' | |
input_filter: 'int' | |
pos_hint: {'center_x': 0.5, 'center_y': 0.8} | |
size_hint: .8, .09 | |
#on_text_validate: root.pesquisa_produto() | |
ScrollView: | |
pos_hint: {'center_x': 0.5, 'center_y': 0.2} | |
MDList: | |
id: produtos_da_contagem | |
padding: 0 | |
<ScreenDigitado> | |
name: 'tela_digitada' | |
id: 'tela_digitada' | |
FloatLayout: | |
orientation: 'vertical' | |
CampoArredondado: | |
id: codigo_digitado | |
hint_text: 'Código' | |
input_type: 'number' | |
input_filter: 'int' | |
pos_hint: {'center_x': 0.5, 'center_y': 0.8} | |
size_hint: .8, .09 | |
on_text_validate: root.pesquisa_produto() | |
MDLabel: | |
id: lbl_descricao_produto | |
text: "Descrição do Produto" | |
halign: "center" | |
font_size: '30sp' | |
pos_hint: {'center_x': 0.5, 'center_y': 0.6} | |
font_size: '30sp' | |
CampoArredondado: | |
id: quantidade_digitada | |
hint_text: 'Quantidade' | |
input_type: 'number' | |
input_filter: 'float' | |
pos_hint: {'center_x': 0.5, 'center_y': 0.4} | |
size_hint: .45, .09 | |
MDFloatingActionButton: | |
icon: "minus" | |
md_bg_color: '#FF0000' | |
pos_hint: {'center_x': 0.1, 'center_y': 0.4} | |
elevation_normal: 12 | |
on_release: root.subtrair() | |
MDFloatingActionButton: | |
icon: "plus" | |
md_bg_color: app.theme_cls.primary_dark | |
pos_hint: {'center_x': 0.9, 'center_y': 0.4} | |
elevation_normal: 12 | |
on_release: root.somar() | |
BotaoRedondo: | |
id: bt_salva_edita | |
text: 'Salvar' | |
pos_hint: {'center_x': .5, 'center_y': .2} | |
size_hint: .9, .09 | |
on_release: root.salvar() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment