如何将Textinput文本转换为矩阵

时间:2017-09-27 22:45:48

标签: python-2.7 kivy

我是kivy / python的新手。我想测试我的编程技巧,所以我修改了这个代码。它将文本作为textinput中的数字,并将它们转换为矩阵并执行操作。它适用于正数,但我也想捕获负数。但这总是会出错。

$("#PhoneField").keydown(function(e) {
var v=$(this).val();
var f=this;
setTimeout(function () {
    if(f.value.indexOf('09') !== 0) {
        $(f).val(v);
    } 
}, 10);
});

这是我不断得到的错误:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="PhoneField" value="09" class="phoneBox" maxlength="10"/>

1 个答案:

答案 0 :(得分:0)

乌拉!我见过魔术。秘密是string.split() 这是修改后的代码。适用于积极和积极的工作 消极的条目。

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from sympy import *

class Screen(GridLayout):
    def __init__(self, **kwargs):
        super(Screen, self).__init__(**kwargs)
        self.input = TextInput(multiline=False, size_hint = (None, None))
        self.add_widget(self.input)
        self.input.bind(on_text_validate=self.print_input)

    def print_input(self, value):
        """function to convert Textinput text into a matrix. works
        for only square matrices"""

        m = 3; n = 3  # rows and columns
        list = [float(el) for el in value.text.split(',')]  # entries must be separated with ','
        mat = Matrix(list)
        mat1 = mat.reshape(3, 3)  # can be made general
        pprint(mat1)

class MyApp(App):

    def build(self):
        return Screen()

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