【问题标题】:passing text from textbox to function that returns array将文本从文本框传递到返回数组的函数
【发布时间】:2019-02-04 09:19:11
【问题描述】:

我必须使用 kivy 在 python 中编写一个程序,该程序在文本框中获取文本并将其传递给执行网络抓取和很多事情的函数,然后返回字符串数组,数组中的最后一个元素是对数组所以我真的很困惑,花了很多时间。所以我必须写两个 kv 文件还是只写一个文件?这是我的简单 kivy 代码作为开始。

试过了还是不行

#textInput.py
from app import *
Builder.load_file('textInput.kv')

require('1.10.0')


class MainScreen(BoxLayout):
    def __init__(self):
        self.super(MainScreen, self).__init__()
    def btn_click(self):
        name =self.BoxLayout.BoxLayout.TextInput
       #the function that takes the output of the text field 
        get_company_name(name)
        #
        #
        #
        # here I will call the function that returns the array so how to     pass the answer 
        # and also pass to where ? shall I use the same kv file or create     another one 
class Test(App):
    def build(self):
        self.title = 'CompanyInfoApp'
        return MainScreen()

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

lbl:我的标签 方向:'垂直'

# Third section title
Label:
    size_hint: (1, .1)
    text: 'Welcome To compnay info App'
    font_size: 25

# Third section Box
BoxLayout:
    Button:
           text:"let's start"
           on_press:root.btn_click()

    size_hint: (1, .2)
    padding: [180, 180, 180, 180]
    BoxLayout:
        Label:

            pos_hint:{'x': .3, 'y': .6}
            text: 'Enter the Company Name:'
            text_size: self.width-20, self.height-20
        TextInput:
            height: self.minimum_height
            pos_hint:{'x': .3, 'y': .6}
            multiline: False
            text: ''

【问题讨论】:

    标签: python textbox kivy


    【解决方案1】:

    尝试为您的TextInput 小部件提供id。然后就可以使用TextInput 小部件的id 访问文本数据了。

    这是一个基本的例子:

    from kivy.app import App
    from kivy.lang import Builder
    from kivy.uix.boxlayout import *
    
    main_kv = """
    <Main>:
        orientation: 'vertical'
        TextInput:
            # TextInput's id
            id: txtinput
            text: ''
        Button:
            text: "print text"
            on_press: root.btn_click()
    """
    
    class Main(BoxLayout):
        def btn_click(self):
            name = self.ids['txtinput'].text
            print(name)
    
    class Test(App):
        def build(self):
            Builder.load_string(main_kv)
            return Main()
    
    Test().run()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-12
      • 2017-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-02
      相关资源
      最近更新 更多