pyqt5--TableWidGet
使用pyqt5展示excel的数据到桌面,并获取选中的数据内容
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import pandas as pd
import numpy as np class Ui_MainWindow(QMainWindow): def __init__(self):
super(QtWidgets.QMainWindow, self).__init__()
self.setupUi(self)
self.retranslateUi(self) def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(666, 488)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.retranslateUi(MainWindow) self.tableWidget = QtWidgets.QTableWidget(self.centralWidget)
self.tableWidget.setGeometry(QtCore.QRect(0, 60, 813, 371))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(0)
self.tableWidget.setRowCount(0)
self.tableWidget.setStyleSheet("selection-background-color:pink")
self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.tableWidget.setSelectionBehavior(QTableWidget.SelectRows) self.tableWidget.raise_() # 设置图标 self.pushButton = QtWidgets.QPushButton(self.centralWidget)
self.pushButton.setGeometry(QtCore.QRect(90, 20, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton.setText("打开")
MainWindow.setCentralWidget(self.centralWidget)
QtCore.QMetaObject.connectSlotsByName(MainWindow) self.pushButton.clicked.connect(self.openfile)
self.pushButton.clicked.connect(self.creat_table_show) # 确定
self.okButton = QtWidgets.QPushButton(self.centralWidget)
self.okButton.setGeometry(QtCore.QRect(180, 20, 75, 23))
self.okButton.setObjectName("okButton")
self.okButton.setText("确定")
MainWindow.setCentralWidget(self.centralWidget)
QtCore.QMetaObject.connectSlotsByName(MainWindow) self.okButton.clicked.connect(self.get_select) def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "测试数据"))
MainWindow.setWindowIcon(QIcon("./head.jpg"))
# MainWindow.show() def get_select(self):
# print(self.tableWidget.columnCount()) # 返回列数
# print(self.tableWidget.rowCount()) # 返回行数 colomn = self.tableWidget.columnCount()
row_list = set()
for i in self.tableWidget.selectionModel().selection().indexes():
row_list.add(i.row())
# print(row_list)
select_data = []
for row in row_list:
row_data = [self.tableWidget.item(row, p).text() for p in range(colomn)]
select_data.append(row_data)
print(select_data) def openfile(self): # 获取路径
openfile_name = QFileDialog.getOpenFileName(self, '选择文件', '', 'Excel files(*.xlsx , *.xls)') #print(openfile_name)
global path_openfile_name
path_openfile_name = openfile_name[0] def creat_table_show(self):
# 读取表格,转换表格
if len(path_openfile_name) > 0:
input_table = pd.read_excel(path_openfile_name)
# print(1,input_table)
input_table_rows = input_table.shape[0]
input_table_colunms = input_table.shape[1]
# print(2,input_table_rows)
# print(3,input_table_colunms)
input_table_header = input_table.columns.values.tolist()
#print(input_table_header) #读取表格,转换表格,给tablewidget设置行列表头 self.tableWidget.setColumnCount(input_table_colunms)
self.tableWidget.setRowCount(input_table_rows)
self.tableWidget.setHorizontalHeaderLabels(input_table_header) #给tablewidget设置行列表头 #遍历表格每个元素,同时添加到tablewidget中
for i in range(input_table_rows):
input_table_rows_values = input_table.iloc[[i]]
#print(input_table_rows_values)
input_table_rows_values_array = np.array(input_table_rows_values)
input_table_rows_values_list = input_table_rows_values_array.tolist()[0]
#print(input_table_rows_values_list)
for j in range(input_table_colunms):
input_table_items_list = input_table_rows_values_list[j]
#print(input_table_items_list)
# print(type(input_table_items_list)) #将遍历的元素添加到tablewidget中并显示 input_table_items = str(input_table_items_list)
newItem = QTableWidgetItem(input_table_items)
newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
self.tableWidget.setItem(i, j, newItem) #遍历表格每个元素,同时添加到tablewidget中
else:
self.centralWidget.show() if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
pyqt5--TableWidGet的更多相关文章
- pyqt5的使用目录
pyqt5的安装 我的第一个例子 标签控件QLabel的使用 按钮QPushButton 信号与槽 pyqt5模块介绍 pycharm活动模板 QObject 定时器 QWidget类-坐标系 ...
- [ PyQt入门教程 ] PyQt5中数据表格控件QTableWidget使用方法
如果你想让你开发的PyQt5工具展示的数据显得整齐.美观.好看,显得符合你的气质,可以考虑使用QTableWidget控件.之前一直使用的是textBrowser文本框控件,数据展示还是不太美观.其中 ...
- PyQt5单元格操作大全
1.显示二维列表数据(QTableView)控件 '''显示二维列表数据(QTableView)控件数据源model需要创建一个QTableView实例和一个数据源model,然后将其两者关联 MVC ...
- Pyqt5实现model/View,解决tableView出现空白行问题。
项目中表格需要显示5万条数据以上,并且实时刷新.开始使用的tableWidget,数据量一大显得力不从心,所以使用Qt的Model/View来重新实现.下面是更改之前编写的小Demo. import ...
- 用 eric6 与 PyQt5 实现python的极速GUI编程(系列04)---- PyQt5自带教程:地址簿(address book)
[引子] 在PyQt5自带教程中,地址簿(address book)程序没有完全实现界面与业务逻辑分离. 本文我打算用eric6+PyQt5对其进行改写,以实现界面与逻辑完全分离. [概览] 1.界面 ...
- PyQt5+Python3.5.2-32bit开发环境搭建
1.基本环境. Window 8.1 64bit Python3.5.2-32bit.exe PyQt5 2.安装python. 去官网下载32位版本的python3.5.2(就是x86那个) 备 ...
- python3使用pyqt5制作一个超简单浏览器
我们使用的是QWebview模块,这里也主要是展示下QWebview的用法. 之前在网上找了半天的解析网页的内容,都不是很清楚. 这是核心代码: webview = Qwebview() webvie ...
- Pyqt5 获取命令行参数sys.argv
大家有没有注意到,很多软件都能接收第三方应用触发命令行参数,根据参数打开想要的效果. 在windows任务管理器调取命令行列,我们同样能看到进程中有好多是带有参数的. 现在,我们用Pyqt5 (Py3 ...
- pyqt5 开发环境
pyqt5 pycharm mac下开发环境 brew install python3 安装python3 brew install pyqt5 配置pycharm http://blog.csdn. ...
- pyqt5
使用python爬虫需要使用webkit执行javascript,查找资料可使用pyqt5,在此记录下安装步骤 windows 安装pyqt5 操作webkit 可用版本 1. 下载python 3. ...
随机推荐
- Makefile之编译运行连接库方法
LIBS+= -L $$PWD/../HKUnifyCamera_one/Debug -lHKUnifyCamera -luuid -Wl,-rpath=$$PWD/../HKUnifyCamera_ ...
- px转rem vue vscode
1.vscode中安装px2rem 2.打开settings.json ,新增 "px2rem.rootFontSize": 75, 3.重启vscode 4.可以转换了
- Gamma阶段项目展示
Gamma阶段项目展示 一. 团队成员介绍 姓名 Gamma职责 个人博客 张圆宁 PM,后端 个人博客 王文珺 后端 个人博客 牛宇航 后端 个人博客 申化文 后端 个人博客 汪慕澜 测试,部署 个 ...
- .net core中使用efcore
官网:https://docs.microsoft.com/zh-cn/aspnet/core/data/ef-mvc/intro?view=aspnetcore-2.2#register-the-s ...
- 怎样把excel整列单元格批量改为文本格式
选中整列,进入“数据”菜单栏,点击“分列”,[列数据格式]选“文本”,点击“完成”即可.
- exports与module.exports的区别,以及export与export.defult的区别
在 JS 模块化编程的模块引入上, 主要有两种方式: CommonJS 模块标准 ES6 moduel 特性 1. CommonJS 模块引入:require() 模块导出:exports 或者 mo ...
- linux_utf
LANG=en_US.utf8 export LC_ALL=en_US.utf8
- Spring Cloud config之三:config-server因为server端和client端的健康检查导致服务超时阻塞问题
springcloud线上一个问题,当config-server连不上git时,微服务集群慢慢的都挂掉. 在入口层增加了日志跟踪问题: org.springframework.cloud.config ...
- golang面对对象
- java 操作实例
1.无重复字符的最长子串 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3. class Soluti ...