将Python TkinterTreeview中的行提取到Pandas数据框架中

2024-09-30 06:32:57 发布

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

我有一个GUI,其中数据帧的结果根据之前输入的过滤器填充到python中的treeview中。然后用户可以单击treeview并将值更新为所需的数字。视图中的行数可以从1到20+不等。一旦用户根据需要更新了视图,我在“检查分配”下面有一个按钮。你知道吗

此时,我想将treeview“导出”到一个数据帧中,以便针对另一个表运行。我似乎不能简单地将其导出为数据帧。有什么办法吗?我只需要第一列和最后一列(共8列)来检查新更新的文件。你知道吗

这是我到目前为止的情况。你知道吗

   def PrintAllocation(self):
        treeview = tkk.Treeview(root)
        treeview.grid(column = 1, row = 8, columnspan = 4, padx = 1, pady = 1)
        cols = list(matches.columns)
        treeview["columns"] = cols

        for i in cols:
            treeview.column(i, width = 100, anchor = "w")
            treeview.heading(i,text=i,anchor='w',)
        for index, row in matches.iterrows():
            treeview.insert("",0,text=index,values=list(row))

        def set_cell_value(event): # Double click to enter the edit state
            for item in treeview.selection():
                #item = I001
                item_text = treeview.item(item, "values")
                         #print(item_text[0:2]) # Output the value of the selected row
                column= treeview.identify_column(event.x)# column
                print(column)
                row = treeview.identify_row(event.y) #row
            cn = int(str(column).replace('#',''))
            rn = int(str(row).replace('I',''))
            if column == '#8':
                entryedit = Text(root,width=50,height = 1)
                entryedit.grid(column = 2, row = 9, padx = 1, pady = 1)
            else:
                entryedit = Text(root,width=10,height = 1)
                entryedit.grid(column = 2, row = 9, padx = 1, pady = 1)
            def saveedit():
                treeview.set(item, column=column, value=entryedit.get(0.0, "end"))
                entryedit.destroy()
                okb.destroy()
            okb = ttk.Button(root, text='OK', width=4, command=saveedit)
            okb.grid(column = 3, row = 9,padx = 1, pady=1)


        def CheckAllocation():
            children = treeview.getchildren()
            for child in children:
                print(treeview.set(child))

        treeview.bind('<Double-1>', set_cell_value) # Double-click the left button to enter the edit
        button_check = Button(root,text="Check Allocation", command = CheckAllocation)
        button_check.grid(column = 2, row = 10, padx = 10, pady=10)

'''




Tags: thetextinfordefcolumnrootitem

热门问题