Spinner,ToggleButton 存储值并在按下 Button 时打印

Spinner, ToggleButton store value and print when pressing Button

我是 python 和 kivy 的新手,我正在尝试为我的硕士论文构建我的第一个应用程序。我现在遇到的问题是我有多个 python 和 kivy 文件,我的应用程序包括一组切换按钮和微调器。我想 'store'/分配选定的值并在我点击提交按钮时打印它们。我正在处理多个 python 和 kivy 文件。该应用程序也有一些滑块,我在按下提交时设法获得了这些值,但无法弄清楚切换或微调器。希望你能帮上忙!:)

主要的.py,比较重要:

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()

主kv文件:

    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

包含工具按钮、微调器和滑块的 .kv 文件,我想大部分实现都需要在这些地方完成

#: 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

包含提交按钮的 .kv 文件:

#: 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()

最后但并非最不重要的是包含分配给按钮的方法的文件,这对我的问题也很重要:

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

该应用程序 运行 是这样的,清除按钮完全按照预期的方式工作,但我真的不知道我是如何在点击提交时设法打印选定的微调器和切换值的......我希望你们能帮忙,从现在开始就一直在努力解决这个问题。提前致谢!

好吧,我想出了一种在点击提交时获取微调器值的简单方法:

   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)

为了获取切换按钮的值,我这样做了:

 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

并在切换按钮的 .kv 文件中添加:

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

我得到了这样的 value/state,但它不是通过提交按钮触发的,不知道该怎么做