M Lorden
M Lorden

Reputation: 31

How to integrate a python program into a kivy app

I'm working on an app written in python with the kivy modules to develop a cross-platform app. Within this app I have a form which takes some numerical values. I would like these numerical values to be passed to another python program I've written, used to calculate some other values, and passed back to the app and returned to the user. The outside program is currently not recognizing that the values I'm trying to pass to it exist. Below is sample code from the 3 files I'm using, 2 for the app and 1 for the outside program. I apologize about the abundance of seemingly unused kivy modules being imported, I use them all in the full app.

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          

Upvotes: 1

Views: 1068

Answers (1)

Amin Etesamian
Amin Etesamian

Reputation: 3699

As far as I understood you want the labels in your GridLayout in the last section of your code to get their texts from your python code. You could do something like this:

from otherprogram import results1, results2, results3, results4

class ResultsScreen(Screen):

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

then in your .kv file you could access these values by calling their root widgets attribute.

    Label:
        text: root.label1_text

and so on.

Upvotes: 3

Related Questions