我应该如何在kivy Python中设置全局变量?

时间:2017-01-19 18:45:14

标签: python kivy kivy-language

我的应用程序很简单,ScreenTwo显示一个“Parametreur”表单,其中包含各种选项。 我要做的是在这个表单的末尾设置一个“保存”按钮,该按钮将注册到名为“resultat”的列表中,所有选项到目前为止都在空输入中留下“0”。 全局变量'resultat'看起来像这样['blabla','15/06/201','31/12/1999','6']。

File.py

from kivy.app import App
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen, WipeTransition
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.properties import ObjectProperty
from kivy.lang import Builder

Builder.load_file("HadrianRunningApp.kv")

class ScreenMenu(Screen):
    pass

class ScreenOne(Screen):
    pass

class ScreenTwo(Screen):
    pass

class Parametreur(FloatLayout):
    def build(self):
        pass

    def submit_input(self):
        # Get the student name from the TextInputs
        label_name = self.label_text_input.text
        resultat.insert(0,label_name)

    def submit_input2(self):
        date_debut_name = self.date_debut_input.text
        resultat.insert(1,date_debut_name)

    def submit_input3(self):
        date_fin_name = self.date_fin_input.text
        resultat.insert(2,date_fin_name)

    def submit_input4(self):
        var_duree_estimation = self.duree_estimation_input.text
        resultat.insert(3,var_duree_estimation)

    def save_input(self):
        print(resultat)

class Manager(ScreenManager):
    screen_menu = ObjectProperty(None)
    screen_one = ObjectProperty(None)
    screen_two = ObjectProperty(None)

class ScreensApp(App):
    def build(self):
        m = Manager(transition=WipeTransition())
        return m

if __name__=='__main__':
    ScreensApp().run()

Kivy文件.kv:

#:kivy 1.9.0

<ScreenMenu>:
    BoxLayout:
        orientation:'vertical'
        size: root.size
        padding: "20dp"
        Button:
            text: "go to Screen 1"
            on_press: root.manager.current = 'screen1'
        Button:
            text: "go to Screen 2"
            on_press: root.manager.current = 'Parametreur'

<ScreenOne>:
    BoxLayout:
        spacing: 20
        padding: 20
        Button:
            text: "go to Screen 2"
            on_press: root.manager.current = 'Parametreur'
        Button:
            text: "go to Menu"
            on_press: root.manager.current = 'screen0'
<ScreenTwo>:
    Parametreur

<Parametreur>:
    label_text_input: label_text
    date_debut_input: date_debut
    date_fin_input: date_fin
    duree_estimation_input: duree_estimation
    resultat: resultat

    RelativeLayout:
        orientation: 'vertical'
        GridLayout:
            size_hint: (1., 0.11)
            pos_hint: {'right': 1, 'center_y': 0.91}
            padding: 6
            spacing: "4dp"
            cols:2
            rows:1
            Button:
                text: "go to Screen 1"
                on_press: root.manager.current = 'screen1'
            Button:
                text: "go to Menu"
                on_press: root.manager.current = 'screen0'
        GridLayout:
            size_hint: (1., 0.8)
            pos_hint: {'right': 1, 'center_y': 0.45}
            padding: "7dp"
            spacing: 5
            cols:2
            rows:7
            Button:
                text: "Liste Actif"
            Label:
                text: " "
            BoxLayout:
                Label:
                    text: "Date du début"
                BoxLayout:
                    orientation: "vertical"
                    Button:
                        text: "Submit"
                        on_press: root.submit_input2()
                    TextInput:
                        id: date_debut
                        text: 'dd/mm/YYYY'          
            BoxLayout:
                Label:
                    text: "Date de fin"
                BoxLayout:
                    orientation: "vertical"
                    Button:
                        text: "Submit"
                        on_press: root.submit_input3()
                    TextInput:
                        id: date_fin
                        text: 'dd/mm/YYYY'
            Label:
                text: "Pourcentage de séparation \n de la base (validation/test)"
            Button:
                text: "Open to Close"
            Button:
                text: "Close to Close"
            Button:
                text: "les 3 (6 en tout) frontières des VSs"
            BoxLayout:
                Label:
                    text: "Durée pour la réestimation \n des modèles (en jours)"
                BoxLayout:
                    orientation: "vertical"
                    Button:
                        text: "Submit"
                        on_press: root.submit_input4()
                    TextInput:
                        id: duree_estimation
                        text: 'dd/mm/YYYY'
            Label:
                text: "variable selection NMF/Entropy"
            Label:
                text: "Kernel/damiers/buntcher/\n neurone/XGBoost/Gradient boosting"

            # We create the widgets
            BoxLayout:
                spacing: "0dp"
                Label:
                    id: label_text
                    text: "0"
                    font_size: "30dp"
                BoxLayout:
                    orientation: "vertical"
                    spacing: "3dp"
                    Button:
                        text: "Add"
                        on_release: label_text.text = str(int(label_text.text) + 1)
                    Button:
                        text: "Subtract"
                        on_release: label_text.text = str(int(label_text.text) - 1)
                Button:
                    text: "Submit"
                    size_hint_x: None
                    on_press: root.submit_input()
            BoxLayout:
                padding: "2dp"
                spacing: "2dp"
                size_hint: (1., 0.50)
                pos_hint: {'right': 1, 'center_y': 0.09}
                Button:
                    id: resultat
                    text:"Save"
                    size_hint_x: None
                    on_press: root.save_input()

<Manager>:
    id: screen_manager

    screen_menu: screen_menu
    screen_one: screen_one
    screen_two: screen_two


    ScreenMenu:
        id: screen_menu
        name: 'screen0'
        manager: screen_manager 

    ScreenOne:
        id: screen_one
        name: 'screen1'
        manager: screen_manager 

    ScreenTwo:
        id: screen_two
        name: 'Parametreur'
        manager: screen_manager

我应该在哪里以及如何设置我的全局变量'resultat'(这是一个列表)?

我已经解决了我的问题,但我仍然不明白它是如何工作的。设置变量是Kivy不需要在Python代码中设置它。但是Kivy和Python如何知道它的类型?现在,它作为一个列表工作,没有我定义它(我打算将它作为一个列表)。

我怎样才能将变量设置为元组?

0 个答案:

没有答案