欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python3+PyQt5實(shí)現(xiàn)柱狀圖

 更新時間:2018年04月24日 09:25:31   作者:basisworker  
這篇文章主要為大家詳細(xì)介紹了python3+PyQt5實(shí)現(xiàn)柱狀圖的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文通過Python3+pyqt5實(shí)現(xiàn)了python Qt GUI 快速編程的16章的excise例子。

#!/usr/bin/env python3

import random
import sys
from PyQt5.QtCore import (QAbstractListModel, QAbstractTableModel,
  QModelIndex, QSize, QTimer, QVariant, Qt,pyqtSignal)
from PyQt5.QtWidgets import (QApplication, QDialog, QHBoxLayout,
  QListView, QSpinBox, QStyledItemDelegate,QStyleOptionViewItem, QWidget)
from PyQt5.QtGui import QColor,QPainter,QPixmap

class BarGraphModel(QAbstractListModel):
 dataChanged=pyqtSignal(QModelIndex,QModelIndex)
 def __init__(self):
  super(BarGraphModel, self).__init__()
  self.__data = []
  self.__colors = {}
  self.minValue = 0
  self.maxValue = 0


 def rowCount(self, index=QModelIndex()):
  return len(self.__data)


 def insertRows(self, row, count):
  extra = row + count
  if extra >= len(self.__data):
   self.beginInsertRows(QModelIndex(), row, row + count - 1)
   self.__data.extend([0] * (extra - len(self.__data) + 1))
   self.endInsertRows()
   return True
  return False


 def flags(self, index):
  #return (QAbstractTableModel.flags(self, index)|Qt.ItemIsEditable)
  return (QAbstractListModel.flags(self, index)|Qt.ItemIsEditable)

 def setData(self, index, value, role=Qt.DisplayRole):
  row = index.row()
  if not index.isValid() or 0 > row >= len(self.__data):
   return False
  changed = False
  if role == Qt.DisplayRole:
   value = value
   self.__data[row] = value
   if self.minValue > value:
    self.minValue = value
   if self.maxValue < value:
    self.maxValue = value
   changed = True
  elif role == Qt.UserRole:
   self.__colors[row] = value
   #self.emit(SIGNAL("dataChanged(QModelIndex,QModelIndex)"),
   #   index, index)
   self.dataChanged[QModelIndex,QModelIndex].emit(index, index)
   changed = True
  if changed:
   #self.emit(SIGNAL("dataChanged(QModelIndex,QModelIndex)"),
    #   index, index)
   self.dataChanged[QModelIndex,QModelIndex].emit(index, index)
  return changed


 def data(self, index, role=Qt.DisplayRole):
  row = index.row()
  if not index.isValid() or 0 > row >= len(self.__data):
   return QVariant()
  if role == Qt.DisplayRole:
   return self.__data[row]
  if role == Qt.UserRole:
   return QVariant(self.__colors.get(row,
     QColor(Qt.red)))
  if role == Qt.DecorationRole:
   color = QColor(self.__colors.get(row,
     QColor(Qt.red)))
   pixmap = QPixmap(20, 20)
   pixmap.fill(color)
   return QVariant(pixmap)
  return QVariant()


class BarGraphDelegate(QStyledItemDelegate):

 def __init__(self, minimum=0, maximum=100, parent=None):
  super(BarGraphDelegate, self).__init__(parent)
  self.minimum = minimum
  self.maximum = maximum


 def paint(self, painter, option, index):
  myoption = QStyleOptionViewItem(option)
  myoption.displayAlignment |= (Qt.AlignRight|Qt.AlignVCenter)
  QStyledItemDelegate.paint(self, painter, myoption, index)


 def createEditor(self, parent, option, index):
  spinbox = QSpinBox(parent)
  spinbox.setRange(self.minimum, self.maximum)
  spinbox.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
  return spinbox


 def setEditorData(self, editor, index):
  value = index.model().data(index, Qt.DisplayRole)
  editor.setValue(value)


 def setModelData(self, editor, model, index):
  editor.interpretText()
  model.setData(index, editor.value())


class BarGraphView(QWidget):

 WIDTH = 20

 def __init__(self, parent=None):
  super(BarGraphView, self).__init__(parent)
  self.model = None


 def setModel(self, model):
  self.model = model
  #self.connect(self.model,
  #  SIGNAL("dataChanged(QModelIndex,QModelIndex)"),
  #  self.update)
  self.model.dataChanged[QModelIndex,QModelIndex].connect(self.update)
  #self.connect(self.model, SIGNAL("modelReset()"), self.update)
  self.model.modelReset.connect(self.update)


 def sizeHint(self):
  return self.minimumSizeHint()


 def minimumSizeHint(self):
  if self.model is None:
   return QSize(BarGraphView.WIDTH * 10, 100)
  return QSize(BarGraphView.WIDTH * self.model.rowCount(), 100)


 def paintEvent(self, event):
  if self.model is None:
   return
  painter = QPainter(self)
  painter.setRenderHint(QPainter.Antialiasing)
  span = self.model.maxValue - self.model.minValue
  painter.setWindow(0, 0, BarGraphView.WIDTH * self.model.rowCount(),
       span)
  for row in range(self.model.rowCount()):
   x = row * BarGraphView.WIDTH
   index = self.model.index(row)
   color = QColor(self.model.data(index, Qt.UserRole))
   y = self.model.data(index)
   painter.fillRect(x, span - y, BarGraphView.WIDTH, y, color)


class MainForm(QDialog):

 def __init__(self, parent=None):
  super(MainForm, self).__init__(parent)

  self.model = BarGraphModel()
  self.barGraphView = BarGraphView()
  self.barGraphView.setModel(self.model)
  self.listView = QListView()
  self.listView.setModel(self.model)
  self.listView.setItemDelegate(BarGraphDelegate(0, 1000, self))
  self.listView.setMaximumWidth(100)
  self.listView.setEditTriggers(QListView.DoubleClicked|
          QListView.EditKeyPressed)
  layout = QHBoxLayout()
  layout.addWidget(self.listView)
  layout.addWidget(self.barGraphView, 1)
  self.setLayout(layout)

  self.setWindowTitle("Bar Grapher")
  QTimer.singleShot(0, self.initialLoad)


 def initialLoad(self):
  # Generate fake data
  count = 20
  self.model.insertRows(0, count - 1)
  for row in range(count):
   value = random.randint(1, 150)
   color = QColor(random.randint(0, 255), random.randint(0, 255),
       random.randint(0, 255))
   index = self.model.index(row)
   self.model.setData(index, value)
   self.model.setData(index, QVariant(color), Qt.UserRole)


app = QApplication(sys.argv)
form = MainForm()
form.resize(600, 400)
form.show()
app.exec_()

運(yùn)行結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • OpenCV2.3.1+Python2.7.3+Numpy等的配置解析

    OpenCV2.3.1+Python2.7.3+Numpy等的配置解析

    這篇文章主要介紹了OpenCV2.3.1+Python2.7.3+Numpy等的配置解析,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • Python中的文本相似度的計算方法總結(jié)

    Python中的文本相似度的計算方法總結(jié)

    在自然語言處理(NLP)領(lǐng)域,文本相似度計算是一個常見的任務(wù),本文為大家整理了Python中的文本相似度常見計算方法,希望對大家有所幫助
    2023-05-05
  • python實(shí)現(xiàn)根據(jù)文件關(guān)鍵字進(jìn)行切分為多個文件的示例

    python實(shí)現(xiàn)根據(jù)文件關(guān)鍵字進(jìn)行切分為多個文件的示例

    今天小編就為大家分享一篇python實(shí)現(xiàn)根據(jù)文件關(guān)鍵字進(jìn)行切分為多個文件的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • 如何基于Django實(shí)現(xiàn)上下文章跳轉(zhuǎn)

    如何基于Django實(shí)現(xiàn)上下文章跳轉(zhuǎn)

    這篇文章主要介紹了如何基于Django實(shí)現(xiàn)上下文章跳轉(zhuǎn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • python 深入了解GIL鎖詳細(xì)

    python 深入了解GIL鎖詳細(xì)

    這篇文章主要介紹了python 深入了解GIL鎖,python的使用者都知道Cpython解釋器有一個弊端,真正執(zhí)行時同一時間只會有一個線程執(zhí)行,這是由于設(shè)計者當(dāng)初設(shè)計的一個缺陷,里面有個叫GIL鎖的,但他到底是什么,接下來和小編一起進(jìn)入下面文章詳細(xì)內(nèi)容吧
    2021-10-10
  • 在Python中實(shí)現(xiàn)函數(shù)重載的示例代碼

    在Python中實(shí)現(xiàn)函數(shù)重載的示例代碼

    這篇文章主要介紹了在Python中實(shí)現(xiàn)函數(shù)重載的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • Python數(shù)據(jù)可視化JupyterNotebook繪圖生成高清圖片

    Python數(shù)據(jù)可視化JupyterNotebook繪圖生成高清圖片

    這篇文章主要為大家介紹了Python數(shù)據(jù)可視化中如何利用Jupyter Notebook繪圖生成高清圖片,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-09-09
  • Pytorch中index_select() 函數(shù)的實(shí)現(xiàn)理解

    Pytorch中index_select() 函數(shù)的實(shí)現(xiàn)理解

    這篇文章主要介紹了Pytorch中index_select() 函數(shù)的實(shí)現(xiàn)理解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Python面向?qū)ο笾腴T類和對象

    Python面向?qū)ο笾腴T類和對象

    這篇文章主要為大家介紹了Python入門類和對象,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • Python實(shí)現(xiàn)1-9數(shù)組形成的結(jié)果為100的所有運(yùn)算式的示例

    Python實(shí)現(xiàn)1-9數(shù)組形成的結(jié)果為100的所有運(yùn)算式的示例

    下面小編就為大家?guī)硪黄狿ython實(shí)現(xiàn)1-9數(shù)組形成的結(jié)果為100的所有運(yùn)算式的示例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11

最新評論