日常学习记录——tkinter显示excel表格中的数据
1 场景需求
点击西瓜数据按钮新建一个窗口,该窗口默认读取程序设定好的数据文件,并显示在创建好的表格控件中,如果需要导入新的文件可以通过更改文件按钮进行更改,点击显示按钮会将原来表格控件中的数据清除,并插入新数据用以显示。
2 运行效果
窗口1界面
点击西瓜数据按钮
点击更改文件按钮
选新文件以后,点击显示按钮(这里随便新建了一个文件)
2 代码实现
1 带垂直滚动条的表格:
代码如下:
# 原始数据显示Label(self.page, text="初始样本数据").grid(row=0, sticky=W)self.listBox = ttk.Treeview(self.page, height=15, columns=self.header, show='headings') # 创建表格self.VScroll = ttk.Scrollbar(self.page, orient='vertical', command=self.listBox.yview) # 创建滚动条self.listBox.configure(yscrollcommand=self.VScroll.set) # 滚动条与表格控件关联self.VScroll.grid(row=1, column=5, sticky=NS) # 滚动条放置位置for col in self.header: # 显示表头以及设置列宽self.listBox.column(col, width=100, anchor='center')self.listBox.heading(col, text=col)self.listBox.grid(row=1, column=0, columnspan=4) # 表格放置位置
2 读取数据
代码如下:
# 读取当前文件的数据def readDataSet(self):content = [] # 存放不包含表头的数据内容wb = load_workbook(self.data_path)sheet1 = wb.worksheets[self.sheet_index]# 迭代读取所有的行cnt = 0for row in sheet1.rows:row_val = [col.value for col in row]if cnt == 0:passelse:content.append(row_val)cnt = cnt + 1print(content)return content
3 显示数据
代码如下:
# 显示当前文件的轨道电路数据集def show(self):self.listBox.delete(*self.listBox.get_children()) # 清空原先表格tempList = self.readDataSet() # 导入当前文件数据for i, row in enumerate(tempList, start=1):self.listBox.insert("", "end", values=(i, row[1], row[2], row[3]))
3 涉及模块
- MainPage.py
from view import *
from Watermelon import *class MainPage(object):def __init__(self, master):self.root = master # 定义内部变量rootself.root.geometry('%dx%d' % (600, 400)) # 设置窗口大小self.createPage()def createPage(self):# 创建不同的界面frameself.selectPage = selectPage(self.root) # 子系统选择界面self.window1Page = window1_Frame(self.root) # 窗口1界面self.window2Page = window2_Frame(self.root) # 窗口2界面self.aboutPage = AboutFrame(self.root) # 关于信息界面# 窗口选择界面的按钮布局Button(self.selectPage, text='窗口1', font=('Microsoft YaHei', 12), command=self.window1_Disp).grid(stick=E + W)Label(self.selectPage).grid(stick=E + W) # 空一行Label(self.selectPage).grid(stick=E + W) # 空一行Button(self.selectPage, text='窗口2', font=('Microsoft YaHei', 12), command=self.window2_Disp).grid(stick=E + W)# 窗口1界面的按钮布局Button(self.window1Page, text='西瓜数据', font=('Microsoft YaHei', 12), command=self.Watermelon_Disp).grid(stick=E + W, padx=10)Label(self.window1Page).grid(stick=E + W) # 空一行Label(self.window1Page).grid(stick=E + W) # 空一行Button(self.window1Page, text='返回', font=('Microsoft YaHei', 12), command=self.selcetmodel).grid(stick=E + W,padx=10)# 窗口2系统界面的按钮布局Button(self.window2Page, text='其他界面', font=('Microsoft YaHei', 12)).grid(stick=E + W, padx=10)Label(self.window2Page).grid(stick=E + W) # 空一行Label(self.window2Page).grid(stick=E + W) # 空一行Button(self.window2Page, text='返回', font=('Microsoft YaHei', 12), command=self.selcetmodel).grid(stick=E + W,padx=10)self.selectPage.pack() # 默认显示诊断子系统选择界面menubar = Menu(self.root)menubar.add_command(label='窗口选择', command=self.selcetmodel)menubar.add_command(label='窗口1', command=self.window1_Disp)menubar.add_command(label='窗口2', command=self.window2_Disp)menubar.add_command(label='关于', command=self.about_Disp)self.root['menu'] = menubar # 设置菜单栏def selcetmodel(self):self.selectPage.pack()self.window1Page.pack_forget()self.window2Page.pack_forget()self.aboutPage.pack_forget()def window1_Disp(self):self.selectPage.pack_forget()self.window1Page.pack()self.window2Page.pack_forget()self.aboutPage.pack_forget()def window2_Disp(self):self.selectPage.pack_forget()self.window1Page.pack_forget()self.window2Page.pack()self.aboutPage.pack_forget()def about_Disp(self):self.selectPage.pack_forget()self.window1Page.pack_forget()self.window2Page.pack_forget()self.aboutPage.pack()def Watermelon_Disp(self): # FDT界面显示self.subroot = Tk() # 定义子窗口rootWatermelon_Page(self.subroot)
- watermelon.py
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from openpyxl import load_workbookclass Watermelon_Page(object):def __init__(self, master=None):self.root = master # 定义内部变量rootself.root.title('西瓜')self.root.geometry('%dx%d' % (600, 400)) # 设置窗口大小self.data_path = '西瓜.xlsx' # 初始西瓜数据集路径self.sheet_index = 0 # 默认获取第1张sheetself.header = ('序号', '密度', '含糖率', '是否是好瓜') # 显示数据的表头self.createPage()def createPage(self):self.page = Frame(self.root) # 创建Frameself.page.pack()# 原始数据显示Label(self.page, text="初始样本数据").grid(row=0, sticky=W)self.listBox = ttk.Treeview(self.page, height=15, columns=self.header, show='headings') # 创建表格self.VScroll = ttk.Scrollbar(self.page, orient='vertical', command=self.listBox.yview) # 创建滚动条self.listBox.configure(yscrollcommand=self.VScroll.set) # 滚动条与表格控件关联self.VScroll.grid(row=1, column=5, sticky=NS) # 滚动条放置位置for col in self.header: # 显示表头以及设置列宽self.listBox.column(col, width=100, anchor='center')self.listBox.heading(col, text=col)self.listBox.grid(row=1, column=0, columnspan=4)# tempList = self.readDataSet() # 表格显示初始数据# for i, row in enumerate(tempList, start=1):# self.listBox.insert("", "end", values=(i, row[1], row[2], row[3]))self.show() # 表格显示初始数据Button(self.page, text='更改文件', width=10, command=self.open_file).grid(row=16, column=0) # 重新导入数据按钮Button(self.page, text="显示", width=10, command=self.show).grid(row=16, column=1) # 显示当前文件数据的按钮Button(self.page, text="关闭", width=10, command=self.page.destroy).grid(row=16, column=2) # 显示当前文件数据的按钮# 读取excel文件函数def open_file(self):filepath = filedialog.askopenfilename(title='打开Excel文件', filetypes=[('Excel Files', '*.xlsx')])if filepath != '':self.data_path = filepathprint(self.data_path)# 读取当前文件的数据def readDataSet(self):content = [] # 存放不包含表头的数据内容wb = load_workbook(self.data_path)sheet1 = wb.worksheets[self.sheet_index]# 迭代读取所有的行cnt = 0for row in sheet1.rows:row_val = [col.value for col in row]if cnt == 0:passelse:content.append(row_val)cnt = cnt + 1print(content)return content# 显示当前文件的轨道电路数据集def show(self):self.listBox.delete(*self.listBox.get_children()) # 清空原先表格tempList = self.readDataSet() # 导入当前文件数据for i, row in enumerate(tempList, start=1):self.listBox.insert("", "end", values=(i, row[1], row[2], row[3]))
4 写在后面
demo工程文件已经上传至CSDN下载资源和GitHub上,CSDN下载链接、GitHub链接。
日常学习记录——tkinter显示excel表格中的数据相关推荐
- excel打开空白不显示内容 没有隐藏_办公软件操作技巧097:如何隐藏excel表格中没有数据的空白区域...
在日常工作中,我们编辑好了excel表格以后,有时为了突出显示表格数据内容,或者为了后期编辑表格数据区时不受干扰等原因,可以把表格中没有数据的多余空白区域隐藏起来,如下图一为数据正常显示,图二为隐藏空 ...
- 使用kettle采集excel表格中的数据
使用kettle采集excel表格中的数据 一.任务描述 二.任务目标 三.任务环境 四.任务分析 五. 任务实施 步骤1.环境准备 步骤2.创建Transformmations 申明: 未经许可,禁 ...
- 【唠叨两句】如何将一张树型结构的Excel表格中的数据导入到多张数据库表中...
小弟昨天遇到一个相对比较棘手的问题,就像标题说的那样.如何将一张树型结构的Excel表格中的数据导入到多张数据库表中,在现实中实际是七张数据库表,这七张表之间有着有着相对比较复杂的主外键关系,对于我这 ...
- python读取excel表格-python读取excel表格中的数据
使用python语言实现Excel 表格中的数据读取,需要用到xlrd.py模块,实现程序如下: import xlrd #导入xlrd模块 class ExcelData(): def __init ...
- 查询oracle数据库的表格数据类型,excel表格中如何查询数据库数据类型-我想把excel表格中的数据导入oracle数据库中,想在......
在excel表里,什么是:字段.记录.数据类型.多工... declare @t table(id numeric(18,2)) insert into @t SELECT col1 FROM ...
- matlab表格三维柱状图,excel制作四维数据表格-excel三维柱形图 ,请问如何根据excel表格中的数据......
Excel 什么是三维,四维表格 Excel表格中貌似没有三维.四维的概念,非得去解释,那么: 多张二维的表格构成三维的数据.四维只能是多个工作簿了. 怎样用excel制作一个小型数据库表格 Exce ...
- 用matlab处理表格,matlab删除excel表格数据-如何用matlab处理多个excel表格中的数据...
如何用matlab处理多个excel表格中的数据 biao='D:\Program Files\matlab\bin\filename.xls'; A1=xlsread (biao,'Sheet1', ...
- 计算机表格中如何计算数据透视表,Excel表格中在数据透视表中添加计算字段的方法...
计算字段是使用数据透视表中的字段同其他内容经过计算后得到的,如果用户需要在数据透视表中自定义计算公式以计算数据,可以通过添加计算字段来实现,下面介绍Excel表格中在数据透视表中添加计算字段的具体操作 ...
- 利用matlab处理表格数据,/如何用matlab处理多个excel表格中的数据
excel最大可处理多少条数据 理论上可以处理59999条(行)数据 但是实际上同一个工作表中无法处理这么多数. Excel 怎样做一个客户多个数据的表 1.首先,简单描述问题,在如下的表格中想姓名一 ...
最新文章
- spark的python开发安装方式_PyCharm搭建Spark开发环境的实现步骤
- Sql 中的变量使用
- 用C语言写HMI程序,HMI画面元素组成设计及代码生成方法与流程
- 我会永远永远的爱你,直到你不爱我的那一天
- 1040 有几个PAT(PAT乙级 C++)
- FreeSql (三)实体特性
- 浙江大学计算机考研408上岸,2016年跨考上岸浙江大学计算机研究生,初试412分经验谈!...
- VSFTP的PASSIVE模式的防火墙设置
- 如何查看磁盘分区情况
- Linux下DHCP服务的配置相关参数说明
- OO第三单元总结——JML
- python脚本-自动检测Base16、32、64、85系列编码、多层解码(新增base91解码)
- 电路交换,分组交换,报文交换
- 试题 基础练习 圆的面积-蓝桥杯
- MATLAB中使用plotyy绘制双纵坐标图及坐标轴设置
- 优思学院|精益生产的前世今生
- wfp 禁用ip_WFP 层要求和限制
- Accuracy, Precision, Recall和F1-score解释
- 一套政务OA系统,助力高效线上办公
- %d %ld %lld
热门文章
- 如何用Autojs写自己的卡密验证界面?实战代码
- 给大家介绍一个大文件传输软件
- 答疑丨北京积分落户,职住已加6分,还能加分吗?
- 南开大学计算机考研大纲,南开大学2019年考研812计算机综合基础考试大纲
- 论文笔记:Prefix-Tuning: Optimizing Continuous Prompts for Generation
- 数据分析进阶 - 使用Pyecharts搭建数据看板
- tutk云平台服务器_Tutk P2P的原理和常见的实现方式 - 书弋江山的博客 - CSDN博客...
- C#关于List的线程安全问题(一)
- AI比赛-NER:“万创杯”中医药天池大数据竞赛——中药说明书实体识别挑战
- ae需要安装哪些插件?——AE常用插件脚本预设整理合集(不定期更新)