Search code examples
pythonkivyspinnertogglebuttongetvalue

Spinner, ToggleButton store value and print when pressing Button


I'm pretty new to python and kivy and I'm trying to build my first app for my masterthesis. The problem I'm struggeling with right now is that I have multiple python and kivy files, and my app includes a group of Toggle buttons and Spinners. I want to 'store'/assign theselected values and print them when i hit my submit button. I'm working with multiple python and kivy files. The app also has some sliders and i managed to get those values when pressing submit, but can't figure it out for toggles nor spinners. I hope you can help!:)

Main .py, rather important:

import kivy
from kivy.app import App
from kivy.uix.button import Label
from kivy.uix.widget import Widget
from kivy.uix.textinput import TextInput
from kivy.lang import Builder
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import BooleanProperty
from kivy.properties import ObjectProperty

Builder.load_file('Header.kv')
Builder.load_file('Statusbar.kv')
Builder.load_file('Inputparameters.kv')
Builder.load_file('Outputparameters.kv')


#Layout
class Peenomat(AnchorLayout):
    def show_selected_value(self, spinner, text):
        print(spinner, text)
    #pass

#DropDown in Inputparameters
#class InputParameters(BoxLayout):
    #state = BooleanProperty(False)

class PeenomatApp(App):
    def build(self):
        return Peenomat()

if __name__=="__main__":
    PeenomatApp().run()

The main kv file:

    AnchorLayout:
        anchor_x: 'left'
        anchor_y: 'bottom'
        GridLayout:
            cols: 1

            canvas.before:
                Color:
                    rgba: 0.75 ,0.75, 0.75, 1
                Rectangle:
                    pos: self.pos
                    size: self.size

            Header:
                id: _header
                size_hint: 1, 0.1
                #height: 100

            InputParameters:
                id:_input_parameters
                size_hint: 1, 0.45


            StatusBar:
                id:_status_bar
                size_hint: 1, 0.1
                #height: 50

            OutputParameters:
                id:_output_parameters
                size_hint: 1, 0.35

The .kv file that includes the toogle buttons, spinners, and sliders where i guess most of the implemantation needs to be done

#: import statusbar StatusBar

<InputParameters@GridLayout>
    prozess: _proz_value
    prozess1: _prozess1
    prozess2: _prozess2
    prozess3: _prozess3

    vorbehandlung: _vorbehandlung
    material: _material
    haerte: _haerte
    rauheit: _rauheit

    cols: 2
    padding: 15
    spacing: 15

    #Farbe Schrift Label
    cb: (1,1, 1,1)

    #Farbe Slider
    cs: (0,0.353, 0.663,1)

    #FontSize
    fs: 20

    #Prozess
    Label:
        text:'Prozess:              '
        bold: True
        font_size: root.fs
        text_size: self.size
        halign: 'left'
        valign: 'center'
        color: root.cb

    BoxLayout:
        orientation: 'horizontal'
        id: _proz_value
        _proz_value: 0
        ToggleButton:
            text:'P-MOH'
            id: _prozess1
            group: "proc_group"
            background_color: 0.0, 0.353, 0.663, 1.0
            bold: True
            on_press: root._proz_value = 1

        ToggleButton:
            text:'E-MOH'
            id: _prozess2
            group: "proc_group"
            background_color: 0.0, 0.353, 0.663, 1.0
            bold: True
            on_press: root._proz_value = 2

        ToggleButton:
            text:'PE-MOH'
            id: _prozess3
            group: "proc_group"
            background_color: 0.0, 0.353, 0.663, 1.0
            bold: True
            on_press: root._proz_value = 3


    #Material

    Label:
        text: 'Material:            '
        bold: True
        font_size: root.fs
        text_size: self.size
        halign: 'left'
        valign: 'center'
        color: root.cb

    Spinner:
        id: _material
        # Callback
        on_text: 


        text: "Auswahl treffen"
        bold:True
        values: ['1.2379', 'Gusseisen', 'Kautschuk', 'Carbon', 'Adamantium']
        #background_normal: '[1,1,1,1]'
        background_color: root.cs
        color: root.cb

    # Herstellschritte
    Label:
        text:'Fertigungsschritte:   '
        bold: 'True'
        font_size: root.fs
        text_size: self.size
        halign: 'left'
        valign: 'center'
        color: root.cb

    Spinner:
        id: _vorbehandlung
        # Callback
        on_text:
            app.root.show_selected_value(self, self.text)

        text: "Auswahl treffen"
        bold:True
        values: ['Fräsen', 'Erodieren', 'Schleifen', 'Polieren']
        #background_normal: '[1,1,1,1]'
        background_color: root.cs
        color: root.cb


    # Haerte
    Label:
        text:'Haerte:               '
        bold: True
        font_size: root.fs
        text_size: self.size
        halign: 'left'
        valign: 'center'
        color: root.cb

    BoxLayout:
        orientation: 'vertical'
        spacing: 15

        Label:
            text: str(_haerte.value) + ' HRC'
            color: root.cs
            bold: True

        Slider:
            id: _haerte
            min: 45
            max: 65
            value:55
            step: 1
            value_track: True
            value_track_color: root.cs

    # Rauheit
    Label:
        text:'Rauheit:              '
        #color: cb
        bold: True
        font_size: root.fs
        text_size: self.size
        halign: 'left'
        valign: 'center'
        color: root.cb

    BoxLayout:
        orientation: 'vertical'
        spacing: 15

        Label:
            text: str("%.1f" % _rauheit.value) + ' Rz' #eine Nachkommastelle
            color: root.cs
            bold: True

        Slider:
            id: _rauheit
            min: 1
            max: 10
            value:5.5
            step: 0.1
            value_track: True
            value_track_color: root.cs

The .kv file that includes the submit button:

#: import statusbar StatusBar

<StatusBar@BoxLayout>

    orientation:'horizontal'
    padding: 5
    fs: 16
    Button:
        text: 'Clear'
        background_color: 0.0, 0.353, 0.663, 1.0
        size_hint: 0.4, 1
        bold: True
        font_size: 20
        on_press: root.btn_clear()

    Button:
        text: 'Submit'
        background_color: 0.0, 0.353, 0.663, 1.0
        size_hint: 0.4, 1
        bold: True
        font_size: 20
        on_press: root.btn_submit()

and last but not the least the file that includes the methods that are assigned to the buttons, which shuld also be important for my issue:

from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.app import App
from kivy.lang import Builder


class StatusBar(BoxLayout):
    group_mode = False
    #translation = ListProperty(None)
    prozess = ObjectProperty(None)
    vorbehandlung = ObjectProperty(None)
    material = ObjectProperty(None)
    haerte = ObjectProperty(None)
    rauheit = ObjectProperty(None)



    def btn_submit(self,text):
        ip = App.get_running_app().root.ids._input_parameters
        print("Haerte:", ip.haerte.value, "Rauheit:", ip.rauheit.value, "Material:", ip.material.text)

    def btn_clear(self):
        np = App.get_running_app().root.ids._input_parameters
        np.prozess1.state = "normal"
        np.prozess2.state = "normal"
        np.prozess3.state = "normal"
        np.material.text = "Auswahl treffen"
        np.haerte.value = 55
        np.rauheit.value = 5.5

The App is running like this and the clear button is exactly doing what its supposed to do, but I really can't figure out how I manage to print the selected spinner and toggle values when hitting submit...I hope you guys can help, been struggeling with this since a while now. Thanks in advance!


Solution

  • Alright i figured out an easy way to get the spinner value when hititng submit:

       def btn_submit(self):
    
           ip = App.get_running_app().root.ids._input_parameters
           print("Haerte:", ip.haerte.value, "Rauheit:", ip.rauheit.value, "Material:", ip.material.text, "Vorbehandlung:", ip.vorbehandlung.text)
    

    to get the value of the toggle buttons i did this:

     def on_state(self, togglebutton):
            tb = togglebutton
            if tb.state == 'down':
                self.verfahren = tb.text
                InputParameters.verfahren = tb.text
                print(self.verfahren)
                return InputParameters.verfahren
    

    and added in the .kv file for the toggle button:

            ToggleButton:
                id:_prozess2
                text:'E-MOH'
                on_state: root.on_state(self)
    

    I'm getting the value/state like that, but it's not triggered with the submit button, can't figure out how to do so