最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)文章

  • 如何通過Python的pyttsx3庫將文字轉(zhuǎn)為音頻

    如何通過Python的pyttsx3庫將文字轉(zhuǎn)為音頻

    pyttsx3是一個(gè)開源的Python文本轉(zhuǎn)語音庫,可以將文本轉(zhuǎn)換為自然的人類語音,這篇文章主要介紹了如何通過Python的pyttsx3庫將文字轉(zhuǎn)為音頻,需要的朋友可以參考下
    2023-04-04
  • numpy中數(shù)組拼接、數(shù)組合并方法總結(jié)(append(),?concatenate,?hstack,?vstack,?column_stack,?row_stack,?np.r_,?np.c_等)

    numpy中數(shù)組拼接、數(shù)組合并方法總結(jié)(append(),?concatenate,?hstack,?vstack

    numpy庫是一個(gè)高效處理多維數(shù)組的工具,可以在進(jìn)行邊寫的數(shù)組計(jì)算上進(jìn)行一系列的操作,下面這篇文章主要給大家介紹了關(guān)于numpy中數(shù)組拼接、數(shù)組合并方法(append(),?concatenate,?hstack,?vstack,?column_stack,?row_stack,?np.r_,?np.c_等)的相關(guān)資料,需要的朋友可以參考下
    2022-08-08
  • PyTorch基礎(chǔ)之torch.nn.CrossEntropyLoss交叉熵?fù)p失

    PyTorch基礎(chǔ)之torch.nn.CrossEntropyLoss交叉熵?fù)p失

    這篇文章主要介紹了PyTorch基礎(chǔ)之torch.nn.CrossEntropyLoss交叉熵?fù)p失講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Python實(shí)現(xiàn)王者榮耀自動(dòng)刷金幣的完整步驟

    Python實(shí)現(xiàn)王者榮耀自動(dòng)刷金幣的完整步驟

    這篇文章主要介紹了Python實(shí)現(xiàn)王者農(nóng)藥自動(dòng)刷金幣的完整步驟,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • Python如何使用內(nèi)置庫matplotlib繪制折線圖

    Python如何使用內(nèi)置庫matplotlib繪制折線圖

    這篇文章主要介紹了Python如何使用內(nèi)置庫matplotlib繪制折線圖,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • 簡單了解Python變量作用域正確使用方法

    簡單了解Python變量作用域正確使用方法

    這篇文章主要介紹了簡單了解Python變量作用域正確使用方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Python從零打造高安全密碼管理器

    Python從零打造高安全密碼管理器

    在數(shù)字化時(shí)代,每人平均需要管理近百個(gè)賬號(hào)密碼,本文將帶大家深入剖析一個(gè)基于Python的高安全性密碼管理器實(shí)現(xiàn)方案,感興趣的小伙伴可以參考一下
    2025-04-04
  • python判斷數(shù)字是否是超級(jí)素?cái)?shù)冪

    python判斷數(shù)字是否是超級(jí)素?cái)?shù)冪

    這篇文章主要為大家詳細(xì)介紹了python判斷數(shù)字是否是超級(jí)素?cái)?shù)冪,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-09-09
  • python——全排列數(shù)的生成方式

    python——全排列數(shù)的生成方式

    今天小編就為大家分享一篇python——全排列數(shù)的生成方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Python騷操作之動(dòng)態(tài)定義函數(shù)

    Python騷操作之動(dòng)態(tài)定義函數(shù)

    這篇文章主要介紹了Python騷操作之動(dòng)態(tài)定義函數(shù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03

最新評(píng)論

临澧县| 武陟县| 乌海市| 贡觉县| 治县。| 太原市| 宿迁市| 通化市| 芮城县| 沾益县| 新丰县| 乌拉特中旗| 阳新县| 揭东县| 浙江省| 乌拉特中旗| 鲜城| 江川县| 宁化县| 天水市| 海淀区| 隆回县| 灵寿县| 衡阳市| 宕昌县| 定安县| 交城县| 高邮市| 田东县| 聂拉木县| 郁南县| 云阳县| 措勤县| 浦东新区| 广宗县| 交口县| 平乐县| 久治县| 黄山市| 策勒县| 于田县|