python?(pyqt)?表格顯示圖片的實(shí)現(xiàn)方式
更新時(shí)間:2023年09月06日 09:43:52 作者:qq_278667286
這篇文章主要介紹了python?(pyqt)?表格顯示圖片的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
python (pyqt) 表格顯示圖片
調(diào)用接口
激活顯示數(shù)據(jù)所包含的圖片:
停止調(diào)用接口
幾秒鐘后表格填充固定圖片顯示為灰色:
代碼
#!/usr/bin/env python # coding=utf-8 # -*- coding: utf-8 -*- import sys, os import yaml from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * import random class IconDelegate(QStyledItemDelegate): def initStyleOption(self, option, index): super(IconDelegate, self).initStyleOption(option, index) if option.features & QStyleOptionViewItem.HasDecoration: s = option.decorationSize s.setWidth(option.rect.width()) option.decorationSize = s class VisionDetectTable(QWidget): data_signal = pyqtSignal(list) Instance = None @classmethod def ShowDetectData(cls, list): if cls.Instance: cls.Instance.data_signal.emit(list) def __init__(self, file_path=None): super(VisionDetectTable, self).__init__() self.columnCount = 3 self.rowCount = 5 self.count = 0 self.detect = [] # QWidget.__init__() self.file_path = sys.path[0] + "/vision_detect/" if file_path: self.file_path = file_path self.loadyaml() self.initUI() self.data_signal.connect(self.setData) VisionDetectTable.Instance = self self.freeze() self.timer_freeze = QTimer(self) self.timer_freeze_flag = 0 self.timer_freeze.timeout.connect(self.freeze_check) self.timer_freeze.start(3000) # test self.timer = QTimer(self) self.timer.timeout.connect(self.showTest) self.timer.start(7000) def freeze_check(self): if self.timer_freeze_flag >= 0: self.timer_freeze_flag = self.timer_freeze_flag - 1 if self.timer_freeze_flag == 0: self.freeze() pass def unfreeze(self): self.timer_freeze_flag = 2 self.setEnabled(True) pass def freeze(self): self.detect = self.vision_detect_list self.showData() self.setEnabled(False) pass def loadyaml(self): p = os.path.abspath(self.file_path + "info.yaml") # 加載自動(dòng)啟動(dòng)進(jìn)程的配置文件 f = open(p, 'r') # 當(dāng)前python目錄/filename d = yaml.load(f) self.vision_detect_list = d["info"] self.vision_detect_list_obj = {} # print self.vision_detect_list ''' - id: 0 label: keep_left name: keep_left namecn: 靠左 icon: traffic_sign_icon/keep_left.png - id: 1 label: keep_right name: keep_right namecn: 靠右 icon: traffic_sign_icon/keep_right.png - id: 2 label: limit_50 name: limit_50 namecn: 限速50 icon: traffic_sign_icon/limit_50.png ''' f.close() for i in self.vision_detect_list: self.vision_detect_list_obj[i["label"]] = i print i["icon"] self.vision_detect_list_obj[i["label"]] = i def showTest(self): self.count = self.count + 1 print self.count fakedata = [] for i in self.vision_detect_list: if random.random() > 0.5: fakedata.append(i) # self.setData(fakedata) VisionDetectTable.ShowDetectData(fakedata) pass def setData(self, lst): self.unfreeze() self.detect = lst self.showData() pass def showData(self): l = len(self.detect) # self.table.clear() self.table.clearContents() mc = self.columnCount * self.rowCount empty = mc - l for k in range(l): i = k / self.columnCount j = k % self.columnCount item = self.detect[k] # self.table.clearCellWidget(i, j) tcw = self.tableCellWidget(item) self.table.setCellWidget(i, j, tcw) # print k if empty > 0: item = {"label": "empty"} while l < mc: i = l / self.columnCount j = l % self.columnCount # item = self.detect[k] # self.table.clearCellWidget(i, j) tcw = self.tableCellWidget(item) self.table.setCellWidget(i, j, tcw) l = l + 1 pass def tableCellWidget(self, item): nm = item["label"] widget = QWidget() score="" if "score" in item: score=str(item["score"]) widget.setToolTip(nm+":"+score) hLayout = QVBoxLayout() hLayout.setContentsMargins(0, 0, 0, 0) widget.setLayout(hLayout) label = QLabel("") label.setAlignment(Qt.AlignCenter) # 水平居中 if not nm in self.vision_detect_list_obj: nm="other_sign" # imgsrc = self.file_path + self.vision_detect_list_obj[nm]["icon"] label.setPixmap(QPixmap(imgsrc).scaled(50, 50)) # 只有圖片 # labeltxt = QLabel(r'0.5 ') # labeltxt.setAlignment(Qt.AlignCenter) # 水平居中 hLayout.addWidget(label) # hLayout.addWidget(labeltxt) return widget pass def initUI(self): layout = QHBoxLayout() layout.setContentsMargins(0, 0, 0, 0) table = QTableWidget() table.horizontalHeader().setSectionResizeMode(QHeaderView.Interactive | QHeaderView.Stretch) # table.setGeometry(QRect(0, 0, self.geometry().width(), self.geometry().height() - 40)) table.setColumnCount(self.columnCount) table.setRowCount(self.rowCount) # table.setEditTriggers(QAbstractItemView.NoEditTriggers) table.setSelectionMode(QAbstractItemView.NoSelection) table.setFocusPolicy(Qt.NoFocus) # for i in QAbstractItemView: print QAbstractItemView self.setStyleSheet("QTableWidget::item:selected{ background-color: rgb(255,0,0);}") # 或#ffffff self.setStyleSheet("QTableWidget::item:focus{ background-color: rgb(0,255,255);}") # self.setStyleSheet("QTableView:item:selected {background-color: #FF9900; color: #0000FF}\n" # "QTableView:item:selected:focus {background-color: #ffff00; color: #FFffFF}") # table.setHorizontalHeaderLabels(['圖片1', '圖片2', '圖片3']) table.verticalHeader().setVisible(False) table.horizontalHeader().setVisible(False) table.setDragEnabled(False) # table.setIconSize(QSize(50, 50)) # delegate = IconDelegate(table) # table.setItemDelegate(delegate) for i in range(self.columnCount): # 讓列寬和圖片相同 table.setColumnWidth(i, 100) for i in range(self.rowCount): # 讓行高和圖片相同 table.setRowHeight(i, 80) layout.addWidget(table) self.setLayout(layout) self.table = table # self.table.setFixedSize(layout.sizeHint()) def resizeEvent(self, e): self.table.resize(self.width(), self.height()) # self.table.move(10, 20) if __name__ == '__main__': app = QApplication(sys.argv) layout = QVBoxLayout() layout.setContentsMargins(0, 0, 0, 0) widget = QWidget() widget.setLayout(layout) # widget.setStyleSheet('.QWidget{border-style:solid;border-width:3;margin:1px;padding:10px;border-color: red yellow blue green;}') widget.show() vdt = VisionDetectTable() # vdt.setContentsMargins(0, 0, 0, 0) layout.addWidget(vdt) # layout.addWidget(vdt,0,Qt.AlignCenter ) # vdt.show() sys.exit(app.exec_())
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
利用OpenCV實(shí)現(xiàn)YOLO對(duì)象檢測方法詳解
這篇文章主要介紹了如何使用YOLOV3對(duì)象檢測器、OpenCV和Python實(shí)現(xiàn)對(duì)圖像和視頻流的檢測。文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-01-01Python數(shù)據(jù)可視化繪圖實(shí)例詳解
數(shù)據(jù)可視化是指用圖形或表格的方式來呈現(xiàn)數(shù)據(jù)。圖表能夠清楚地呈現(xiàn)數(shù)據(jù)性質(zhì), 以及數(shù)據(jù)間或?qū)傩蚤g的關(guān)系。本文為大家分享了幾個(gè)Python數(shù)據(jù)可視化繪圖的實(shí)例,感興趣的可以了解一下2022-05-05Python中filter與lambda的結(jié)合使用詳解
今天小編就為大家分享一篇Python中filter與lambda的結(jié)合使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12Numpy中關(guān)于arctan和arctan2的區(qū)別
這篇文章主要介紹了Numpy中關(guān)于arctan和arctan2的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09Python中的進(jìn)程操作模塊(multiprocess.process)
這篇文章介紹了Python中的進(jìn)程操作模塊(multiprocess.process),文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05解決jupyter notebook 出現(xiàn)In[*]的問題
這篇文章主要介紹了解決jupyter notebook 出現(xiàn)In[*]的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04Python Reduce函數(shù)的高級(jí)用法詳解
這篇文章主要介紹了reduce函數(shù)的工作原理和應(yīng)用,同時(shí)提供豐富的示例代碼,方便更好地理解如何使用reduce函數(shù)來輕松解決復(fù)雜的數(shù)據(jù)聚合問題,需要的可以參考下2023-11-11