I just started coding and I'm doing a test app, but I can't change tabs when I click the button. I needed to check if something was written in the text input, if the date was selected, and if these two options are correct, I wanted the "CalculaCompra" tab to open when clicking the button. Can anyone help me with this? I've been racking my brain for two days with this... kkkkk
Python code
from kivymd.app import MDApp
from kivymd.uix.floatlayout import MDFloatLayout
from kivymd.uix.picker import MDDatePicker
from kivymd.uix.tab import MDTabsBase, MDTabs
from datetime import datetime
from locale import setlocale, LC_ALL
import sqlite3
class VamosComprar(MDApp):
def build(self):
pass
def on_start(self):
self.root.ids.tabs.add_widget(MenuApp(tab_label_text='Inicio'))
self.root.ids.tabs.add_widget(CalculaCompra(tab_label_text='Calculadora'))
self.root.ids.tabs.add_widget(VerListaCompra(tab_label_text='Listas'))
def on_tab_switch(self, instance_tabs, instance_tab, instance_tab_label, tab_text):
pass
def irpracalcula(self):
self.on_tab_switch = CalculaCompra(tab_label_text='Calculadora')
self.on_tab_switch.to_widget()
# def irpracalcula(self, tacerto=False):
# # verif = MenuApp()
# # verif.criarLista()
# self.tacerto = tacerto
# try:
# if self.tacerto:
# self.root.ids.tabs.on_tab_switch(tab_label_text='Calculadora')
# except Exception as erro:
# print(erro)
class MenuApp(MDFloatLayout, MDTabsBase):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def criarLista(self):
self.nomedalista = self.ids.nomedalista.text
self.ondevaicomprar = self.ids.ondevaicomprar.text
self.datalabel = self.ids.datamenu.text
if len(self.nomedalista) <= 0:
self.ids.nomedalista.error = True
self.ids.nomedalista.text = ' '
if self.datalabel == 'Insira a data':
self.ids.datamenu.theme_text_color = 'Error'
if len(self.nomedalista) > 0 and self.datalabel != 'Insira a data':
self.abas = MDTabs()
self.abas.ids.tabs.switch_tab(CalculaCompra)
# ircalc = VamosComprar()
# ircalc.irpracalcula(tacerto=True)
# try:
# self.nomedalistadb = self.nomedalista.replace(" ", "_")
# banco = sqlite3.connect('listComp.db')
# cursor = banco.cursor()
# cursor.execute('CREATE TABLE IF NOT EXISTS ' + self.nomedalistadb + ' ('
# 'id INTEGER PRIMARY KEY AUTOINCREMENT,'
# 'mercado TEXT,'
# 'data TEXT,'
# 'produto TEXT,'
# 'quantidade REAL,'
# 'precounit REAL,'
# 'qtdatac REAL,'
# 'precoatac REAL,'
# 'total REAL'
# ')')
#
# banco.close()
# return print('Feito tudo certo')
# except sqlite3.Error as erro:
# print(f'Erro: {erro}')
def abrir_data(self):
setlocale(LC_ALL, 'pt_BR.utf-8')
date_dialog = MDDatePicker()
date_dialog.bind(on_save=self.on_save, on_cancel=self.on_cancel)
date_dialog.open()
# Quando salvar data muda na label
def on_save(self, instance, value, date_range):
# print(instance, type(value), date_range, type(str(value)))
self.dtlabelpos = f'{str(value.day)}/{str(value.month)}/{str(value.year)}'
self.ids.datamenu.text = self.dtlabelpos
def on_cancel(self, instance, value):
pass
class CalculaCompra(MDFloatLayout, MDTabsBase):
def __init__(self, **kwargs):
super().__init__(**kwargs)
class VerListaCompra(MDFloatLayout, MDTabsBase):
def __init__(self, **kwargs):
super().__init__(**kwargs)
VamosComprar().run()
Code Kivy:
MDBoxLayout:
orientation: 'vertical'
MDToolbar:
title: 'Vamos Comprar?'
anchor_title: 'center'
MDTabs:
id: tabs
tab_hint_x: True
on_tab_switch: app.on_tab_switch(*args)
<MenuApp>
id: menuapp
MDBoxLayout:
padding: 40, 40, 40, 40
margin: 40, 40
orientation: 'vertical'
MDLabel:
id: titulomenu
text: "Criar lista de Compras"
halign: "center"
font_size: 24
MDTextField:
id: nomedalista
hint_text: "Nome da Lista"
required: True
helper_text_mode: "on_error"
helper_text: 'Insira um nome para a lista'
MDTextField:
id: ondevaicomprar
hint_text: "Onde irá fazer a compra?"
MDBoxLayout:
padding: 50, 50
MDLabel:
id: datamenu
index: 0
text: "Insira a data"
valign: 'middle'
MDIconButton:
index: 1
icon: 'calendar-month-outline'
valign: 'middle'
on_release: root.abrir_data()
MDRaisedButton
id: btncriarlista
text: 'Criar Lista'
size_hint_x: .8
pos_hint: {"center_x": .5, "center_y": .5}
on_release: root.criarLista()
<CalculaCompra>
id: calculacompra
MDBoxLayout:
padding: 40, 40, 40, 40
margin: 40, 40
orientation: 'vertical'
MDLabel:
id: titulocalcula
text: "Preencha os campos"
halign: "center"
font_size: 24
MDTextField:
id: produto
hint_text: "Produto"
required: True
helper_text_mode: "on_error"
helper_text: 'Insira o nome do produto'
MDTextField:
id: quantidade
hint_text: "Quantidade"
MDTextField:
id: precounit
hint_text: "Preço unit."
MDBoxLayout:
padding: 50, 50
MDLabel:
id: total
index: 0
text: "Total"
valign: 'middle'
MDIconButton:
index: 1
icon: 'cart-plus'
valign: 'middle'
on_release:
MDRaisedButton
text: 'Ver Lista'
size_hint_x: .8
pos_hint: {"center_x": .5, "center_y": .5}
on_release:
<VerListaCompra>
MDLabel:
id: label
text: "ver lista"
halign: "center"
I wanted that when I click on the button, it would check if there is something written in the text and if the date was selected, and when I click on the button, go to the next tab.