text输入值未更新

2024-06-23 19:30:54 发布

您现在位置:Python中文网/ 问答频道 /正文

为什么textinput值没有用这个代码更新?我需要捕捉用户在单击按钮时键入的值,但不管看起来如何,我只能获得默认文本。在

这是python 3.4.3上的一个可运行示例:

# -*- coding: utf-8 -*-
import kivy
kivy.require("1.9.1")

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout 
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput 
from functools import partial 

class TestApp(App):
    def check(self, texto, *args):
        print(texto)

    def build(self):
        layout = FloatLayout()
        txtinput = TextInput(background_color=(1,1,1,1),
                                   font_size=16,
                                   size_hint=(None, None),
                                   size=(200, 35),
                                   pos_hint={"center_x":0.5, "bottom":0.8},
                                   multiline=False,
                                   text="write here...",
                                   use_bubble=True)
        boton = Button(size_hint=(None, None),
                              size=(150, 50),
                              pos_hint={"left":1, "bottom":1},
                              text="Check",
                              on_release=partial(self.check, txtinput.text))
        layout.add_widget(txtinput)
        layout.add_widget(boton)
        return layout

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

我想要一个纯Python的答案,我不太喜欢使用kv文件。在


Tags: textfromimportselfnoneappsizebutton
1条回答
网友
1楼 · 发布于 2024-06-23 19:30:54

在创建txtinput之后,用with TextInput.bind()check方法绑定到您的txtinput

txtinput = TextInput(background_color=(1,1,1,1), ...
txtinput.bind(text=self.check)

输入值在相关代码段中可用。在

目前check方法是实例方法,我想它总是接受App的实例作为第一个属性,txtinput作为第二个属性,input text作为第三个属性。在

要访问check中的文本,方法签名应更新为该方法:

^{pr2}$

相关问题 更多 >

    热门问题