【问题标题】:display the array as a table in Kivy Python在 Kivy Python 中将数组显示为表格
【发布时间】:2020-06-15 15:17:17
【问题描述】:

我是 Kivy 的新手,遇到这个问题:我有两个数组

X=[[1,2,3],    and            Sum-Rows= [[6],
   [4,5,6],                              [15], 
   [7,8,9]]                              [24]]

如何在 Kivy Python 中将这些数组的元素显示为表格?

             first Value      Second Value      Third Value     Sum
first row        1                 2                 3           6
second row       4                 5                 6           15
third row        7                 8                 9           24

我认为 Recyclerview 可能是解决方案? 谢谢

【问题讨论】:

  • 看看 GridLayout。你可以用你的元素的标签来填充它。
  • 感谢您的反馈,完美,如果是动态数组,我该怎么做?
  • 很简单,GridLayout有cols和rows这样的参数。您可以使用元素获取列表的长度,例如更改列数或同时更改行数,然后将其填充到for 循环中。
  • 我在下面写了我的代码,你能看吗?

标签: python arrays layout android-recyclerview kivy


【解决方案1】:

这是我的代码: main.py

from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.properties import ObjectProperty, StringProperty
from kivy.uix.textinput import TextInput
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock
from kivy.uix.gridlayout import GridLayout
import numpy as np




Window.clearcolor = (0.5, 0.5, 0.5, 1)

Builder.load_file('MyMain.kv')



class MainWindow(GridLayout):

    def __init__(self, **kwargs):
        super(MainWindow, self).__init__(**kwargs)
        Window.bind(on_key_down=self._on_keyboard_down)
        self.counter = 1
        Clock.schedule_once(self.add_stuff)

    def add_stuff(self, *args):
        self.textlist = [TextInput()]
        self.ids.grid.add_widget(Label(text='% Gas {} '.format(self.counter)))
        self.counter += 1
        self.ids.grid.add_widget(self.textlist[0])




    def addnewtextinput(self):
        self.ids.grid.add_widget(Label(text='% Gas  ' + str(self.counter)))
        self.counter += 1
        self.textlist.append(TextInput())
        self.ids.grid.add_widget(self.textlist[-1])


    def _on_keyboard_down(self, instance, keyboard, keycode, text, modifiers):
        if keyboard == 13:  # 32 - Cpace key presssed Ascii

            self.getresult()


        if keyboard == 9 : # 13 - Enter key presssed Ascii
            self.addnewtextinput()



        b = [i.text.strip().split(" ") for i in self.textlist]

        X = [[int(y) for y in x] for x in b]

        for i in range(len(X)-1):

)
            if (len(X[i]) < len(X[i+1])):
                for _ in range(len(X[i+1]) - len(X[i])):
                    X[i].append(0)

            if (len(X[i+1]) < len(X[i])):
                for _ in range(len(X[i]) - len(X[i+1])):
                    X[i+1].append(0)
            print(X[i])

        print(X) # this is the Dynamic Array






class TextInput(TextInput):
    def __init__(self, **kwargs):
        super(TextInput, self).__init__(**kwargs)

        def set_focus(dt):
            self.focus = True

        Clock.schedule_once(set_focus, .1)


class MyMainApp(App):
    def build(self):
        return MainWindow()


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

MyMain.kv

<CustButton@Button>:
    font_size: 40


    <MainWindow>:
        spacing : 10
        name: "erst"
        grid: grid.__self__
        rows:2
        cols:1
        GridLayout:
            orientation: "horizon"
            padding: 2
            id:grid
            cols:2








    <TextInputX>
        focus: True

列表 X 是动态列表 如何使用灵活的列和行进行动态 GridLayout?

【讨论】:

    【解决方案2】:

    MainWindow 本身就是 GridLayout ,为什么要在里面多加一个 GridLayout 呢? 要动态更改它,您可以执行以下操作:

    # if you are in MainWindow, which is GridLayout
    self.clear_widgets()
    # number of cols is length or your rows + 1 for sum
    self.cols = len(X[0]) + 1
    for i in range(len(X)):
        for j in X[i]:
            self.add_widget(Label(text=str(j))
        self.add_widget(Label(text=str(Sum-Rows[i][0])))
    

    就是这样。

    【讨论】:

    • 谢谢,如果我输入 3.Textinput,我有这个错误:引发 GridLayoutException(kivy.uix.gridlayout.GridLayoutException:GridLayout 中的孩子太多。增加行/列!我怎样才能增加行/cols! ?
    • self.cols = 5 例如,或 self.rows = 10
    • 完美非常感谢,现在我试着做一个如上表的表格,“定义行”:第一个值|||第二个值|||第三个值||| ..... X值|||总和。 “定义 col”:第一行|||第二行|||第三行 |||....Y 行。但我不能为第一列和第一行“定义行”和“定义列”写一个静态名称我该怎么做?。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 2013-11-14
    • 2017-12-01
    • 2021-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多