mardi 26 janvier 2021

Python, KivyMD - Getting data from Checkboxes, Text input, there must be a better way

enter image description hereNew to programming (about 6 months) Trying to create a heavy equipment inspection form for my company. Everything is working but I am suspicious that there may be an easier way. I have a form with multiple check box groups, all my data is going into a dictionary with keys to the values. As you can see from my code, I have a lot of repetition already and once I finish the If statements for the check boxes my code will be very large and repetitive for extracting the data. Looking for some tips on this. In the KV file scroll down to the "tires" check boxes to see the id references for the if statments.

Thanks

PYTHON CODE

from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from kivy.uix.scrollview import ScrollView
from kivy.core.window import Window
from kivy.properties import StringProperty
from kivy.app import runTouchApp




class ContentNavigationDrawer(BoxLayout):
    screen_manager = ObjectProperty()
    nav_drawer = ObjectProperty()


class MainApp(MDApp):
    text = StringProperty('-.text')

    def build(self):
        self.theme_cls.primary_palette = 'Amber'
        self.theme_cls.theme_style = 'Dark'
        self.theme_cls.primary_hue = 'A700'
        return Builder.load_string(KV)

    def my_callback(self):
        mileage = self.root.ids.mileage.text
        hours = self.root.ids.hours.text
        comments = self.root.ids.comments.text
        if self.root.ids.a1.active is True:
            a1 = 1
        else:
            a1 = 0
        if self.root.ids.b1.active is True:
            b1 = 1
        else:
            b1 = 0
        data_out = dict()
        data_out['ac'] = comments
        data_out['aa'] = mileage
        data_out['ab'] = hours
        data_out['ad'] = a1
        data_out['ae'] = b1
        print(data_out)


MainApp().run()

Kivy Code

<Check_tires@MDCheckbox>:
    group: 'tires'
    size_hint: None, None
    size: "48dp", "48dp"   
<Check_breaks@MDCheckbox>:
    group: 'breaks'
    size_hint: None, None
    size: "48dp", "48dp"   
<Check_steering@MDCheckbox>:
    group: 'steering'
    size_hint: None, None
    size: "48dp", "48dp"   
<Check_undercarriage@MDCheckbox>:
    group: 'undercarriage'
    size_hint: None, None
    size: "48dp", "48dp"    
<Check_suspension@MDCheckbox>:
    group: 'suspension'
    size_hint: None, None
    size: "48dp", "48dp"
<Check_engine@MDCheckbox>:
    group: 'engine'
    size_hint: None, None
    size: "48dp", "48dp"
<Check_drive@MDCheckbox>:
    group: 'drive'
    size_hint: None, None
    size: "48dp", "48dp"
<Check_fuel@MDCheckbox>:
    group: 'fuel'
    size_hint: None, None
    size: "48dp", "48dp"
<Check_cooling@MDCheckbox>:
    group: 'cooling'
    size_hint: None, None
    size: "48dp", "48dp"
<Check_electrical@MDCheckbox>:
    group: 'electrical'
    size_hint: None, None
    size: "48dp", "48dp"
        
<ContentNavigationDrawer>:

    ScrollView:

        MDList:

            OneLineIconListItem:
                text: 'Inspection'
                on_press:
                    root.nav_drawer.set_state("close")
                    root.screen_manager.current = 'inspection'
                IconLeftWidget:
                    icon: 'file'

            OneLineIconListItem:
                text: 'Load Count'
                on_press:
                    root.nav_drawer.set_state('close')
                    root.screen_manager.current = 'loads'
                IconLeftWidget:
                    icon: 'counter'

            OneLineIconListItem:
                text: 'Map'
                on_press:
                    root.nav_drawer.set_state('close')
                    root.screen_manager.current = 'map'
                IconLeftWidget:
                    icon: 'map'

            OneLineIconListItem:
                text: "Settings"
                on_press:
                    root.nav_drawer.set_state("close")
                    root.screen_manager.current = "settings"
                IconLeftWidget:
                    icon: 'settings'
                    
            
Screen:

    MDToolbar:
        id: toolbar
        pos_hint: {"top": 1}
        elevation: 10
        title: "Menu"
        left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]

    NavigationLayout:
        x: toolbar.height

        ScreenManager:
            id: screen_manager

            Screen:
                name: "inspection"
                
                MDTextFieldRect:
                    id: hours
                    multiline: False
                    size_hint: 0.15, None
                    height: "30dp"
                    pos_hint: {'center_x': .92, 'center_y': .85}
                    on_text: app.my_callback()
                MDLabel:
                    text: "Hours"
                    pos_hint: {'center_x': 1.25, 'center_y': .85}
                    
                MDTextFieldRect:
                    id: mileage
                    multiline: False
                    size_hint: 0.15, None
                    height: "30dp"
                    pos_hint: {'center_x': .92, 'center_y': .8}
                    on_text: app.my_callback()
                MDLabel:
                    text: "Mileage"
                    pos_hint: {'center_x': 1.25, 'center_y': .8}
                    
                MDCheckbox:
                    size_hint: None, None
                    size: "48dp", "48dp"
                    pos_hint: {'center_x': 0.92, 'center_y': .75}
                MDLabel:
                    text: "Spill Kit"
                    pos_hint: {'center_x': 1.25, 'center_y': .75}
                    
                MDCheckbox:
                    size_hint: None, None
                    size: "48dp", "48dp"
                    pos_hint: {'center_x': 0.92, 'center_y': .7}
                MDLabel:
                    text: "360 Walk"
                    pos_hint: {'center_x': 1.25, 'center_y': .7}
                    
                MDCheckbox:
                    size_hint: None, None
                    size: "48dp", "48dp"
                    pos_hint: {'center_x': 0.92, 'center_y': .65}
                MDLabel:
                    text: "Radio Check"
                    pos_hint: {'center_x': 1.25, 'center_y': .65}
                    
                MDTextFieldRect:
                    id: comments
                    size_hint: 0.35, 0.41
                    height: "30dp"
                    pos_hint: {'center_x': .819, 'center_y': .37}
                    on_text: app.my_callback()
                MDLabel:
                    text: "Comments"
                    pos_hint: {'center_x': 1.145, 'center_y': .6}
                
                MDLabel:
                    text: "Inspection Item"
                    pos_hint: {'center_x': .6, 'center_y': .85}
                MDLabel:
                    text: "Good"
                    pos_hint: {'center_x': .775, 'center_y': .85}
                MDLabel:
                    text: "Repair"
                    pos_hint: {'center_x': .87, 'center_y': .85}
                MDLabel:
                    text: "Inoperable"
                    pos_hint: {'center_x': 0.955, 'center_y': .85}
                MDLabel:
                    text: "N/A"
                    pos_hint: {'center_x': 1.083, 'center_y': .85}
                                   
                Check_tires:
                    id: a1
                    active: True
                    pos_hint: {'center_x': .6, 'center_y': .8}
                Check_tires:
                    id: b1
                    pos_hint: {'center_x': .5, 'center_y': .8}
                Check_tires:
                    id: c1
                    pos_hint: {'center_x': .4, 'center_y': .8}
                Check_tires:
                    id:d1
                    pos_hint: {'center_x': .3, 'center_y': .8}
                MDLabel:
                    text: "Tires"
                    pos_hint: {'center_x': .6, 'center_y': .8}
                    
                Check_breaks:
                    active: True
                    pos_hint: {'center_x': .6, 'center_y': .75}
                Check_breaks:
                    pos_hint: {'center_x': .5, 'center_y': .75}
                Check_breaks:
                    pos_hint: {'center_x': .4, 'center_y': .75}
                Check_breaks:
                    pos_hint: {'center_x': .3, 'center_y': .75}
                MDLabel:
                    text: "Brakes"
                    pos_hint: {'center_x': .6, 'center_y': .75}
                    
                Check_steering:
                    active: True
                    pos_hint: {'center_x': .6, 'center_y': .7}
                Check_steering:
                    pos_hint: {'center_x': .5, 'center_y': .7}
                Check_steering:
                    pos_hint: {'center_x': .4, 'center_y': .7}
                Check_steering:
                    pos_hint: {'center_x': .3, 'center_y': .7}
                MDLabel:
                    text: "Steering"
                    pos_hint: {'center_x': .6, 'center_y': .7}
                    
                Check_undercarriage:
                    active: True
                    pos_hint: {'center_x': .6, 'center_y': .65}
                Check_undercarriage:
                    pos_hint: {'center_x': .5, 'center_y': .65}
                Check_undercarriage:
                    pos_hint: {'center_x': .4, 'center_y': .65}
                Check_undercarriage:
                    pos_hint: {'center_x': .3, 'center_y': .65}
                MDLabel:
                    text: "Undercarriage"
                    pos_hint: {'center_x': .6, 'center_y': .65}
                    
                Check_suspension:
                    active: True
                    pos_hint: {'center_x': .6, 'center_y': .6}
                Check_suspension:
                    pos_hint: {'center_x': .5, 'center_y': .6}
                Check_suspension:
                    pos_hint: {'center_x': .4, 'center_y': .6}
                Check_suspension:
                    pos_hint: {'center_x': .3, 'center_y': .6}
                MDLabel:
                    text: "Suspension"
                    pos_hint: {'center_x': .6, 'center_y': .6}
                
                Check_engine:
                    active: True
                    pos_hint: {'center_x': .6, 'center_y': .55}
                Check_engine:
                    pos_hint: {'center_x': .5, 'center_y': .55}
                Check_engine:
                    pos_hint: {'center_x': .4, 'center_y': .55}
                Check_engine:
                    pos_hint: {'center_x': .3, 'center_y': .55}
                MDLabel:
                    text: "Engine"
                    pos_hint: {'center_x': .6, 'center_y': .55}
                    
                Check_drive:
                    active: True
                    pos_hint: {'center_x': .6, 'center_y': .5}
                Check_drive:
                    pos_hint: {'center_x': .5, 'center_y': .5}
                Check_drive:
                    pos_hint: {'center_x': .4, 'center_y': .5}
                Check_drive:
                    pos_hint: {'center_x': .3, 'center_y': .5}
                MDLabel:
                    text: "Drive Train"
                    pos_hint: {'center_x': .6, 'center_y': .5}
                    
                Check_fuel:
                    active: True
                    pos_hint: {'center_x': .6, 'center_y': .45}
                Check_fuel:
                    pos_hint: {'center_x': .5, 'center_y': .45}
                Check_fuel:
                    pos_hint: {'center_x': .4, 'center_y': .45}
                Check_fuel:
                    pos_hint: {'center_x': .3, 'center_y': .45}
                MDLabel:
                    text: "Fuel System"
                    pos_hint: {'center_x': .6, 'center_y': .45}
                    
                Check_cooling:
                    active: True
                    pos_hint: {'center_x': .6, 'center_y': .4}
                Check_cooling:
                    pos_hint: {'center_x': .5, 'center_y': .4}
                Check_cooling:
                    pos_hint: {'center_x': .4, 'center_y': .4}
                Check_cooling:
                    pos_hint: {'center_x': .3, 'center_y': .4}
                MDLabel:
                    text: "Cooling System"
                    pos_hint: {'center_x': .6, 'center_y': .4}
                    
                Check_electrical:
                    active: True
                    pos_hint: {'center_x': .6, 'center_y': .35}
                Check_electrical:
                    pos_hint: {'center_x': .5, 'center_y': .35}
                Check_electrical:
                    pos_hint: {'center_x': .4, 'center_y': .35}
                Check_electrical:
                    pos_hint: {'center_x': .3, 'center_y': .35}
                MDLabel:
                    text: "Electrical System"
                    pos_hint: {'center_x': .6, 'center_y': .35}
                    
                MDCheckbox:
                    size_hint: None, None
                    size: "48dp", "48dp"
                    pos_hint: {'center_x': .6, 'center_y': .3}
                MDCheckbox:
                    size_hint: None, None
                    size: "48dp", "48dp"
                    pos_hint: {'center_x': .5, 'center_y': .3}
                MDCheckbox:
                    size_hint: None, None
                    size: "48dp", "48dp"
                    pos_hint: {'center_x': .4, 'center_y': .3}
                MDCheckbox:
                    size_hint: None, None
                    size: "48dp", "48dp"
                    pos_hint: {'center_x': .3, 'center_y': .3}
                MDLabel:
                    text: "Exhaust System"
                    pos_hint: {'center_x': .6, 'center_y': .3}
                    
                MDCheckbox:
                    size_hint: None, None
                    size: "48dp", "48dp"
                    pos_hint: {'center_x': .6, 'center_y': .25}
                MDCheckbox:
                    size_hint: None, None
                    size: "48dp", "48dp"
                    pos_hint: {'center_x': .5, 'center_y': .25}
                MDCheckbox:
                    size_hint: None, None
                    size: "48dp", "48dp"
                    pos_hint: {'center_x': .4, 'center_y': .25}
                MDCheckbox:
                    size_hint: None, None
                    size: "48dp", "48dp"
                    pos_hint: {'center_x': .3, 'center_y': .25}
                MDLabel:
                    text: "Hydraulic System"
                    pos_hint: {'center_x': .6, 'center_y': .25}
                    
                MDCheckbox:
                    size_hint: None, None
                    size: "48dp", "48dp"
                    pos_hint: {'center_x': .6, 'center_y': .2}
                MDCheckbox:
                    size_hint: None, None
                    size: "48dp", "48dp"
                    pos_hint: {'center_x': .5, 'center_y': .2}
                MDCheckbox:
                    size_hint: None, None
                    size: "48dp", "48dp"
                    pos_hint: {'center_x': .4, 'center_y': .2}
                MDCheckbox:
                    size_hint: None, None
                    size: "48dp", "48dp"
                    pos_hint: {'center_x': .3, 'center_y': .2}
                MDLabel:
                    text: "Transmission"
                    pos_hint: {'center_x': .6, 'center_y': .2}
                    
                MDCheckbox:
                    size_hint: None, None
                    size: "48dp", "48dp"
                    pos_hint: {'center_x': .6, 'center_y': .15}
                MDCheckbox:
                    size_hint: None, None
                    size: "48dp", "48dp"
                    pos_hint: {'center_x': .5, 'center_y': .15}
                MDCheckbox:
                    size_hint: None, None
                    size: "48dp", "48dp"
                    pos_hint: {'center_x': .4, 'center_y': .15}
                MDCheckbox:
                    size_hint: None, None
                    size: "48dp", "48dp"
                    pos_hint: {'center_x': .3, 'center_y': .15}
                MDLabel:
                    text: "Clutch"
                    pos_hint: {'center_x': .6, 'center_y': .15}
                    
                MDCheckbox:
                    size_hint: None, None
                    size: "48dp", "48dp"
                    pos_hint: {'center_x': .6, 'center_y': .1}
                MDCheckbox:
                    size_hint: None, None
                    size: "48dp", "48dp"
                    pos_hint: {'center_x': .5, 'center_y': .1}
                MDCheckbox:
                    size_hint: None, None
                    size: "48dp", "48dp"
                    pos_hint: {'center_x': .4, 'center_y': .1}
                MDCheckbox:
                    size_hint: None, None
                    size: "48dp", "48dp"
                    pos_hint: {'center_x': .3, 'center_y': .1}
                MDLabel:
                    text: "Operator Controls"
                    pos_hint: {'center_x': .6, 'center_y': .1}
                    
                MDCheckbox:
                    size_hint: None, None
                    size: "48dp", "48dp"
                    pos_hint: {'center_x': .6, 'center_y': .05}
                MDCheckbox:
                    size_hint: None, None
                    size: "48dp", "48dp"
                    pos_hint: {'center_x': .5, 'center_y': .05}
                MDCheckbox:
                    size_hint: None, None
                    size: "48dp", "48dp"
                    pos_hint: {'center_x': .4, 'center_y': .05}
                MDCheckbox:
                    size_hint: None, None
                    size: "48dp", "48dp"
                    pos_hint: {'center_x': .3, 'center_y': .05}
                MDLabel:
                    text: "Lights"
                    pos_hint: {'center_x': .6, 'center_y': .05}
                              
            Screen:
                name: "loads"
                
                MDFloatingActionButton:
                    icon: "minus"
                    md_bg_color: app.theme_cls.primary_color
                    pos_hint: {'center_x': .1, 'center_y': .35}
                MDLabel:
                    text: "Till"
                    md_bg_color: app.theme_cls.primary_color  
                    pos_hint: {'center_x': .59, 'center_y': .8}
                MDLabel:
                    text: "0"
                    md_bg_color: app.theme_cls.primary_color  
                    pos_hint: {'center_x': .595, 'center_y': .5}               
                MDFloatingActionButton:
                    icon: "plus"
                    md_bg_color: app.theme_cls.primary_color
                    pos_hint: {'center_x': .1, 'center_y': .65}
                    
                MDFloatingActionButton:
                    icon: "minus"
                    md_bg_color: app.theme_cls.primary_color
                    pos_hint: {'center_x': .3, 'center_y': .35}
                MDLabel:
                    text: "CCR"
                    md_bg_color: app.theme_cls.primary_color  
                    pos_hint: {'center_x': .78, 'center_y': .8}
                MDLabel:
                    text: "0"
                    md_bg_color: app.theme_cls.primary_color  
                    pos_hint: {'center_x': .795, 'center_y': .5}               
                MDFloatingActionButton:
                    icon: "plus"
                    md_bg_color: app.theme_cls.primary_color
                    pos_hint: {'center_x': .3, 'center_y': .65}
                    
                MDFloatingActionButton:
                    icon: "minus"
                    md_bg_color: app.theme_cls.primary_color
                    pos_hint: {'center_x': .5, 'center_y': .35}
                MDLabel:
                    text: "Sub Base"
                    md_bg_color: app.theme_cls.primary_color  
                    pos_hint: {'center_x': 0.96, 'center_y': .8}
                MDLabel:
                    text: "0"
                    md_bg_color: app.theme_cls.primary_color  
                    pos_hint: {'center_x': .995, 'center_y': .5}               
                MDFloatingActionButton:
                    icon: "plus"
                    md_bg_color: app.theme_cls.primary_color
                    pos_hint: {'center_x': .5, 'center_y': .65}
                    
                MDFloatingActionButton:
                    icon: "minus"
                    md_bg_color: app.theme_cls.primary_color
                    pos_hint: {'center_x': .7, 'center_y': .35}
                MDLabel:
                    text: "Coarse Base"
                    md_bg_color: app.theme_cls.primary_color  
                    pos_hint: {'center_x': 1.15, 'center_y': .8}
                MDLabel:
                    text: "0"
                    md_bg_color: app.theme_cls.primary_color  
                    pos_hint: {'center_x': 1.195, 'center_y': .5}               
                MDFloatingActionButton:
                    icon: "plus"
                    md_bg_color: app.theme_cls.primary_color
                    pos_hint: {'center_x': .7, 'center_y': .65}
                    
                MDFloatingActionButton:
                    icon: "minus"
                    md_bg_color: app.theme_cls.primary_color
                    pos_hint: {'center_x': .9, 'center_y': .35}
                MDLabel:
                    text: "Waste"
                    md_bg_color: app.theme_cls.primary_color  
                    pos_hint: {'center_x': 1.37, 'center_y': .8}
                MDLabel:
                    text: "0"
                    md_bg_color: app.theme_cls.primary_color  
                    pos_hint: {'center_x': 1.395, 'center_y': .5}               
                MDFloatingActionButton:
                    icon: "plus"
                    md_bg_color: app.theme_cls.primary_color
                    pos_hint: {'center_x': .9, 'center_y': .65}

            Screen:
                name: "map"
                MDLabel:
                    text: "Map"
                    halign: "center"

            Screen:
                name: "settings"
                MDLabel:
                    text: "Settings"
                    halign: "center"

    MDNavigationDrawer:
        id: nav_drawer

        ContentNavigationDrawer:
            screen_manager: screen_manager
            nav_drawer: nav_drawer
'''



Aucun commentaire:

Enregistrer un commentaire