KvLang - TextInput:在新屏幕上显示在 .py 文件中计算的列表(与计算列表的内容不同 class)

KvLang - TextInput: display a list calculated in the .py file on a new screen (a different class to what the list was calculated under)

我有一个 .py 文件和一个 .kv 文件。我正在创建一个 kivy 应用程序,它在屏幕上采用 selected 微调器值(Var 1、Var2、Var 3、Var 4),使用算法将它们与某些限制进行比较并创建我想要的答案列表在新屏幕上显示。 到目前为止,我可以成功 select 微调器值,将它们输入我的算法并在 python shell 中生成一个列表 'final_list'。我遇到的问题是如何在 kivy 应用程序的新屏幕上显示它们。

在我的.py文件中 class MainScreen(Screen): 我的计算列表称为 'final_list',我希望 'final_list' 替换我在 class 下的 .kv 文件中的文本:文本:(“第一个示例答案\n 第二个示例答案").

例如,对于 selected 微调器值 Var 1=WNW,Var 2=1.2,Var 3=E,Var 4=medium...我应该得到一个值列表 [a7,a8, a10,a19,a20] 在新屏幕上。

非常感谢任何帮助,谢谢, 蒙迪

这是我的 M_A_I_N.py 文件:

import kivy
import numpy as np

kivy.require("1.9.1")                       # My version of Kivy

                                            # Kivy Modules
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.core.window import Window
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.widget import Widget
from kivy.uix.button import Button

                                            #Dictionary
dict={'N':1, 'NNE':2, 'NE':3, 'ENE':4,
      'E':5, 'ESE':6, 'SE':7, 'SSE':8,
      'S':9, 'SSW':10, 'SW':11, 'WSW':12,
      'W':13, 'WNW':14, 'NW':15, 'NNW':16,
      'Low':1, 'Medium':2, 'High':3}


class Manager(ScreenManager):
    pass


class MainScreen(Screen):

    def __init__(self, **kwargs):
        super(MainScreen, self).__init__(**kwargs)

    def print_info(self):
        value= self.weather_selection.ids.spinner_id_1.text
        live_weather = [dict[self.weather_selection.ids.spinner_id_1.text],
                float(self.weather_selection.ids.spinner_id_2.text),
                dict[self.weather_selection.ids.spinner_id_3.text],
                dict[self.weather_selection.ids.spinner_id_4.text]]

        print(live_weather)

     #Spot Lists
        a1 = [13, 15, 5, 10, 10, 13, 2, 3]
        a2 = [12, 14, 5, 10, 10, 12, 2, 3]
        a3 = [12, 14, 4, 10, 10, 12, 2, 3]
        a4 = [12, 14, 5, 10, 10, 12, 2, 3]
        a5 = [10, 12, 4, 10, 10, 12, 1, 1]
        a6 = [10, 12, 4, 10, 10, 12, 1, 1]
        a7 = [1, 16, 0.5, 2, 2, 6, 1, 3]
        a8 = [1, 16, 0.5, 2, 2, 6, 1, 3]
        a9 = [10, 12, 1, 3, 2, 6, 1, 2]
        a10 = [12, 14, 1, 2.5, 4, 6, 1, 3]
        a11 = [1, 16, 1.5, 4, 2, 8, 2, 2]
        a12 = [10, 12, 2.5, 10, 4, 6, 3, 3]
        a13 = [10, 12, 1, 3, 4, 6, 1, 3]
        a14 = [1, 16, 2.5, 10, 4, 6, 1, 2]
        a15 = [12, 14, 3, 10, 2, 8, 1, 2]
        a16 = [12, 14, 3, 10, 6, 8, 1, 1]
        a17 = [1, 16, 3, 10, 6, 12, 3, 3]
        a18 = [12, 14, 2, 10, 4, 6, 1, 1]
        a19 = [12, 14, 1, 10, 4, 6, 1, 3]
        a20 = [12, 14, 1, 2.5, 4, 6, 1, 3]
        a21 = [12, 14, 1.5, 10, 4, 6, 2, 3]
        a22 = [12, 14, 2, 10, 4, 6, 2, 3]

        allSpots = np.stack((a1, a2, a3, a4, a5, a6, a7, a8, a9,
                             a10, a11, a12, a13, a14, a15, a16,
                             a17, a18
                             , a19, a20, a21, a22))


        numberOfspots = (allSpots.shape[0])

        g = [0] * numberOfspots
        # print(range(numberOfspots))

        for i in range(22):
            if live_weather[0] >= allSpots[i][0] and live_weather[0] <= allSpots[i][1] \
                    and live_weather[1] >= allSpots[i][2] and live_weather[1] <= allSpots[i][3] \
                    and live_weather[2] >= allSpots[i][4] and live_weather[2] <= allSpots[i][5] \
                    and live_weather[3] >= allSpots[i][6] and live_weather[3] <= allSpots[i][7]:
                g[i] = 1
            else:
                g[i] = 0


        SB = ["a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8",
              "a9","a10", "a11", "a12", "a13", "a14", "a15", "a16","a17", "a18"
            , "a19", "a20", "a21", "a22"]

        for i in range(len(g)):
            if g[i] == 1:

                final_list = SB[i]
                print(final_list)


class AnotherScreen(Screen):
    pass

class WeatherSelection(BoxLayout):

    # For Spinner
    def spinner_clicked(self, value):

        print("spinner 1 {}:"
              "spinner 2 {}:"
              "spinner 3 {}:"
              "spinner 4 {}".format(self.ids.spinner_id_1.text,
                                    self.ids.spinner_id_2.text,
                                    self.ids.spinner_id_3.text,
                                    self.ids.spinner_id_4.text))


presentation=Builder.load_file("M_A_I_N.kv")

class MyApp(App):

    def build(self):
        return presentation

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

这是我的 M_A_I_N.kv 文件

#:kivy 1.9.1

#: import FadeTransition kivy.uix.screenmanager.FadeTransition
#: import CheckBox kivy.uix.checkbox

Manager:
    transition: FadeTransition()
    MainScreen:
    AnotherScreen:

<MainScreen>:
    name: "main"
    weather_selection:weather_selection
    FloatLayout: # allows us to have two widgets on one screen
        WeatherSelection
            id:weather_selection
        Button:
            color: 0,1,0,1
            font_size: 15
            size_hint: 0.15,0.15
            text: "Calculate"
            on_release: root.print_info();app.root.current= "other"
            pos_hint: {"right":1, "bottom":1}


<WeatherSelection>:
    orientation: "vertical"
    padding: 100
    spacing: 100

    GridLayout:
        size: root.size
        rows: 2
        cols:4

        TextInput:
            text: "Var 1"
        TextInput:
            text: "Var 2"
        TextInput:
            text: "Var 3"
        TextInput:
            text: "Var 4"

        BoxLayout:
            orientation: "horizontal"
            size_hint_x: .25

            Spinner:
                text: "Select"
                values: ["S","SSW","SW","WSW","W","WNW","NW","NNW"]
                id: spinner_id_1
                on_text: root.spinner_clicked(spinner_id_1.text)

        BoxLayout:
            orientation: "horizontal"
            size_hint_x: .25

            Spinner:
                text: "Select"
                values: ["0.8", "0.9", "1.0", "1.1", "1.2","1.3", "1.4", "1.5", "1.6", "1.7", "1.8", "1.9", "2.0", "2.1", "2.2", "2.3", "2.4"]
                id: spinner_id_2
                on_text: root.spinner_clicked(spinner_id_2.text)

        BoxLayout:
            orientation: "horizontal"
            size_hint_x: .25

            Spinner:
                text: "Select"
                values: ["N","NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"]
                id: spinner_id_3
                on_text: root.spinner_clicked(spinner_id_3.text)


        BoxLayout:
            orientation: "horizontal"
            size_hint_x: .25

            Spinner:
                text: "Select"
                values: ["Low","Medium","High"]
                id: spinner_id_4
                on_text: root.spinner_clicked(spinner_id_4.text)


<AnotherScreen>:
    name: "other"

    FloatLayout: # allows us to have two widgets on one screen

        Button:
            color: 0,1,0,1
            font_size: 15
            size_hint: 0.15,0.15
            text: "Back Home"

            on_release: app.root.current= "main"
            pos_hint: {"right":1, "top":1}

        BoxLayout:
            orientation: "horizontal"
            size_hint_x: .25
            TextInput:
                text: ("1st example answer \n 2nd example answer")
  1. 如果 2 个屏幕在同一个文件中,请使用 global 变量以便它们都可以访问。

  2. 如果 2 个屏幕在不同的文件中,那么,对于 python,它们只是 2 个模块,所以有 2 个方法:

    • Python方式:使用模块共享2个屏幕之间的值
    • 基维方式:
      • 在您的 MyApp
      • 处声明一个 class 变量
      • 然后可以通过app.get_running_app().your_class_variable_name
      • 得到值
      • 别忘了from kivy.app import app

现在我添加一个关于如何跨模块共享值的示例。 我使用python方式,Kivy方式肯定更简单,因为你不需要一个新文件,但我喜欢分离文件来保存所有共享变量,更清晰。

这里是持有变量需要分享的文件:

# GlobalShared.py
MY_NUMBER = 0

这里是存放2个屏幕的主文件,因为现在我们使用GlobalShared.py来存放变量,所以2个屏幕是在同一个文件中还是在2个不同的文件中都没有关系,在这里,我将它们全部放在 main.py 中只是为了快速破解。如果需要,您可以将它们分开。

每个屏幕有3个按钮

  • 读取:读取MY_NUMBER的值
  • 更改:将 MY_NUMBER 添加 1
  • 转到:转到下一个屏幕

您可以看到它如何读取共享变量并更改它,而其他屏幕仍然可以访问和更改变量。

# main.py
import GlobalShared
from kivy.app import App
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen, ScreenManager

Builder.load_string("""
<ScreenOne>:
    BoxLayout:
        orientation:'vertical'
        Label:
            text: 'I am ScreenOne'
        Label:
            id: lbl1
        Button:
            text: 'Read'
            on_press: root.press_read()
        Button:
            text: 'Change'
            on_press: root.press_change()
        Button:
            text: 'Go to ScreenTwo'
            on_press: app.sm.current = "screen_2"
<ScreenTwo>:
    BoxLayout:
        orientation:'vertical'
        Label:
            text: 'I am ScreenTwo'
        Label:
            id: lbl2
        Button:
            text: 'Read'
            on_press: root.press_read()
        Button:
            text: 'Change'
            on_press: root.press_change()
        Button:
            text: 'Go to ScreenOne'
            on_press: app.sm.current = "screen_1"
""")


class ScreenOne(Screen):
    def press_read(self):
        self.ids.lbl1.text = "SharedVar is " + str(GlobalShared.MY_NUMBER)

    def press_change(self):
        GlobalShared.MY_NUMBER = GlobalShared.MY_NUMBER + 1
        self.ids.lbl1.text = "SharedVar is now " + str(GlobalShared.MY_NUMBER)


class ScreenTwo(Screen):
    def press_read(self):
        self.ids.lbl2.text = "SharedVar is now " + str(GlobalShared.MY_NUMBER)

    def press_change(self):
        GlobalShared.MY_NUMBER = GlobalShared.MY_NUMBER + 1
        self.ids.lbl2.text = "SharedVar is " + str(GlobalShared.MY_NUMBER)


class ScreenApp(App):
    sm = ScreenManager()

    def build(self):
        ScreenApp.sm.add_widget(ScreenOne(name='screen_1'))
        ScreenApp.sm.add_widget(ScreenTwo(name='screen_2'))
        return ScreenApp.sm


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