1

我正在使用带有 kivy 模块的 python 编写的应用程序来开发跨平台应用程序。在这个应用程序中,我有一个带有一些数值的表格。我希望将这些数值传递给我编写的另一个 python 程序,用于计算其他一些值,然后传递回应用程序并返回给用户。外部程序目前没有认识到我试图传递给它的值存在。下面是我正在使用的 3 个文件的示例代码,2 个用于应用程序,1 个用于外部程序。对于导入的大量看似未使用的 kivy 模块,我深表歉意,我在完整的应用程序中都使用了它们。

主文件

import kivy
import flowcalc

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.dropdown import DropDown
from kivy.uix.spinner import Spinner
from kivy.uix.button import Button
from kivy.base import runTouchApp
from kivy.uix.textinput import TextInput
from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty, ListProperty
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
from kivy.core.window import Window
from kivy.uix.slider import Slider
from kivy.uix.scatter import Scatter
from kivy.uix.image import AsyncImage
from kivy.uix.carousel import Carousel

Builder.load_file('main.kv')

#Declare Screens

class FormScreen(Screen):
    pass

class ResultsScreen(Screen):
    pass

#Create the screen manager

sm = ScreenManager()

sm.add_widget(FormScreen(name = 'form'))
sm.add_widget(ResultsScreen(name = 'results'))

class TestApp(App):
    def build(self):
        return sm

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

主文件

<FormScreen>:
    BoxLayout:
        orientation: 'vertical'
        AsyncImage:
            source: 'sample.png'
            size_hint: 1, None
            height: 50
        GridLayout:
            cols: 2
            Label:
                text: 'Company Industry'
            Label:
                text: 'Sample'
            Label:
                text: 'Company Name'
            TextInput:
                id: companyname
            Label:
                text: 'Company Location'
            TextInput:
                id: companylocation  
            Label:
                text: 'Data1'
            TextInput:
                id: data1
            Label:
                text: 'Data2'
            TextInput:
                id: data2
            Label:
                text: 'Data3'
            TextInput:
                id: data3
        Button: 
            text: 'Submit'
            size_hint: 1, .1
            on_press: root.manager.current = 'results'

<ResultsScreen>:
    BoxLayout:
        orientation: 'vertical'
        AsyncImage:
            source: 'sample.png'
            size_hint: 1, None
            height: 50
        Label:
            text: 'Results'
            size_hint: 1, .1
        GridLayout:
            cols: 2
            Label: 
                text: 'Results 1'
            Label:
                text: results1
            Label: 
                text: 'Results 2'
            Label:
                text: results2
            Label: 
                text: 'Results 3'
            Label:
                text: results3
            Label: 
                text: 'Results 4'
            Label:
                text: results4

其他程序.py

data1float = float(data1.text)                                                                       
data2float = float(data2.text)                               
data3float = float(data3.text)

results1 = data1float + data2float
results2 = data1float - data3float
results3 = data2float * data3float
results4 = 10 * data2float          
4

1 回答 1

2

据我了解,您希望代码最后一部分中的 GridLayout 中的标签从您的 python 代码中获取它们的文本。你可以这样做:

from otherprogram import results1, results2, results3, results4

class ResultsScreen(Screen):

    label1_text = results1
    label2_text = results2
    label3_text = results3
    label4_text = results4

然后在您的 .kv 文件中,您可以通过调用它们的根小部件属性来访问这些值。

    Label:
        text: root.label1_text

等等。

于 2016-09-13T03:51:31.193 回答