繁体   English   中英

从用户获取文本输入并使用 kivy 打印 python

[英]Getting text input from the user and print it python using kivy

我正在尝试打印用户给出的输入,但我得到的只是:< ObjectProperty name=input >

我不能在我的 py 中使用文本输入,因为如果我尝试运行安装了它的程序,python 会退出。 我曾尝试将弹出窗口放入“测试” class 但它只是出现了一个不同的错误。

任何帮助表示赞赏,谢谢。

这是我的代码:

在我的 py 中:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.popup import Popup

class MyPopup(Popup):
    pass

class Test(Widget):
pass

class TestApp(App):
    def build(self):
        return Test()

    def Process(self):
        text = MyPopup.input
        print(text)

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

在我的 kv 中:

#:import Factory kivy.factory.Factory

<MyPopup@Popup>
    input: text
    auto_dismiss: True
    size_hint: 0.4, 0.4
    TextInput:
        id: text
        hint_text: 'insert text'
        multiline: False
        on_text_validate: app.Process()

<Test>:
    canvas:
        Color:
            rgba: 0, 0, 0, 0
        Rectangle:
            pos: 0, 0
            size: self.width, self.height - self.height/6
        Color:
            rgba: 1, 1, 1, 0.1
        Rectangle:
            pos: 0, 0
            size: self.width/2 + self.width/10, self.height - self.height/6
        Color:
            rgba: 1, 1, 1, 1
        Rectangle:
            pos: 0, self.height - self.height/6
            size: self.width, self.height/6

    Button:
        id: GL
        text: 'In here'
        pos: 0 , root.height - root.height/6
        on_parent: GLdropdown.dismiss()
        on_release: GLdropdown.open(self)
        size_hint_y: None
        height: root.height/6
        width: root.width/7
    DropDown:
        id: GLdropdown
        on_select: btn.text = '{}'.format(args[1])

        Button:
            id: 'AV'
            text: 'press me'
            size_hint_y: None
            height: 50
            on_release: Factory.MyPopup().open()

如果你改变:

on_text_validate: app.Process()

至:

on_text_validate: app.Process(self.text)

并将Process()方法更改为:

def Process(self, text):
    print(text)

我认为它会按您的意愿工作。

问题是该行:

text = MyPopup.input

使text成为对 class MyPopupObjectProperty的引用。 您真正想要的是对TextInput文本的引用,它只是TextInput中的self.text

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM