Python 如何更快地将值从带pandas的excel导入tkinter?
伙计们!你好吗?下面是我的代码,我在使用Python 如何更快地将值从带pandas的excel导入tkinter?,python,excel,pandas,tkinter,openpyxl,Python,Excel,Pandas,Tkinter,Openpyxl,伙计们!你好吗?下面是我的代码,我在使用insertData函数时遇到了这个问题。此函数用于使用Pandas将excel电子表格中的值输入tkinter树视图。几乎没问题,但速度太慢了,我不知道怎么修。如果有人能帮助我,我会很高兴的。 提前谢谢 from tkinter import * import ttk import openpyxl import pandas as pd nuScreen = Tk() nuScreen.title("ContrasinSystem - Usuário
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()
您想打开excel文件阅读多少次
def insertData():
对于范围内的m(行计数):
self.dataValues=pd.read_excel(r'Registros.xlsx',str(self.cmb.get()),
跳过\u空白\u行=真,跳过=0)
对于self.dataValues中的l:
dataVector.append(l)
self.treeview.insert(“,“end”,value=dataVector)
打印(self.dataValues)
是否应该省略第一个循环
def insertData():
self.dataValues=pd.read_excel(r'Registros.xlsx',str(self.cmb.get()),
跳过\u空白\u行=真,跳过=0)
对于self.dataValues中的l:
self.treeview.insert(“,“end”,value=l)
打印(self.dataValues)
您是否已确定代码的哪一部分比较慢?是在读取数据吗?是否将数据插入到树视图?还有别的事吗?抱歉迟到了,布莱恩。问题在于每行读取一行的循环。所以,问题与tkinter完全无关?我建议删除所有tkinter代码,只使用读取数据且速度较慢的循环。这样你可能会得到更多帮助。第一个也做得太慢:/,第二个只给了我树视图中8行的标题。我编辑了我的答案。这是因为列表dataVector
list只包含excel工作表中的标题。我更新了代码。请注意:用于self中的l。数据值:
在行中迭代。所以你只需要把这些行放到树视图中。无需使用dataVector
列表