从 Kivy 文件中读取 Python 2.7 中的用户输入数据

Reading user-input data in Python 2.7 from Kivy file

希望这对你们中的一些人来说是一个简单的问题。所以我正在编写一些代码来连接加热器控制器。 GUI 主要是用 Kivy 语言编写的(以划分各种屏幕),但我有一个 .py 文件将所有 Kivy 文件联系在一起。在我的一个 Kivy 文件中,有一些 TextInput 字段供用户定义一些数值。文本字段填满后,用户单击 "Start" 按钮开始加热器测试。 现在,我的问题是当使用 "Start" 按钮作为我的标志时,我怎样才能让 Python 读取字段中的数值?我在网上看过关于使用 .getraw_input 方法的帖子,但没有 Python 检索并解释来自 Kivy 代码的值。任何帮助将不胜感激!

这是我的 .py 文件的一部分,我需要在其中评估用户输入值...

class ControllerScreen(Screen):

def build(self):
    return ControllerScreen()

def CreateExcelFile(self):
        wb = Workbook()
        ws = wb.active
        ws.cell('A1').value = 'Timestamp'
        ws.cell('B1').value = 'Temperature'
        ws.cell('C1').value = 'Batch 1'
        ws.cell(row=2, column=1).value = 000
        wb.save("Spreadsheet1.xlsx")

这是 Kivy 文件的摘录,包含用户在其中键入数字的单个文本字段的代码...

<ControllerScreen>:
    TextInput:                                    #Ramp time increment field
        id: ramp_time
        font_size: 25
        size_hint: 0.1,0.06
        pos_hint: {"right":0.575, 'y':0.67}

    Button:                             #Start analysis button
        on_release: app.root.current = "home"
        text: "Start"
        font_size: 25
        color: 0,1,0.5,1
        size_hint: 0.29,0.075
        pos_hint: {'x':0.35, 'y':0.1}

最后,这是一张 GUI 的图片,可以为我所关注的文本字段提供一些参考(红色圆圈是我在上面的代码中引用的那个)...

要访问 TextInput 的值,您需要使用 TextInput 的 属性 text,因此要获得一个数字,您需要:

n = int(self.ids.ramp_time.text) #or float or whatever you need

但是,如果您不会访问 ControllerScreen 中的 text,那么您需要管理 类 之间的通信,以便您可以在它们之间使用变量。您可以通过以下答案实现它:例如 ,

您可以选择为输入设置 filtering,这样您的用户就不会搞砸(例如输入 letter/s)

I've looked online and posts about using the .get and raw_input methods, but nothing that has Python retrieving and interpreting values from Kivy code.

我认为问题在于您希望直接从 python 而不是从 Kivy 获取用户输入。我会说 Kivy 很新,所以在旧的 tutorials/websites 中不会提及。官方网站或youtube上有直接针对kivy的教程和文档。