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

标签 android python kivy kivy-language

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

主要.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()

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

其他程序.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          

最佳答案

据我了解,您希望代码最后一部分的 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

等等。

关于android - 如何将 python 程序集成到 kivy 应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39459530/

相关文章:

java - 如何在后台继续运行蓝牙 Activity ?

android - 将字符串写入文件

Android 应用程序旋转时意外停止

python - 变量交换是否保证在python中是原子的?

Kivy 启动器和依赖项/第三方库

python - 为什么运行 py 和 kv 脚本时出现空白屏幕?

python - 将图表作为带有按钮的小部件放置在 Kivy 上

android - SSL 握手中止 - 确切原因是什么?

python - 有没有好的网页摘要器?

python - 如何将 .wav 文件转换为 python3 中的频谱图