如何更快地将带有pandas的excel值导入tkinter?

2024-05-16 10:29:22 发布

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

伙计们!你好吗?我有下面的代码,我在使用insertData函数时遇到了这个问题。此函数用于使用Pandas将excel电子表格中的值输入tkinter树视图。几乎没问题,但太慢了,我不知道怎么修。如果有人能帮我,我会很高兴的。 提前谢谢!你知道吗

from tkinter import *
import ttk
import openpyxl
import pandas as pd

nuScreen = Tk()
nuScreen.title("ContrasinSystem - Usuário Comum")
nuScreen.iconbitmap("logocontransin.ico")

book = openpyxl.load_workbook('Registros.xlsx')
sheet = book.sheetnames
sh = book.active.cell

#Contador de Linhas:
wb2 = openpyxl.load_workbook('Registros.xlsx')
sheet2 = wb2.worksheets[0]

rowCount = sheet2.max_row

v = []

#Design:


class main():
    def __init__(self,tk):
        for x in range (0,len(sheet)):
            v.append(sheet[int(x)])


        self.wb2 = openpyxl.load_workbook('Registros.xlsx')
        self.sheet2 = self.wb2.worksheets[0]

        self.row_count = self.sheet2.max_row
        self.column_count = self.sheet2.max_column


        self.nuFrame = Frame(nuScreen, width = 1500, height = 450)

        self.nuFrame.pack()

        self.img = PhotoImage(file="logocontransin.png")
        self.w = Label(self.nuFrame, image = self.img)
        self.w.img = self.img
        self.w.place(x=65,y=150)


        self.srchlbl = ttk.Label(self.nuFrame, text = "Buscar:")
        self.srchlbl.place(x=25,y=75)
        self.srchetr = ttk.Entry(self.nuFrame, width = 30)
        self.srchetr.place(x=75,y=75)

        self.treeview = ttk.Treeview(self.nuFrame)
        self.treeview.place(x=300,y=75, width = 1100)

        dataVector = []      
        def columnsName():
            def Header():
                self.columnVector = []
                self.dataVector = []
                teste = []
                self.treeview.column("#0", width = 20)          
                self.columnHeader = pd.read_excel(r'Registros.xlsx', str(self.cmb.get()), header_only = True, nrows=0).columns
                for a in self.columnHeader:
                    self.columnVector.append(a)
                self.treeview.configure(columns = self.columnVector)
                for b in self.columnHeader:
                    self.treeview.heading(str(b), text = str(b))
            def insertData():
                for m in range(rowCount):
                    self.dataValues = pd.read_excel(r'Registros.xlsx',str(self.cmb.get()), skip_blank_lines=True, skiprows=0)
                for l in self.dataValues:
                    dataVector.append(l)
                self.treeview.insert("", "end",values = dataVector)
                print(self.dataValues)

            Header()
            insertData()


        self.cmbLbl = ttk.Label(self.nuFrame, text = "Assunto:")
        self.cmbLbl.place(x=1200, y=325)
        self.cmb = ttk.Combobox(self.nuFrame, values = v)
        self.cmb.place(x=1250,y=325)

        self.btncmb = ttk.Button(self.nuFrame, text = "Buscar", command = columnsName)
        self.btncmb.place(x=1320,y=375)

nuScreen.geometry("1500x450")
main(nuScreen)
nuScreen.mainloop()

Tags: inimportselffordefplacexlsxttk
1条回答
网友
1楼 · 发布于 2024-05-16 10:29:22

您要打开多少次excel文件进行读取?你知道吗

def insertData():
  for m in range(rowCount):
    self.dataValues = pd.read_excel(r'Registros.xlsx',str(self.cmb.get()), 
    skip_blank_lines=True, skiprows=0)
  for l in self.dataValues:
    dataVector.append(l)
    self.treeview.insert("", "end",values = dataVector)
    print(self.dataValues)

你应该省略第一个循环吗?你知道吗

def insertData():
  self.dataValues = pd.read_excel(r'Registros.xlsx',str(self.cmb.get()), 
  skip_blank_lines=True, skiprows=0)
  for l in self.dataValues:
    self.treeview.insert("", "end",values = l)
    print(self.dataValues)

相关问题 更多 >