PyQT5 實現(xiàn)快捷鍵復(fù)制表格數(shù)據(jù)的方法示例
本文主要介紹了PyQT5 實現(xiàn)快捷鍵復(fù)制表格數(shù)據(jù)的方法示例,分享給大家,具體如下:
表格數(shù)據(jù)如下:
# -*- coding:utf-8 -*- import pyperclip from PyQt5.QtWidgets import QApplication, QWidget from PyQt5.QtGui import QStandardItemModel, QStandardItem from PyQt5.QtCore import Qt from PyQt5 import QtCore, QtWidgets # 復(fù)制選擇表格數(shù)據(jù) def selected_tb_text(table_view): try: indexes = table_view.selectedIndexes() # 獲取表格對象中被選中的數(shù)據(jù)索引列表 indexes_dict = {} for index in indexes: # 遍歷每個單元格 row, column = index.row(), index.column() # 獲取單元格的行號,列號 if row in indexes_dict.keys(): indexes_dict[row].append(column) else: indexes_dict[row] = [column] # 將數(shù)據(jù)表數(shù)據(jù)用制表符(\t)和換行符(\n)連接,使其可以復(fù)制到excel文件中 text = '' for row, columns in indexes_dict.items(): row_data = '' for column in columns: data = table_view.model().item(row, column).text() if row_data: row_data = row_data + '\t' + data else: row_data = data if text: text = text + '\n' + row_data else: text = row_data return text except BaseException as e: print(e) return '' class Ui_Form(object): # UI類 def setupUi(self, Form): Form.setObjectName("Form") self.tableView = QtWidgets.QTableView(Form) self.tableView.setEnabled(True) self.tableView.setGeometry(QtCore.QRect(5, 5, 400, 200)) self.tableView.setObjectName("tableView") QtCore.QMetaObject.connectSlotsByName(Form) # 邏輯類 class StartRun(QWidget, Ui_Form): def __init__(self): super().__init__() self.init_ui() # 實例化窗體 self.show() def init_ui(self): self.setupUi(self) # 實例化控件 self.update_table_view() def update_table_view(self): data = [ (1, '張三', 18), (2, '李四', 29), (3, '王五', 25), (4, '趙六', 26), ] columns = ['id', 'name', 'age'] model = QStandardItemModel(len(data), len(columns)) # 設(shè)置數(shù)據(jù)層次結(jié)構(gòu),rows行cols列 model.setHorizontalHeaderLabels([str(i) for i in columns]) # 設(shè)置列名 for row in range(len(data)): for column in range(len(data[row])): item = QStandardItem(str(data[row][column])) model.setItem(row, column, item) # 設(shè)置每個位置的文本值 self.tableView.setModel(model) # 實例化表格視圖,設(shè)置模型為自定義的模型 def keyPressEvent(self, event): # 重寫鍵盤監(jiān)聽事件 # 監(jiān)聽 CTRL+C 組合鍵,實現(xiàn)復(fù)制數(shù)據(jù)到粘貼板 if (event.key() == Qt.Key_C) and QApplication.keyboardModifiers() == Qt.ControlModifier: text = selected_tb_text(self.tableView) # 獲取當(dāng)前表格選中的數(shù)據(jù) if text: pyperclip.copy(text) # 復(fù)制數(shù)據(jù)到粘貼板 if __name__ == '__main__': import sys app = QApplication(sys.argv) S = StartRun() sys.exit(app.exec_())
到此這篇關(guān)于PyQT5 實現(xiàn)快捷鍵復(fù)制表格數(shù)據(jù)的方法示例的文章就介紹到這了,更多相關(guān)PyQT5 快捷鍵復(fù)制表格數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python GUI庫圖形界面開發(fā)之PyQt5中QWebEngineView內(nèi)嵌網(wǎng)頁與Python的數(shù)據(jù)交互傳參詳細(xì)方法實例
- python GUI庫圖形界面開發(fā)之PyQt5瀏覽器控件QWebEngineView詳細(xì)使用方法
- pyqt5的QWebEngineView 使用模板的方法
- 利用PyQt5+Matplotlib 繪制靜態(tài)/動態(tài)圖的實現(xiàn)代碼
- PyQt5-QDateEdit的簡單使用操作
- Python+PyQt5+MySQL實現(xiàn)天氣管理系統(tǒng)
- python3.6.8 + pycharm + PyQt5 環(huán)境搭建的圖文教程
- PyQt5實現(xiàn)簡單的計算器
- PyQt5的QWebEngineView使用示例
相關(guān)文章
pycharm 使用心得(六)進(jìn)行簡單的數(shù)據(jù)庫管理
功能簡介:pycharm自帶了一個簡單的數(shù)據(jù)庫插件,可以比較方便的進(jìn)行簡單的數(shù)據(jù)庫操作。2014-06-06pycharm配置安裝autopep8自動規(guī)范代碼的實現(xiàn)
這篇文章主要介紹了pycharm配置安裝autopep8自動規(guī)范代碼的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03創(chuàng)建Python Docker鏡像的詳細(xì)步驟
Python和Docker是兩個極其流行的技術(shù),結(jié)合它們可以創(chuàng)建強大的應(yīng)用程序,Docker允許將應(yīng)用程序及其依賴項打包到一個獨立的容器中,而Python則提供了豐富的庫和工具來開發(fā)應(yīng)用程序,本文將提供如何創(chuàng)建Python Docker鏡像的全面指南,,需要的朋友可以參考下2023-12-12numpy中的delete刪除數(shù)組整行和整列的實例
今天小編就為大家分享一篇numpy中的delete刪除數(shù)組整行和整列的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05