Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
777 views
in Technique[技术] by (71.8m points)

checkbox - 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):


    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
        if self.root.ids.c1.active is True:
            c1 = 1
        else:
            c1 = 0
        if self.root.ids.d1.active is True:
            d1 = 1
        else:
            d1 = 0
        data_out = dict()
        data_out['aa'] = a1
        data_out['ab'] = b1
        data_out['ac'] = c1
        data_out['ad'] = d1
        data_out['ed'] = mileage
        data_out['ef'] = hours
        data_out['el'] = comments
        print(data_out)


MainApp().run()

Kivy Code

'''
<Check_tires@MDCheckbox>:
    group: 'tires'
    size_hint: None, None
    size: "48dp", "48dp" 
    on_release: app.my_callback()  
<Check_breaks@MDCheckbox>:
    group: 'breaks'
    size_hint: None, None
    size: "48dp", "48dp"   
    on_release: app.my_callback()
<Check_steering@MDCheckbox>:
    group: 'steering'
    size_hint: None, None
    size: "48dp", "48dp"   
    on_release: app.my_callback()
<Check_undercarriage@MDCheckbox>:
    group: 'undercarriage'
    size_hint: None, None
    size: "48dp", "48dp"    
    on_release: app.my_callback()
<Check_suspension@MDCheckbox>:
    group: 'suspension'
    size_hint: None, None
    size: "48dp", "48dp"
    on_release: app.my_callback()
<Check_engine@MDCheckbox>:
    group: 'engine'
    size_hint: None, None
    size: "48dp", "48dp"
    on_release: app.my_callback()
<Check_drive@MDCheckbox>:
    group: 'drive'
    size_hint: None, None
    size: "48dp", "48dp"
    on_release: app.my_callback()
<Check_fuel@MDCheckbox>:
    group: 'fuel'
    size_hint: None, None
    size: "48dp", "48dp"
    on_release: app.my_callback()
<Check_cooling@MDCheckbox>:
    group: 'cooling'
    size_hint: None, None
    size: "48dp", "48dp"
    on_release: app.my_callback()
<Check_electrical@MDCheckbox>:
    group: 'electrical'
    size_hint: None, None
    size: "48dp", "48dp"
    on_release: app.my_callback()
        
<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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

KV:

MDCheckbox:
    on_active: app.callback(self, self.active)

Python:

def callback(self, instance, value)
    self.data_out[instance] = value
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...