如何将python程序集成到kivy应用程序中

时间:2016-09-12 22:10:13

标签: android python kivy kivy-language

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

main.py

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

main.kv

<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

otherprogram.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          

1 个答案:

答案 0 :(得分: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文件中,你可以通过调用它们的root widgets属性来访问这些值。

    Label:
        text: root.label1_text

等等。