how can i add an if condition to my app in kivymd - python

So the main issue with this code is that yeah sure it works fine but only displays onto the console instead of the kivy app.
def my_func(self):
open_p = self.root.get_screen("tab").ids.op.text
volume = self.root.get_screen("tab").ids.vl.text
hi = self.root.get_screen("tab").ids.hi.text
lo = self.root.get_screen("tab").ids.lo.text
model = pickle.load(open('ustech_model', 'rb'))
input_dt = (open_p, volume, lo, hi)
inpt_val = np.asarray(input_dt, dtype=float)
reshape_vals = inpt_val.reshape(1, -1)
anticipate = model.predict(reshape_vals)
# return anticipate.round(2)
if inpt_val[0] < anticipate:
print(f"Buy!! & TP:{anticipate.round(2)}")
else:
print(f"Sell!! & TP:{anticipate.round(2)}")
how can i change the output to include the text i want to be displayed on my main application
here is the rest of the code:
import numpy as np
from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.uix.screenmanager import MDScreenManager
from kivymd.uix.tab import MDTabsBase
from kivymd.uix.floatlayout import MDFloatLayout
from kivy.core.window import Window
import pickle
import warnings
# warnings.filterwarnings('ignore', category="FutureWarning")
warnings.filterwarnings('ignore', category=UserWarning)
Window.size = (400, 600)
KV = """
MDScreenManager:
HomeScreen:
Main:
<HomeScreen>:
name:'menu'
MDRectangleFlatIconButton:
text: "WELCOME"
icon: "distribute-horizontal-center"
line_color: 0, 0, 0, 0
pos_hint: {"center_x": .5, "center_y": .5}
font_size: 25
# font_name: "branda.ttf"
MDIconButton:
icon: "location-enter"
pos_hint: {'center_x': 0.5, 'center_y': 0.4}
on_press:
root.manager.current = 'tab'
root.manager.transition.direction = "left"
MDLabel:
text: "REMEMBER TO ALWAYS BE HUMBLE!!"
halign: "center"
pos_hint: {'center_y': 0.1}
font_size: 12
<Main>:
name: 'tab'
MDBoxLayout:
orientation: "vertical"
MDTopAppBar:
title: "TRADEMAVERICKS"
anchor_title: 'center'
right_action_items:
[["distribute-horizontal-center"]]
MDTabs:
id: tabs
allow_stretch: 'True'
on_tab_switch: root.on_tab_switch(*args)
# NAS100
Tab:
title: "NAS100"
MDGridLayout:
cols: 2
padding: dp(36)
spacing: dp(20)
MDTextField:
id: op
hint_text: "OPEN"
mode: "line"
pos_hint: {"center_x": .5, "center_y": .9}
size_hint_x: None
width: 150
MDTextField:
id: vl
hint_text: "VOLUME"
mode: "line"
pos_hint: {"center_x": .5, "center_y": .7}
size_hint_x: None
width: 150
MDTextField:
id: lo
hint_text: "LOW"
mode: "line"
pos_hint: {"center_x": .5, "center_y": .5}
size_hint_x: None
width: 150
MDTextField:
id: hi
hint_text: "HIGH"
mode: "line"
pos_hint: {"center_x": .5, "center_y": .3}
size_hint_x: None
width: 150
MDRaisedButton:
text: "SUBMIT"
on_release:
app.func()
# if inpt_val[0] < anticipate:
# print(f"Buy!! & TP:{anticipate.round(2)}")
# else:
# print(f"Sell!! & TP:{anticipate.round(2)}")
MDCard:
size_hint: None, None
size: "325dp", "150dp"
pos_hint: {"center_x": .5, "center_y": .32}
radius: 5
MDLabel:
text: ""
id: put_txt_here
halign: 'center'
pos_hint: {"center_x": .5, "center_y": .6}
MDIconButton:
icon: "home"
icon_size: "40"
pos_hint: {"center_x": .8, "center_y": .1}
on_press:
root.manager.current = 'menu'
root.manager.transition.direction = "right"
# US30
Tab:
title: "US30"
MDGridLayout:
cols: 2
padding: dp(36)
spacing: dp(20)
MDTextField:
id: o
hint_text: "OPEN"
mode: "line"
pos_hint: {"center_x": .5, "center_y": .9}
size_hint_x: None
width: 150
MDTextField:
id: v
hint_text: "VOLUME"
mode: "line"
pos_hint: {"center_x": .5, "center_y": .7}
size_hint_x: None
width: 150
MDTextField:
id: l
hint_text: "LOW"
mode: "line"
pos_hint: {"center_x": .5, "center_y": .5}
size_hint_x: None
width: 150
MDTextField:
id: h
hint_text: "HIGH"
mode: "line"
pos_hint: {"center_x": .5, "center_y": .3}
size_hint_x: None
width: 150
MDRaisedButton:
text: "SUBMIT"
on_release:
app.us30()
MDCard:
size_hint: None, None
size: "325dp", "150dp"
pos_hint: {"center_x": .5, "center_y": .32}
radius: 5
MDLabel:
text: ""
id: put_us30
halign: 'center'
pos_hint: {"center_x": .5, "center_y": .6}
MDIconButton:
icon: "home"
icon_size: "40"
pos_hint: {"center_x": .8, "center_y": .1}
on_press:
root.manager.current = 'menu'
root.manager.transition.direction = "right"
# GER30
Tab:
title: "GER30"
MDGridLayout:
cols: 2
padding: dp(36)
spacing: dp(20)
MDTextField:
id: op_1
hint_text: "OPEN"
mode: "line"
pos_hint: {"center_x": .5, "center_y": .9}
size_hint_x: None
width: 150
MDTextField:
id: vol_1
hint_text: "VOLUME"
mode: "line"
pos_hint: {"center_x": .5, "center_y": .7}
size_hint_x: None
width: 150
MDTextField:
id: lo_1
hint_text: "LOW"
mode: "line"
pos_hint: {"center_x": .5, "center_y": .5}
size_hint_x: None
width: 150
MDTextField:
id: hi_1
hint_text: "HIGH"
mode: "line"
pos_hint: {"center_x": .5, "center_y": .3}
size_hint_x: None
width: 150
MDRaisedButton:
text: "SUBMIT"
on_release:
app.ger30()
MDCard:
size_hint: None, None
size: "325dp", "150dp"
pos_hint: {"center_x": .5, "center_y": .32}
radius: 5
MDLabel:
text: ""
id: put_ger30
halign: 'center'
pos_hint: {"center_x": .5, "center_y": .6}
MDIconButton:
icon: "home"
icon_size: "40"
pos_hint: {"center_x": .8, "center_y": .1}
on_press:
root.manager.current = 'menu'
root.manager.transition.direction = "right"
<Tab>:
MDLabel:
id: label
halign: "center"
"""
class Tab(MDFloatLayout, MDTabsBase):
pass
class HomeScreen(Screen):
pass
class Main(Screen):
def on_tab_switch(self, instance_tabs, instance_tab, instance_tab_label, tab_text):
'''Called when switching tabs.
:type instance_tabs: <kivymd.uix.tab.MDTabs object>;
:param instance_tab: <main.Tab object>;
:param instance_tab_label: <kivymd.uix.tab.MDTabsLabel object>;
:param tab_text: text or name icon of tab;
'''
# instance_tab.ids.label.text = tab_text
# 'Red', 'Pink', 'Purple', 'DeepPurple', 'Indigo', 'Blue', 'LightBlue', 'Cyan', 'Teal', 'Green',
# 'LightGreen', 'Lime', 'Yellow', 'Amber', 'Orange', 'DeepOrange', 'Brown', 'Gray', 'BlueGray'
class TM(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.material_style = "M3"
self.theme_cls.primary_palette = "Amber"
self.icon = "logo.png"
Builder.load_string(KV)
sm = MDScreenManager()
sm.add_widget(HomeScreen(name='menu'))
sm.add_widget(Main(name='tab'))
return sm
# For NAS100
def my_func(self):
open_p = self.root.get_screen("tab").ids.op.text
volume = self.root.get_screen("tab").ids.vl.text
hi = self.root.get_screen("tab").ids.hi.text
lo = self.root.get_screen("tab").ids.lo.text
model = pickle.load(open('ustech_model', 'rb'))
input_dt = (open_p, volume, lo, hi)
inpt_val = np.asarray(input_dt, dtype=float)
reshape_vals = inpt_val.reshape(1, -1)
anticipate = model.predict(reshape_vals)
# return anticipate.round(2)
if inpt_val[0] < anticipate:
print(f"Buy!! & TP:{anticipate.round(2)}")
else:
print(f"Sell!! & TP:{anticipate.round(2)}")
def func(self):
self.root.get_screen("tab").ids.put_txt_here.text = str(self.my_func())
# For US30
def my_us30(self):
o = self.root.get_screen("tab").ids.o.text
v = self.root.get_screen("tab").ids.v.text
h = self.root.get_screen("tab").ids.h.text
l = self.root.get_screen("tab").ids.l.text
model = pickle.load(open('us30_model', 'rb'))
input_dt = (o, v, h, l)
inpt_val = np.asarray(input_dt, dtype=float)
reshape_vals = inpt_val.reshape(1, -1)
anticipate = model.predict(reshape_vals)
return anticipate.round(2)
# if inpt_val[0] < anticipate:
# print(f"Buy!! & TP:{anticipate.round(2)}")
# else:
# print(f"Sell!! & TP:{anticipate.round(2)}")
def us30(self):
self.root.get_screen("tab").ids.put_us30.text = str(self.my_us30())
# For GER30
def my_ger30(self):
op_1 = self.root.get_screen("tab").ids.op_1.text
vol_1 = self.root.get_screen("tab").ids.vol_1.text
hi_1 = self.root.get_screen("tab").ids.hi_1.text
lo_1 = self.root.get_screen("tab").ids.lo_1.text
model = pickle.load(open('us30_model', 'rb'))
input_dt = (op_1, vol_1, hi_1, lo_1)
inpt_val = np.asarray(input_dt, dtype=float)
reshape_vals = inpt_val.reshape(1, -1)
anticipate = model.predict(reshape_vals)
return anticipate.round(2)
# if inpt_val[0] < anticipate:
# print(f"Buy!! & TP:{anticipate.round(2)}")
# else:
# print(f"Sell!! & TP:{anticipate.round(2)}")
def ger30(self):
self.root.get_screen("tab").ids.put_ger30.text = str(self.my_ger30())
TM().run()
I tried to change the print to the use the id for the label but it threw an error that it couldnt be found in the function

For ger30:
# For GER30
def my_ger30(self):
op_1 = self.root.get_screen("tab").ids.op_1.text
vol_1 = self.root.get_screen("tab").ids.vol_1.text
hi_1 = self.root.get_screen("tab").ids.hi_1.text
lo_1 = self.root.get_screen("tab").ids.lo_1.text
model = pickle.load(open('us30_model', 'rb'))
input_dt = (op_1, vol_1, hi_1, lo_1)
inpt_val = np.asarray(input_dt, dtype=float)
reshape_vals = inpt_val.reshape(1, -1)
anticipate = model.predict(reshape_vals)
if inpt_val[0] < anticipate:
self.root.get_screen("tab").ids.put_ger30.text = f"Buy!! & TP:{anticipate.round(2)}"
else:
self.root.get_screen("tab").ids.put_ger30.text = f"Sell!! & TP:{anticipate.round(2)}"
def ger30(self):
self.my_ger30()

Related

Updating labels with user input from MDTextField

I'm trying to print user input from MDTextField to a label, but I keep getting "AttributeError: 'super' object has no attribute 'getattr'".
Here's my python file:
'''
class TodoLayout(MDScreen, MDFloatLayout):
pass
class CatLayout(Screen):
pass
class WindowManager(ScreenManager):
pass
class BullyCat(MDApp, App):
def get_data(self):
user_input = (self.root.get_screen("to_do_view").ids.data.text)
self.root.get_screen("to_do_view").ids.label_id.text = user_input
def build(self):
return Builder.load_file("kivyfile.kv")
def add_todo(self):
global screen_manager
screen_manager = ScreenManager()
screen_manager.get_screen("main").todo_list.add_widget(TodoCard())
if __name__ == "__main__":
BullyCat().run()
'''
Here's my .kv file:
'''
WindowManager:
TodoLayout:
CatLayout:
<TodoLayout>:
name: "to_do_view"
MDScreen:
id: 'main'
name: 'main'
MDFloatLayout:
md_bg_color: 0, 1, 0, .1
MDLabel:
text: "MyTasks"
pos_hint: {"center_x": .9, "center_y": .95}
font_size: "35sp"
MDLabel:
id: date
text: ""
pos_hint: {"center_x": .885, "center_y": .89}
font_size: "18sp"
MDIconButton:
icon: "plus"
pos_hint: {"center_x": .92, "center_y": .925}
user_font_size: "30sp"
md_bg_color: 30/255, 1, 30/255, 0.8
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
on_release: on_release: root.ids.add_input.text = choice(app.my_list)
MDIconButton:
icon: "cat"
pos_hint: {"center_x": .075, "center_y": .925}
user_font_size: "30sp"
md_bg_color: 30/255, 1, 30/255, 0.8
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
on_release:
app.root.current = "cat_view"
root.manager.transition.direction = "left"
MDTextField:
id: data
hint_text: "Write a task"
pos_hint: {"center_x": .5, "center_y": .1}
size_hint: 0.5, None
MDRectangleFlatButton:
text: 'add'
pos_hint: {"center_x": .8, "center_y": .1}
on_release: app.get_data()
MDLabel:
id: 'label_id'
text: 'Task 1'
pos_hint: {"center_x": .9, "center_y": .8}
font_size: 30
<CatLayout>:
name: 'cat_view'
MDScreen:
id: 'second'
name: 'second'
MDFloatLayout:
md_bg_color: 0, 1, 0, .1
MDIconButton:
icon: "pencil"
pos_hint: {"center_x": .075, "center_y": .925}
user_font_size: "30sp"
md_bg_color: 30/255, 1, 30/255, 0.8
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
on_release:
app.root.current = "to_do_view"
root.manager.transition.direction = "right"
MDTextField:
hint_text: "Persistent helper text"
pos_hint: {"center_x": .5, "center_y": .1}
size_hint: 0.5, None
'''
Ultimately i'm trying to build a to-do app with an animated cat that will bully you verbally into doing your tasks.
I suspect the problem lies within here:
def get_data(self):
user_input = (self.root.get_screen("to_do_view").ids.data.text)
self.root.get_screen("to_do_view").ids.label_id.text = user_input
I am very new to coding and i'm a bit lost here. Thanks a lot!
Ids in your kv should not be enclosed in quotes. Just change:
id: 'label_id'
to:
id: label_id

How to get text from a textfield inside another classin kivy

I'm stuck in this problem, I'm using a dialog so that the user can change his password but i cant access the textfield text from the DashboardGerente class...
main.py
class Content(BoxLayout):
old_pass_tf = ObjectProperty()
new_pass_tf = ObjectProperty()
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.app = MDApp.get_running_app()
def show_passnew(self,widget):
if widget.state == "normal":
self.ids.new_pass.password = True
else:
self.ids.new_pass.password = False
def show_passold(self,widget):
if widget.state == "normal":
self.ids.old_pass.password = True
else:
self.ids.old_pass.password = False
class DashboardGerente(Screen):
dialog = None
cont = Content()
def Logout(self):
wm = MDApp.get_running_app().root
wm.current = "Login"
def teste(self):
print(Login.username)
def show_alert_dialog(self):
if not self.dialog:
self.dialog = MDDialog(
title = "Alterar palavra-passe",
text = "Palavra-passe antiga",
content_cls=Content(),
type="custom",
buttons=[
MDFlatButton(
text="Cancelar",
text_color=rgba(0,0,0,1),
on_release = self.cancelar
),
MDFlatButton(
text="Alterar",
text_color = rgba(0,0,0,1),
on_release = self.change_pass
),
],
)
self.dialog.open()
def cancelar(self,obj):
self.dialog.dismiss()
def change_pass(self, obj):
#i want to access the textfield text here
pass
.kv file
<Content>:
orientation: "vertical"
spacing: "12dp"
size_hint_y: None
height: "120dp"
id: content_dialog_change_pass
MDTextField:
id: old_pass
hint_text: "Palavra-passe antiga"
mode: "rectangle"
required: False
color: 1, 0, 1, 1
line_color_focus: 0.5, 0, 1, 1
icon_right: "shield-key"
size_hint: .9, None
password: True
GridLayout:
cols:2
MDCheckbox:
on_press: root.show_passold(self)
size_hint: None, None
height: 5
width: 20
MDLabel:
text: "Mostrar Palavra-passe"
font_size: "13px"
color: 0,0,0,.4
MDTextField:
id: new_pass
hint_text: "Palavra-passe nova"
mode: "rectangle"
required: False
color: 1, 0, 1, 1
line_color_focus: 0.5, 0, 1, 1
icon_right: "shield-key"
size_hint: .9, None
password: True
GridLayout:
cols:2
MDCheckbox:
on_press: root.show_passnew(self)
size_hint: None, None
height: 10
width: 20
MDLabel:
text: "Mostrar Palavra-passe"
font_size: "13px"
color: 0,0,0,.4
<DashboardGerente#FloatLayout>:
name: "DashboardGerente"
MDBottomNavigation:
panel_color: get_color_from_hex("#c300ff")
text_color: 0,0,0,1
MDBottomNavigationItem: ## Perfil do utilizador
name: 'Profile'
text: 'Perfil'
icon: 'account'
badge_icon: "numeric-10"
MDCard:
border_radius: 20
radius: [15]
size_hint:None, None
size: 300, 400
pos_hint: {"center_x": 0.5, "center_y": 0.5}
elevation: 10
orientation: "vertical"
AnchorLayout:
anchor_x: "left"
anchor_y: "top"
MDIconButton:
id:logout
text_color: 0,0,0,1
on_release: root.Logout(), root.teste()
icon: "account-arrow-left"
GridLayout:
cols: 2
padding: 40,0,0,0
MDLabel:
text: "ID: "
MDLabel:
text: "5000"
MDLabel:
text: "Nome: "
MDLabel:
text: "nome"
MDLabel:
text: "Cargo: "
MDLabel:
text: "cargo"
MDLabel:
text: "Salário: "
MDLabel:
text: "1000"
MDLabel:
text: "Secção: "
MDLabel:
text: "Frutaria"
MDRaisedButton:
text: "Mudar palavra-passe"
md_bg_color: 0.5, 0, 1, 1
on_press: root.teste()
pos_hint: {"center_x": 0.5, "center_y": 0.5}
on_release: root.show_alert_dialog()
MDBottomNavigationItem:
name: 'Providers'
text: 'Fornecedores'
icon: 'truck'
MDLabel:
text: 'Mail'
halign: 'center'
MDBottomNavigationItem:
name: 'Products'
text: 'Produtos'
icon: 'shopping'
badge_icon: "numeric-10"
MDLabel:
text: 'Mail'
halign: 'center'
MDBottomNavigationItem:
name: 'Employes'
text: 'Empregados'
icon: 'account-multiple'
badge_icon: "numeric-10"
MDRaisedButton:
text: "Adicionar funcionários"
md_bg_color: 0.5, 0, 1, 1
pos_hint: {"center_x": 0.5, "center_y": 0.5}
MDBottomNavigationItem:
name: 'Sections'
text: 'Secções'
icon: 'storefront'
badge_icon: "numeric-10"
MDLabel:
text: 'Mail'
halign: 'center'
This are my project files that matter for now, how can i get the .text value from the old_pass textfield in the DashboardGerente class?
I would suggest as follows,
Create a property, say, user_input = StringProperty("") in your App's subclass. then in kv lang of Content class,
MDTextField:
id: old_pass
on_text: app.user_input = self.text
Now use this in any method of DashboardGerente as MDApp.get_running_app().user_input

Get value from .kv class to .py function

I have custom class in main.kv file, and with command I can add to another screen in createRecipe.kv file. But how can I read names.text(from Label), unit.text(from Label) and qtyRe.text(from TextInput) to main.py file?
screen_manager.get_screen('createRecipe').ingredientforRecipe.add_widget(AddIngToRecipe(names=names, unit=unit))
main.kv
<AddIngToRecipe>
id: addIngToRecipe
names: names
unit: unit
MDLabel:
id: names
text: str(root.names)
markup: True
font_size: '20sp'
size_hint_x: .8
pos_hint: {"center_x": .4, "center_y": .4}
MDLabel:
id: unit
text: str(root.unit)
markup: True
font_size: '18sp'
size_hint_x: .8
theme_text_color: "Custom"
text_color: 1, 1, 1, 1
pos_hint: {"center_x": .73, "center_y": .4}
TextInput:
id: qtyRe
hint_text: 'Qty:'
size_hint: 1, None
input_filter: 'int'
pos_hint: {"center_x": .5, "center_y": .5}
height: 55
cursor_color: 75/255, 0/255, 130/255, 1
background_color: 0, 0, 0, 0
multiline: False
createRecipe.kv
MDScreen:
name: 'createRecipe'
ingredientforRecipe: ingredientforRecipe
MDFloatLayout:
id: create_recipe
ScrollView:
do_scroll_y: True
do_scroll_x: False
size_hint_y: .41
pos_hint: {"center_x": .55, "y": 0.41}
bar_width: 0
GridLayout:
id: ingredientforRecipe
cols: 1
height: self.minimum_height
row_default_height: 70
size_hint_y: None
main.py
class AddIngToRecipe(FakeRectangularElevationBehavior, FloatLayout, TouchBehavior):
names = ObjectProperty()
unit = ObjectProperty()
def get_from(self):
names1 = self.ids.qtyRe.text
print(names1)
class PieceofCake(MDApp, Screen):
def create_recipe(self, Renames, Recomment):
self.get_from_addIngToRecipe = AddIngToRecipe()
self.get_from_addIngToRecipe.get_from()
names = self.ids.ingredientforRecipe.qtyRe.text
print(names)
print(Renames)
print(Recomment)
names = self.ids.names.text
unit = self.ids.unit.text
qtyRe = self.ids.qtyRe.text
Further more, if you want to access values of a widget from a different class from the one you are working with by any chance, you can use:
MDApp.get_running_app().root.ids.idOfTheClass.ids.idOfTheWidget.valueToGet
MDApp.get_running_app().root.ids.idOfTheClass acts the same as self for the targeted class

Prevent KivyMD from drawing a widget several times

I am currently building a mobile app with Python and KivyMD. On my application I have an MDExpansionPanel which is called in the on_pre_enter method of the ScreenManager. Naturally this will cause that every time I enter to the same page, the widget will be drawn once again.
Is there a way to delete the previous widget before drawing the new one? I've tried several approaches however I have had no success.
The code for a minimal reproducible example is the following:
Python code:
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.app import MDApp
from kivymd.uix.expansionpanel import MDExpansionPanel, MDExpansionPanelOneLine
from kivy.uix.boxlayout import BoxLayout
class MyContentAliment(BoxLayout):
pass
class MyContentCasetas(BoxLayout):
pass
class MyContentGasolina(BoxLayout):
pass
class MyContentHosped(BoxLayout):
pass
class MyContentVarios(BoxLayout):
pass
class LoginWindow(Screen):
pass
class TravelManagerWindow(Screen):
panel_container = ObjectProperty(None)
# EXPANSION PANEL PARA SOLICITAR GV
def set_expansion_panel(self):
# FOOD PANEL
self.ids.panel_container.add_widget(MDExpansionPanel(icon="food", content=MyContentAliment(),
panel_cls=MDExpansionPanelOneLine(text="Alimentacion")))
# CASETAS PANEL
self.ids.panel_container.add_widget(MDExpansionPanel(icon="food", content=MyContentCasetas(),
panel_cls=MDExpansionPanelOneLine(text="Casetas")))
# GAS PANEL
self.ids.panel_container.add_widget(MDExpansionPanel(icon="food", content=MyContentGasolina(),
panel_cls=MDExpansionPanelOneLine(text="Gasolina")))
# HOSPEDAJE PANEL
self.ids.panel_container.add_widget(MDExpansionPanel(icon="food", content=MyContentHosped(),
panel_cls=MDExpansionPanelOneLine(text="Hospedaje")))
# VARIOS PANEL
self.ids.panel_container.add_widget(MDExpansionPanel(icon="food", content=MyContentVarios(),
panel_cls=MDExpansionPanelOneLine(text="Varios")))
### WINDOW MANAGER ################################
class WindowManager(ScreenManager):
pass
ScreenManager().add_widget(LoginWindow(name='login'))
ScreenManager().add_widget(TravelManagerWindow(name='travelManager'))
class reprodExample(MDApp):
def build(self):
self.theme_cls.primary_palette = "Teal"
return WindowManager()
if __name__ == "__main__":
reprodExample().run()
Kivy code:
:
LoginWindow:
TravelManagerWindow:
<LoginWindow>:
name: 'login'
MDRaisedButton:
text: 'Enter'
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
size_hint: None, None
on_release:
root.manager.transition.direction = 'up'
root.manager.current = 'travelManager'
<TravelManagerWindow>:
name:'travelManager'
on_pre_enter: root.set_expansion_panel()
MDRaisedButton:
text: 'Back'
pos_hint: {'center_x': 0.5, 'center_y': 0.85}
size_hint: None, None
on_release:
root.manager.transition.direction = 'down'
root.manager.current = 'login'
BoxLayout:
orientation: 'vertical'
size_hint:1,0.85
pos_hint: {"center_x": 0.5, "center_y":0.37}
adaptive_height:True
height: self.minimum_height
ScrollView:
adaptive_height:True
GridLayout:
size_hint_y: None
cols: 1
row_default_height: root.height*0.10
height: self.minimum_height
BoxLayout:
adaptive_height: True
orientation: 'horizontal'
GridLayout:
id: panel_container
size_hint_x: 0.6
cols: 1
adaptive_height: True
BoxLayout:
size_hint_x: 0.05
MDCard:
id: resumen_solicitud
size_hint: None, None
size: "250dp", "300dp"
pos_hint: {"top": 0.9, "center_x": .5}
elevation: 0.1
MDBoxLayout:
orientation: 'vertical'
canvas.before:
Color:
rgba: 0.8, 0.8, 0.8, 1
Rectangle:
pos: self.pos
size: self.size
MDLabel:
text: 'Monto Total Solicitado'
font_style: 'Button'
halign: 'center'
font_size: (root.width**2 + root.height**2) / 15.5**4
size_hint_y: 0.2
MDSeparator:
height: "1dp"
MDTextField:
id: suma_solic_viaje
text: "$ 0.00"
bold: True
line_color_normal: app.theme_cls.primary_color
halign: "center"
size_hint_x: 0.8
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
MDSeparator:
height: "1dp"
BoxLayout:
id: expense_graph
halign: 'center'
<MyContentAliment>:
adaptive_height: True
MDBoxLayout:
orientation:'horizontal'
adaptive_height:True
size_hint_x:self.width
pos_hint: {"center_x":0.5, "center_y":0.5}
spacing: dp(10)
padding_horizontal: dp(10)
MDLabel:
text: 'Monto:'
multiline: 'True'
halign: 'center'
pos_hint: {"x":0, "top":0.5}
size_hint_x: 0.15
font_style: 'Button'
font_size: 19
MDTextField:
id: monto_aliment_viaje
hint_text: 'Monto a solicitar'
pos_hint: {"x":0, "top":0.5}
halign: 'left'
size_hint_x: 0.3
helper_text: 'Ingresar el monto a solicitar'
helper_text_mode: 'on_focus'
write_tab: False
required: True
MDRaisedButton:
id: boton_aliment_viaje
pos_hint: {"x":0, "top":0.5}
text:'Ingresar Gasto'
### CASETAS
<MyContentCasetas>:
adaptive_height: True
MDBoxLayout:
orientation:'horizontal'
adaptive_height:True
size_hint_x:self.width
pos_hint: {"center_x":0.5, "center_y":0.5}
spacing: dp(10)
padding_horizontal: dp(10)
MDLabel:
text: 'Monto:'
multiline: 'True'
halign: 'center'
pos_hint: {"x":0, "top":0.5}
size_hint_x: 0.15
font_style: 'Button'
font_size: 19
MDTextField:
id: monto_casetas_viaje
hint_text: 'Monto a solicitar'
pos_hint: {"x":0, "top":0.5}
halign: 'left'
size_hint_x: 0.3
helper_text: 'Ingresar el monto a solicitar'
helper_text_mode: 'on_focus'
write_tab: False
input_filter: 'float'
required: True
MDRaisedButton:
id: boton_casetas_viaje
pos_hint: {"x":0, "top":0.5}
text:'Ingresar Gasto'
BoxLayout:
size_hint_x: 0.05
### GASOLINA
<MyContentGasolina>:
adaptive_height: True
MDBoxLayout:
orientation:'horizontal'
adaptive_height:True
size_hint_x:self.width
pos_hint: {"center_x":0.5, "center_y":0.5}
spacing: dp(10)
padding_horizontal: dp(10)
MDLabel:
text: 'Monto:'
multiline: 'True'
halign: 'center'
pos_hint: {"x":0, "top":0.5}
size_hint_x: 0.15
font_style: 'Button'
font_size: 19
MDTextField:
id: monto_gas_viaje
hint_text: 'Monto a solicitar'
pos_hint: {"x":0, "top":0.5}
halign: 'left'
size_hint_x: 0.3
helper_text: 'Ingresar el monto a solicitar'
helper_text_mode: 'on_focus'
write_tab: False
input_filter: 'float'
required: True
MDRaisedButton:
id: boton_gas_viaje
pos_hint: {"x":0, "top":0.5}
text:'Ingresar Gasto'
BoxLayout:
size_hint_x: 0.05
Thanks a lot in advance.

Kivy: Multiple Screens and Menu Items not impletmenting

I am very new in kivy and working on a simple application using python. I want to create an application in kivy which comprises of constant menu items on left pane and their respective screens on right big pane or screen. But I am unable to find the solution in kivy examples and also in youtube video tutorials and also in google. Please check my simple code and refer to some solution.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.base import runTouchApp
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from kivy.properties import NumericProperty
#Code Starts here
Builder.load_string("""
#:import random random.random
#:import NoTransition kivy.uix.screenmanager.NoTransition
<MenuScreen>:
hue: random()
canvas:
Color:
hsv: self.hue, .5, .12
Rectangle:
size: self.size
Button:
text: 'Home'
size_hint: None, None
pos_hint: {'center_x':0.05, 'center_y':0.95}
size: 150, 50
on_release: root.manager.current = 'home'
on_release: root.manager.transition = NoTransition(duration=0)
Button:
text: 'Login History'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.88}
size: 150, 50
on_release: root.manager.current = 'loginhistory'
on_release: root.manager.transition = NoTransition(duration=0)
Button:
text: 'Alarm History'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.81}
size: 150, 50
on_release: root.manager.current = 'alarmhistory'
on_release: root.manager.transition = NoTransition(duration=0)
Button:
text: 'User Management'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.74}
size: 150, 50
on_release: root.manager.current = 'usermanagement'
on_release: root.manager.transition = NoTransition(duration=0)
Button:
text: 'Call SoS'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.67}
size: 150, 50
on_release: root.manager.current = 'callsos'
on_release: root.manager.transition = NoTransition(duration=0)
Button:
text: 'Settings'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.60}
size: 150, 50
on_release: root.manager.current = 'settings'
on_release: root.manager.transition = NoTransition(duration=0)
Button:
text: 'Call RealTec'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.53}
size: 150, 50
on_release: root.manager.current = 'callrealtec'
on_release: root.manager.transition = NoTransition(duration=0)
BoxLayout:
size_hint: .5, None
height: 250
pos_hint: {'center_x': .5}
orientation: 'vertical'
<HomeScreen>:
hue: random()
canvas:
Color:
hsv: self.hue, .5, .2
Rectangle:
size: self.size
Button:
background_color: [1, 2, 1, 2]
text: 'Home'
size_hint: None, None
pos_hint: {'center_x':0.05, 'center_y':0.95}
size: 150, 50
Button:
text: 'Login History'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.88}
size: 150, 50
on_release: root.manager.current = 'loginhistory'
Button:
text: 'Alarm History'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.81}
size: 150, 50
on_release: root.manager.current = 'alarmhistory'
Button:
text: 'User Management'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.74}
size: 150, 50
on_release: root.manager.current = 'usermanagement'
Button:
text: 'Call SoS'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.67}
size: 150, 50
on_release: root.manager.current = 'callsos'
Button:
text: 'Settings'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.60}
size: 150, 50
on_release: root.manager.current = 'settings'
Button:
text: 'Call RealTec'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.53}
size: 150, 50
on_release: root.manager.current = 'callrealtec'
Button:
text: 'Main Menu'
size_hint: None, None
pos_hint: {'center_x':0.05, 'center_y':0.46}
size: 150, 50
on_release: root.manager.current = 'menu'
BoxLayout:
size_hint: .5, None
height: 250
pos_hint: {'center_x': .5}
orientation: 'vertical'
<LoginHistoryScreen>:
hue: random()
canvas:
Color:
hsv: self.hue, .5, .9
Rectangle:
size: self.size
Button:
text: 'Home'
size_hint: None, None
pos_hint: {'center_x':0.05, 'center_y':0.95}
size: 150, 50
on_release: root.manager.current = 'home'
Button:
text: 'Login History'
background_color: [1, 2, 1, 2]
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.88}
size: 150, 50
Button:
text: 'Alarm History'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.81}
size: 150, 50
on_release: root.manager.current = 'alarmhistory'
Button:
text: 'User Management'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.74}
size: 150, 50
on_release: root.manager.current = 'usermanagement'
Button:
text: 'Call SoS'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.67}
size: 150, 50
on_release: root.manager.current = 'callsos'
Button:
text: 'Settings'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.60}
size: 150, 50
on_release: root.manager.current = 'settings'
Button:
text: 'Call RealTec'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.53}
size: 150, 50
on_release: root.manager.current = 'callrealtec'
Button:
text: 'Main Menu'
size_hint: None, None
pos_hint: {'center_x':0.05, 'center_y':0.46}
size: 150, 50
on_release: root.manager.current = 'menu'
BoxLayout:
size_hint: .5, None
height: 250
pos_hint: {'center_x': .5}
orientation: 'vertical'
<AlarmHistoryScreen>:
hue: random()
canvas:
Color:
hsv: self.hue, .5, .6
Rectangle:
size: self.size
Button:
text: 'Home'
size_hint: None, None
pos_hint: {'center_x':0.05, 'center_y':0.95}
size: 150, 50
on_release: root.manager.current = 'home'
Button:
text: 'Login History'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.88}
size: 150, 50
on_release: root.manager.current = 'loginhistory'
Button:
text: 'Alarm History'
background_color: [1, 2, 1, 2]
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.81}
size: 150, 50
Button:
text: 'User Management'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.74}
size: 150, 50
on_release: root.manager.current = 'usermanagement'
Button:
text: 'Call SoS'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.67}
size: 150, 50
on_release: root.manager.current = 'callsos'
Button:
text: 'Settings'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.60}
size: 150, 50
on_release: root.manager.current = 'settings'
Button:
text: 'Call RealTec'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.53}
size: 150, 50
on_release: root.manager.current = 'callrealtec'
Button:
text: 'Main Menu'
size_hint: None, None
pos_hint: {'center_x':0.05, 'center_y':0.46}
size: 150, 50
on_release: root.manager.current = 'menu'
BoxLayout:
size_hint: .5, None
height: 250
pos_hint: {'center_x': .5}
orientation: 'vertical'
<UserManagementScreen>:
hue: random()
canvas:
Color:
hsv: self.hue, .5, .8
Rectangle:
size: self.size
Button:
text: 'Home'
size_hint: None, None
pos_hint: {'center_x':0.05, 'center_y':0.95}
size: 150, 50
on_release: root.manager.current = 'home'
Button:
text: 'Login History'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.88}
size: 150, 50
on_release: root.manager.current = 'loginhistory'
Button:
text: 'Alarm History'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.81}
size: 150, 50
on_release: root.manager.current = 'alarmhistory'
Button:
text: 'User Management'
background_color: [1, 2, 1, 2]
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.74}
size: 150, 50
Button:
text: 'Call SoS'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.67}
size: 150, 50
on_release: root.manager.current = 'callsos'
Button:
text: 'Settings'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.60}
size: 150, 50
on_release: root.manager.current = 'settings'
Button:
text: 'Call RealTec'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.53}
size: 150, 50
on_release: root.manager.current = 'callrealtec'
Button:
text: 'Main Menu'
size_hint: None, None
pos_hint: {'center_x':0.05, 'center_y':0.46}
size: 150, 50
on_release: root.manager.current = 'menu'
BoxLayout:
size_hint: .5, None
height: 250
pos_hint: {'center_x': .5}
orientation: 'vertical'
<CallSoSScreen>:
hue: random()
canvas:
Color:
hsv: self.hue, .5, .7
Rectangle:
size: self.size
Button:
text: 'Home'
size_hint: None, None
pos_hint: {'center_x':0.05, 'center_y':0.95}
size: 150, 50
on_release: root.manager.current = 'home'
Button:
text: 'Login History'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.88}
size: 150, 50
on_release: root.manager.current = 'loginhistory'
Button:
text: 'Alarm History'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.81}
size: 150, 50
on_release: root.manager.current = 'alarmhistory'
Button:
text: 'User Management'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.74}
size: 150, 50
on_release: root.manager.current = 'usermanagement'
Button:
text: 'Call SoS'
background_color: [1, 2, 1, 2]
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.67}
size: 150, 50
Button:
text: 'Settings'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.60}
size: 150, 50
on_release: root.manager.current = 'settings'
Button:
text: 'Call RealTec'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.53}
size: 150, 50
on_release: root.manager.current = 'callrealtec'
Button:
text: 'Main Menu'
size_hint: None, None
pos_hint: {'center_x':0.05, 'center_y':0.46}
size: 150, 50
on_release: root.manager.current = 'menu'
BoxLayout:
size_hint: .5, None
height: 250
pos_hint: {'center_x': .5}
orientation: 'vertical'
<SettingsScreen>:
hue: random()
canvas:
Color:
hsv: self.hue, .5, .5
Rectangle:
size: self.size
Button:
text: 'Home'
size_hint: None, None
pos_hint: {'center_x':0.05, 'center_y':0.95}
size: 150, 50
on_release: root.manager.current = 'home'
Button:
text: 'Login History'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.88}
size: 150, 50
on_release: root.manager.current = 'loginhistory'
Button:
text: 'Alarm History'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.81}
size: 150, 50
on_release: root.manager.current = 'alarmhistory'
Button:
text: 'User Management'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.74}
size: 150, 50
on_release: root.manager.current = 'usermanagement'
Button:
text: 'Call SoS'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.67}
size: 150, 50
on_release: root.manager.current = 'callsos'
Button:
text: 'Settings'
background_color: [1, 2, 1, 2]
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.60}
size: 150, 50
Button:
text: 'Call RealTec'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.53}
size: 150, 50
on_release: root.manager.current = 'callrealtec'
Button:
text: 'Main Menu'
size_hint: None, None
pos_hint: {'center_x':0.05, 'center_y':0.46}
size: 150, 50
on_release: root.manager.current = 'menu'
BoxLayout:
size_hint: .5, None
height: 250
pos_hint: {'center_x': .5}
orientation: 'vertical'
<CallRealTecScreen>:
hue: random()
canvas:
Color:
hsv: self.hue, .5, .4
Rectangle:
size: self.size
Button:
text: 'Home'
size_hint: None, None
pos_hint: {'center_x':0.05, 'center_y':0.95}
size: 150, 50
on_release: root.manager.current = 'home'
Button:
text: 'Login History'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.88}
size: 150, 50
on_release: root.manager.current = 'loginhistory'
Button:
text: 'Alarm History'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.81}
size: 150, 50
on_release: root.manager.current = 'alarmhistory'
Button:
text: 'User Management'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.74}
size: 150, 50
on_release: root.manager.current = 'usermanagement'
Button:
text: 'Call SoS'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.67}
size: 150, 50
on_release: root.manager.current = 'callsos'
Button:
text: 'Settings'
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.60}
size: 150, 50
on_release: root.manager.current = 'settings'
Button:
text: 'Call RealTec'
background_color: [1, 2, 1, 2]
size_hint: None, None
pos_hint: {'center_x':.05, 'center_y':.53}
size: 150, 50
Button:
text: 'Main Menu'
size_hint: None, None
pos_hint: {'center_x':0.05, 'center_y':0.46}
size: 150, 50
on_release: root.manager.current = 'menu'
BoxLayout:
size_hint: .5, None
height: 250
pos_hint: {'center_x': .5}
orientation: 'vertical'
""")
# Declare both screens
class MenuScreen(Screen):
hue = NumericProperty(0)
pass
class HomeScreen(Screen):
hue = NumericProperty(0)
pass
class LoginHistoryScreen(Screen):
hue = NumericProperty(0)
pass
class AlarmHistoryScreen(Screen):
hue = NumericProperty(0)
pass
class UserManagementScreen(Screen):
hue = NumericProperty(0)
pass
class CallSosScreen(Screen):
hue = NumericProperty(0)
pass
class SettingsScreen(Screen):
hue = NumericProperty(0)
pass
class CallRealTecScreen(Screen):
hue = NumericProperty(0)
pass
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(HomeScreen(name='home'))
sm.add_widget(LoginHistoryScreen(name='loginhistory'))
sm.add_widget(AlarmHistoryScreen(name='alarmhistory'))
sm.add_widget(UserManagementScreen(name='usermanagement'))
sm.add_widget(CallSosScreen(name='callsos'))
sm.add_widget(CallRealTecScreen(name='callrealtec'))
sm.add_widget(SettingsScreen(name='settings'))
class TestApp(App):
def build(self):
return sm
if __name__ == '__main__':
TestApp().run()
I didn't read through your code. You should be providing a Minimal, Complete, and Verifiable example. That said here's a short example that you can build upon. The main take away from the example, is that you don't have to add ScreenManager as a top level widget.
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
class Manager(ScreenManager):
def __init__(self, *args, **kwargs):
super(Manager, self).__init__(*args, **kwargs)
for i in range(4):
txt = 'Screen {}'.format(i)
lbl = Label(text=txt)
screen = Screen(name=txt)
screen.add_widget(lbl)
self.add_widget(screen)
class Nav(GridLayout):
def __init__(self, sm=None, *args, **kwargs):
super(Nav, self).__init__(*args, **kwargs)
self.sm = sm
self.rows = 4
self.size_hint = (.2, 1)
for i in range(4):
self.add_widget(Button(text="Screen {}".format(i), on_release=self.change))
def change(self, btn):
self.sm.current = btn.text
class Root(BoxLayout):
def __init__(self, *args, **kwargs):
super(Root, self).__init__(*args, **kwargs)
self.orientation = "horizontal"
sm = Manager()
self.add_widget(Nav(sm=sm))
self.add_widget(sm)
class TestApp(App):
def build(App):
return Root()
if __name__ == '__main__':
TestApp().run()

Categories

Resources