简体   繁体   English

append 值动态微调器

[英]append values to spinner dynamically

I'm trying to write some basic GUI using Kivy .我正在尝试使用Kivy编写一些基本的 GUI。

My program loads some data from a CSV file that contains an unknown number of rows.我的程序从包含未知行数的 CSV 文件加载一些数据。

The first column is called sValue which basically tells me the "id" of the spinner and the second column has a Name value.第一列称为 sValue,它基本上告诉我微调器的“id”,第二列有一个 Name 值。

My goal is to iterate all of the rows inside this CSV and to create dynamically x spinners based on the different numbers of "id" that the CSV has and in each spinner to show the values that it might have.我的目标是迭代此 CSV 中的所有行,并根据 CSV 具有的不同数量的“id”动态创建 x 微调器,并在每个微调器中显示它可能具有的值。

For example, if the CSV looks as follows:例如,如果 CSV 如下所示:

sValue    Name
1           a
1           b
2           a
3           a
3           b
3           c

I want the code to create 3 spinners where in spinner 1 it will have the values a,b.我希望代码创建 3 个微调器,其中在微调器 1 中它将具有值 a、b。 spinner 2 will have the value a and spinner 3 will have a,b,c.微调器 2 的值为 a,微调器 3 的值为 a,b,c。

I wrote the following code however it only shows me the last value that was added (I guess because it always makes a new spinner instead of appending):我写了下面的代码,但它只显示了最后添加的值(我猜是因为它总是创建一个新的微调器而不是附加):

from kivy.uix.label import Label
from kivy.uix.spinner import Spinner
from kivy.uix.floatlayout import FloatLayout
from kivy.app import App

import pandas as pd

def loadData():
    data = pd.read_csv('data.csv')
    SValues = range(min(data['sValue']),max(data['sValue']))
    return data, SValues

class MainApp(App):
    def build(self):
        Data, SValues = loadData()
        layout = self.initializeScreen(Data,SValues)
        return layout

    def initializeScreen(self, Data, SValues):
        self.spinnerObject = {}
        self.imageObject = {}
        complete_layout = FloatLayout()

        s_layout = FloatLayout()
        for ind, row in Data.iterrows():
            self.labelObject = Label(text='S %d' % row['sValue'], bold=True)
            self.labelObject.size_hint  = (1/len(SValues), 0.05)
            self.labelObject.pos_hint={'x': (row['sValue']-1)/len(SValues), 'y':0.8}
            s_layout.add_widget(self.labelObject)

            self.spinnerObject[row['sValue']] = Spinner(text='sValue %d' % row['sValue'], values=row['Name'])
            self.spinnerObject[row['sValue']].size_hint  = (1/len(SValues), 0.1)
            self.spinnerObject[row['sValue']].pos_hint={'x': (row['sValue']-1)/len(SValues), 'y':0.7}
            s_layout.add_widget(self.spinnerObject[row['sValue']])
    
            
        complete_layout.add_widget(s_layout)

        
        return complete_layout

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

What I get looks like this:我得到的是这样的:

在此处输入图像描述

You can modify your loop to add the values to existing Spinner instances:您可以修改循环以将值添加到现有的Spinner实例:

    for ind, row in Data.iterrows():
        try:
            spinner = self.spinnerObject[row['sValue']]
        except:
            spinner = Spinner(text='sValue %d' % row['sValue'], values=[])
            self.spinnerObject[row['sValue']] = spinner
            self.spinnerObject[row['sValue']].size_hint = (1 / len(SValues), 0.1)
            self.spinnerObject[row['sValue']].pos_hint = {'x': (row['sValue'] - 1) / len(SValues), 'y': 0.7}
            s_layout.add_widget(self.spinnerObject[row['sValue']])
        spinner.values.append(row['Name'])

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

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