特金特表:添加数据并指定索引

2024-04-26 12:43:53 发布

您现在位置:Python中文网/ 问答频道 /正文

有人用过tkinter table吗? 我在向新行添加数据时遇到问题。我有一个用户输入对象的输入字段。然后在def onButtonSents()中,我检查这个单词是否包含在objects数组中,并将该对象输入到表命名对象的第一列,在第二列中添加对象的积极情绪,依此类推。我想通过点击按钮添加新行,把不同列的一行值放入字典。我现在所拥有的:

file sentiment_analysis.py:
    objects [] #an array of objects which user enters into entry field
    positiveSentiments = dict()  #dictionary with key - object and positive words which correspond to it
    negativeSentiments = dict()  #dictionary with key - object and negative words which correspond to it


    array_cols = ['Object', 'Positive sentiments', 'Negative sentiments'] 
    var = tktable.ArrayVar()

#below is initialization of my tkinter table

    import tktable
    array_cols = ['Object', 'Positive sentiments', 'Negative sentiments']
    var = tktable.ArrayVar()
    table = tktable.Table(frame,
        rows = 1,
        cols = 3,
        roworigin=0,
        colorigin=0,
        variable=var,)
    table.pack(side='bottom', fill='both')
    var.set(index='0,0', value = 'Object')
    table.tag_configure('active', )
    var.set(index='0,1', value = 'Positive Sentiments')
    var.set(index='0,2', value = 'Negative Sentiments')

#The method which puts data into the rows of table
    def onButtonSents():
        table.insert_rows(1)
        for i in range (1, 5):
            index1 = '%i,%i'  % (i, 0)
            table.activate(index1)
            for key in sentiment_analysis.positiveSentiments.keys():
                if key == entry.get():
                    var.set(index='active', value=entry.get())
            for i in range (1, 5):
                index2 = '%i,%i'  % (i, 1)
                table.activate(index2)
            for key in sentiment_analysis.positiveSentiments.keys():
                if key == entry.get():
                    var.set(index='active',   value=sentiment_analysis.positiveSentiments[key])
            for i in range (1, 5):
                index3 = '%i,%i'  % (i, 2)
                table.activate(index3)
            for key in sentiment_analysis.negativeSentiments.keys():
                if key == entry.get():
                    var.set(index='active', value=sentiment_analysis.negativeSentiments[key])

但是表格的单元格没有正确填充。第一个对象如果填充正确,但是所有对象都会变得相同,就像第一个输入“ubs”,当我输入第二个“小麦”时,第一个对象也会变成“小麦”,他们的情绪也会改变。在

不幸的是,我在网上没有找到任何关于这个问题的建议。我将非常感谢你的任何建议!在


Tags: 对象keyinwhichforindexvaluevar