python3+PyQt5泛型委托详解

自定义委托可以让我们对视图中出现的数据项的外观和行为进行完全控制。如果有很多模型,可能会希望不是全部的大多数模型能够仅用一个自定义委托,如果不能这么做,那么对于这些自定义委托,将很有可能存在大量重复代码。为了使得维护工作变得轻松,更好的方法为不要为每个模型创建一个自定义委托,而是用一系列的通用组件来共同构成一个委托。本文通过Python3+pyqt5实现了python Qt GUI 快速编程的16章的泛型委托例子。

/home/yrd/eric_workspace/chap16/richtextlineedit.py

#!/usr/bin/env python3

import platform
import sys
import html
from PyQt5.QtCore import QSize, Qt,pyqtSignal
from PyQt5.QtGui import QColor, QFont,QFontMetrics, QIcon, QKeySequence, QPixmap,QTextCharFormat
from PyQt5.QtWidgets import QAction,QApplication,QMenu,QTextEdit

class RichTextLineEdit(QTextEdit):
 returnPressed=pyqtSignal()
 (Bold, Italic, Underline, StrikeOut, Monospaced, Sans, Serif,
  NoSuperOrSubscript, Subscript, Superscript) = range(10)

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

  self.monofamily = "courier"
  self.sansfamily = "helvetica"
  self.seriffamily = "times"
  self.setLineWrapMode(QTextEdit.NoWrap)
  self.setTabChangesFocus(True)
  self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
  self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
  fm = QFontMetrics(self.font())
  h = int(fm.height() * (1.4 if platform.system() == "Windows"
         else 1.2))
  self.setMinimumHeight(h)
  self.setMaximumHeight(int(h * 1.2))
  self.setToolTip("Press <b>Ctrl+M</b> for the text effects "
    "menu and <b>Ctrl+K</b> for the color menu")

 def toggleItalic(self):
  self.setFontItalic(not self.fontItalic())

 def toggleUnderline(self):
  self.setFontUnderline(not self.fontUnderline())

 def toggleBold(self):
  self.setFontWeight(QFont.Normal
    if self.fontWeight() > QFont.Normal else QFont.Bold)

 def sizeHint(self):
  return QSize(self.document().idealWidth() + 5,
      self.maximumHeight())

 def minimumSizeHint(self):
  fm = QFontMetrics(self.font())
  return QSize(fm.width("WWWW"), self.minimumHeight())

 def contextMenuEvent(self, event):
  self.textEffectMenu()

 def keyPressEvent(self, event):
  if event.modifiers() & Qt.ControlModifier:
   handled = False
   if event.key() == Qt.Key_B:
    self.toggleBold()
    handled = True
   elif event.key() == Qt.Key_I:
    self.toggleItalic()
    handled = True
   elif event.key() == Qt.Key_K:
    self.colorMenu()
    handled = True
   elif event.key() == Qt.Key_M:
    self.textEffectMenu()
    handled = True
   elif event.key() == Qt.Key_U:
    self.toggleUnderline()
    handled = True
   if handled:
    event.accept()
    return
  if event.key() in (Qt.Key_Enter, Qt.Key_Return):
   self.returnPressed.emit()
   event.accept()
  else:
   QTextEdit.keyPressEvent(self, event)

 def colorMenu(self):
  pixmap = QPixmap(22, 22)
  menu = QMenu("Colour")
  for text, color in (
    ("&Black", Qt.black),
    ("B&lue", Qt.blue),
    ("Dark Bl&ue", Qt.darkBlue),
    ("&Cyan", Qt.cyan),
    ("Dar&k Cyan", Qt.darkCyan),
    ("&Green", Qt.green),
    ("Dark Gr&een", Qt.darkGreen),
    ("M&agenta", Qt.magenta),
    ("Dark Mage&nta", Qt.darkMagenta),
    ("&Red", Qt.red),
    ("&Dark Red", Qt.darkRed)):
   color = QColor(color)
   pixmap.fill(color)
   action = menu.addAction(QIcon(pixmap), text, self.setColor)
   action.setData(color)
  self.ensureCursorVisible()
  menu.exec_(self.viewport().mapToGlobal(
     self.cursorRect().center()))

 def setColor(self):
  action = self.sender()
  if action is not None and isinstance(action, QAction):
   color = QColor(action.data())
   if color.isValid():
    self.setTextColor(color)

 def textEffectMenu(self):
  format = self.currentCharFormat()
  menu = QMenu("Text Effect")
  for text, shortcut, data, checked in (
    ("&Bold", "Ctrl+B", RichTextLineEdit.Bold,
     self.fontWeight() > QFont.Normal),
    ("&Italic", "Ctrl+I", RichTextLineEdit.Italic,
     self.fontItalic()),
    ("Strike &out", None, RichTextLineEdit.StrikeOut,
     format.fontStrikeOut()),
    ("&Underline", "Ctrl+U", RichTextLineEdit.Underline,
     self.fontUnderline()),
    ("&Monospaced", None, RichTextLineEdit.Monospaced,
     format.fontFamily() == self.monofamily),
    ("&Serifed", None, RichTextLineEdit.Serif,
     format.fontFamily() == self.seriffamily),
    ("S&ans Serif", None, RichTextLineEdit.Sans,
     format.fontFamily() == self.sansfamily),
    ("&No super or subscript", None,
     RichTextLineEdit.NoSuperOrSubscript,
     format.verticalAlignment() ==
     QTextCharFormat.AlignNormal),
    ("Su&perscript", None, RichTextLineEdit.Superscript,
     format.verticalAlignment() ==
     QTextCharFormat.AlignSuperScript),
    ("Subs&cript", None, RichTextLineEdit.Subscript,
     format.verticalAlignment() ==
     QTextCharFormat.AlignSubScript)):
   action = menu.addAction(text, self.setTextEffect)
   if shortcut is not None:
    action.setShortcut(QKeySequence(shortcut))
   action.setData(data)
   action.setCheckable(True)
   action.setChecked(checked)
  self.ensureCursorVisible()
  menu.exec_(self.viewport().mapToGlobal(
     self.cursorRect().center()))

 def setTextEffect(self):
  action = self.sender()
  if action is not None and isinstance(action, QAction):
   what = action.data()
   if what == RichTextLineEdit.Bold:
    self.toggleBold()
    return
   if what == RichTextLineEdit.Italic:
    self.toggleItalic()
    return
   if what == RichTextLineEdit.Underline:
    self.toggleUnderline()
    return
   format = self.currentCharFormat()
   if what == RichTextLineEdit.Monospaced:
    format.setFontFamily(self.monofamily)
   elif what == RichTextLineEdit.Serif:
    format.setFontFamily(self.seriffamily)
   elif what == RichTextLineEdit.Sans:
    format.setFontFamily(self.sansfamily)
   if what == RichTextLineEdit.StrikeOut:
    format.setFontStrikeOut(not format.fontStrikeOut())
   if what == RichTextLineEdit.NoSuperOrSubscript:
    format.setVerticalAlignment(
      QTextCharFormat.AlignNormal)
   elif what == RichTextLineEdit.Superscript:
    format.setVerticalAlignment(
      QTextCharFormat.AlignSuperScript)
   elif what == RichTextLineEdit.Subscript:
    format.setVerticalAlignment(
      QTextCharFormat.AlignSubScript)
   self.mergeCurrentCharFormat(format)

 def toSimpleHtml(self):
  htmltext = ""
  black = QColor(Qt.black)
  block = self.document().begin()
  while block.isValid():
   iterator = block.begin()
   while iterator != block.end():
    fragment = iterator.fragment()
    if fragment.isValid():
     format = fragment.charFormat()
     family = format.fontFamily()
     color = format.foreground().color()
     text=html.escape(fragment.text())
     if (format.verticalAlignment() ==
      QTextCharFormat.AlignSubScript):
      text = "<sub>{0}</sub>".format(text)
     elif (format.verticalAlignment() ==
       QTextCharFormat.AlignSuperScript):
      text = "<sup>{0}</sup>".format(text)
     if format.fontUnderline():
      text = "<u>{0}</u>".format(text)
     if format.fontItalic():
      text = "<i>{0}</i>".format(text)
     if format.fontWeight() > QFont.Normal:
      text = "<b>{0}</b>".format(text)
     if format.fontStrikeOut():
      text = "<s>{0}</s>".format(text)
     if color != black or family:
      attribs = ""
      if color != black:
       attribs += ' color="{0}"'.format(color.name())
      if family:
       attribs += ' face="{0}"'.format(family)
      text = "<font{0}>{1}</font>".format(attribs,text)
     htmltext += text
    iterator += 1
   block = block.next()
  return htmltext

if __name__ == "__main__":
 def printout(lineedit):
  print(str(lineedit.toHtml()))
  print(str(lineedit.toPlainText()))
  print(str(lineedit.toSimpleHtml()))
 app = QApplication(sys.argv)
 lineedit = RichTextLineEdit()
 lineedit.returnPressed.connect(lambda:printout(lineedit))
 lineedit.show()
 lineedit.setWindowTitle("RichTextEdit")
 app.exec_()

/home/yrd/eric_workspace/chap16/genericdelegates.py

#!/usr/bin/env python3

from PyQt5.QtCore import (QDate, QSize, Qt)
from PyQt5.QtWidgets import (QApplication, QDateEdit, QLineEdit,
  QSpinBox, QStyledItemDelegate,QStyle)
from PyQt5.QtGui import QColor,QTextDocument
import richtextlineedit

class GenericDelegate(QStyledItemDelegate):

 def __init__(self, parent=None):
  super(GenericDelegate, self).__init__(parent)
  self.delegates = {}

 def insertColumnDelegate(self, column, delegate):
  delegate.setParent(self)
  self.delegates[column] = delegate

 def removeColumnDelegate(self, column):
  if column in self.delegates:
   del self.delegates[column]

 def paint(self, painter, option, index):
  delegate = self.delegates.get(index.column())
  if delegate is not None:
   delegate.paint(painter, option, index)
  else:
   QStyledItemDelegate.paint(self, painter, option, index)

 def createEditor(self, parent, option, index):
  delegate = self.delegates.get(index.column())
  if delegate is not None:
   return delegate.createEditor(parent, option, index)
  else:
   return QStyledItemDelegate.createEditor(self, parent, option,
             index)

 def setEditorData(self, editor, index):
  delegate = self.delegates.get(index.column())
  if delegate is not None:
   delegate.setEditorData(editor, index)
  else:
   QStyledItemDelegate.setEditorData(self, editor, index)

 def setModelData(self, editor, model, index):
  delegate = self.delegates.get(index.column())
  if delegate is not None:
   delegate.setModelData(editor, model, index)
  else:
   QStyledItemDelegate.setModelData(self, editor, model, index)

class IntegerColumnDelegate(QStyledItemDelegate):

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

 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 DateColumnDelegate(QStyledItemDelegate):

 def __init__(self, minimum=QDate(),
     maximum=QDate.currentDate(),
     format="yyyy-MM-dd", parent=None):
  super(DateColumnDelegate, self).__init__(parent)
  self.minimum = minimum
  self.maximum = maximum
  self.format = format

 def createEditor(self, parent, option, index):
  dateedit = QDateEdit(parent)
  #dateedit=QDateTimeEdit(parent)
  dateedit.setDateRange(self.minimum, self.maximum)
  dateedit.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
  dateedit.setDisplayFormat(self.format)
  dateedit.setCalendarPopup(True)
  return dateedit

 def setEditorData(self, editor, index):
  value = index.model().data(index, Qt.DisplayRole)
  #if value.isNull:
  editor.setDate(value)
  #editor.setDisplayFormat(self.format)

 def setModelData(self, editor, model, index):
  model.setData(index, editor.date())
 def paint(self, painter, option, index):
  text = index.model().data(index, Qt.DisplayRole).toString(self.format)
  palette = QApplication.palette()
  document = QTextDocument()
  document.setDefaultFont(option.font)
  if option.state & QStyle.State_Selected:
   document.setHtml("<font color={0}>{1}</font>".format(palette.highlightedText().color().name(),text))
  else:
   document.setHtml(text)
  painter.save()
  color = (palette.highlight().color()
     if option.state & QStyle.State_Selected
     else QColor(index.model().data(index,
        Qt.BackgroundColorRole)))
  painter.fillRect(option.rect, color)
  painter.translate(option.rect.x(), option.rect.y())
  document.drawContents(painter)
  painter.restore()  

class PlainTextColumnDelegate(QStyledItemDelegate):

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

 def createEditor(self, parent, option, index):
  lineedit = QLineEdit(parent)
  return lineedit

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

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

class RichTextColumnDelegate(QStyledItemDelegate):

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

 def paint(self, painter, option, index):
  text = index.model().data(index, Qt.DisplayRole)
  palette = QApplication.palette()
  document = QTextDocument()
  document.setDefaultFont(option.font)
  if option.state & QStyle.State_Selected:
   document.setHtml("<font color={0}>{1}</font>".format(palette.highlightedText().color().name(),text))
  else:
   document.setHtml(text)
  painter.save()
  color = (palette.highlight().color()
     if option.state & QStyle.State_Selected
     else QColor(index.model().data(index,
        Qt.BackgroundColorRole)))
  painter.fillRect(option.rect, color)
  painter.translate(option.rect.x(), option.rect.y())
  document.drawContents(painter)
  painter.restore()

 def sizeHint(self, option, index):
  text = index.model().data(index).toString()
  document = QTextDocument()
  document.setDefaultFont(option.font)
  document.setHtml(text)
  return QSize(document.idealWidth() + 5,
      option.fontMetrics.height())

 def createEditor(self, parent, option, index):
  lineedit = richtextlineedit.RichTextLineEdit(parent)
  return lineedit

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

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

/home/yrd/eric_workspace/chap16/carhirelog.pyw

#!/usr/bin/env python3

import bisect
import os
import platform
import sys
from PyQt5.QtCore import (QAbstractTableModel, QDate, QModelIndex,
  QVariant, Qt,pyqtSignal)
from PyQt5.QtWidgets import (QApplication, QMainWindow,
  QShortcut, QTableView)
from PyQt5.QtGui import QKeySequence
import genericdelegates

(LICENSE, CUSTOMER, HIRED, MILEAGEOUT, RETURNED, MILEAGEBACK,
 NOTES, MILEAGE, DAYS) = range(9)

class CarHireLog(object):

 def __init__(self, license, customer, hired, mileageout,
     returned=QDate(), mileageback=0, notes=""):
  self.license = license  # plain text
  self.customer = customer # plain text
  self.hired = hired     # QDate
  self.mileageout = mileageout   # int
  self.returned = returned    # QDate
  self.mileageback = mileageback  # int
  self.notes = notes   # HTML

 def field(self, column):
  if column == LICENSE:
   return self.license
  elif column == CUSTOMER:
   return self.customer
  elif column == HIRED:
   return self.hired
  elif column == MILEAGEOUT:
   return self.mileageout
  elif column == RETURNED:
   return self.returned
  elif column == MILEAGEBACK:
   return self.mileageback
  elif column == NOTES:
   return self.notes
  elif column == MILEAGE:
   return self.mileage()
  elif column == DAYS:
   return self.days()
  assert False

 def mileage(self):
  return (0 if self.mileageback == 0
     else self.mileageback - self.mileageout)

 def days(self):
  return (0 if not self.returned.isValid()
     else self.hired.daysTo(self.returned))

 def __hash__(self):
  return super(CarHireLog, self).__hash__()

 def __eq__(self, other):
  if self.hired != other.hired:
   return False
  if self.customer != other.customer:
   return False
  if self.license != other.license:
   return False
  return id(self) == id(other)

 def __lt__(self, other):
  if self.hired < other.hired:
   return True
  if self.customer < other.customer:
   return True
  if self.license < other.license:
   return True
  return id(self) < id(other)

class CarHireModel(QAbstractTableModel):
 dataChanged = pyqtSignal(QModelIndex,QModelIndex)
 def __init__(self, parent=None):
  super(CarHireModel, self).__init__(parent)
  self.logs = []

  # Generate fake data
  import gzip
  import random
  import string
  surname_data = gzip.open(os.path.join(
    os.path.dirname(__file__), "surnames.txt.gz")).read()
  surnames = surname_data.decode("utf8").splitlines()
  years = ("06 ", "56 ", "07 ", "57 ", "08 ", "58 ")
  titles = ("Ms ", "Mr ", "Ms ", "Mr ", "Ms ", "Mr ", "Dr ")
  notetexts = ("Returned <font color=red><b>damaged</b></font>",
    "Returned with <i>empty fuel tank</i>",
    "Customer <b>complained</b> about the <u>engine</u>",
    "Customer <b>complained</b> about the <u>gears</u>",
    "Customer <b>complained</b> about the <u>clutch</u>",
    "Returned <font color=darkred><b>dirty</b></font>",)
  today = QDate.currentDate()
  for i in range(250):
   license = []
   for c in range(5):
    license.append(random.choice(string.ascii_uppercase))
   license = ("".join(license[:2]) + random.choice(years) +
      "".join(license[2:]))
   customer = random.choice(titles) + random.choice(surnames)
   hired = today.addDays(-random.randint(0, 365))
   mileageout = random.randint(10000, 30000)
   notes = ""
   if random.random() >= 0.2:
    days = random.randint(1, 21)
    returned = hired.addDays(days)
    mileageback = (mileageout +
        (days * random.randint(30, 300)))
    if random.random() > 0.75:
     notes = random.choice(notetexts)
   else:
    returned = QDate()
    mileageback = 0
   log = CarHireLog(license, customer, hired, mileageout,
        returned, mileageback, notes)
   bisect.insort(self.logs, log)

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

 def columnCount(self, index=QModelIndex()):
  return 9

 def data(self, index, role):
  if not index.isValid():
   return QVariant()
  if role == Qt.DisplayRole:
   log = self.logs[index.row()]
   value = log.field(index.column())
   if (index.column() in (MILEAGEBACK, MILEAGE, DAYS) and
    value == 0):
    return 0
   return value
  if (role == Qt.TextAlignmentRole and
   index.column() not in (LICENSE, CUSTOMER, NOTES)):
   return QVariant(int(Qt.AlignRight|Qt.AlignVCenter))
  if role == Qt.BackgroundColorRole:
   palette = QApplication.palette()
   if index.column() in (LICENSE, MILEAGE, DAYS):
    return QVariant(palette.alternateBase())
   else:
    return QVariant(palette.base())
  return QVariant()

 def setData(self, index, value, role=Qt.EditRole):
  if (index.isValid() and role == Qt.EditRole and
   index.column() not in (LICENSE, MILEAGE, DAYS)):
   log = self.logs[index.row()]
   column = index.column()
   if column == CUSTOMER:
    log.customer = value
   elif column == HIRED:
    #log.hired = value.toDate()
    log.hired = value
   elif column == MILEAGEOUT:
    log.mileageout = value
   elif column == RETURNED:
    #log.returned = value.toDate()
    log.returned = value
   elif column == MILEAGEBACK:
    log.mileageback = value
   elif column == NOTES:
    log.notes = value
   self.dataChanged[QModelIndex,QModelIndex].emit(index,index)
   return True
  return False

 def headerData(self, section, orientation, role):
  if role == Qt.TextAlignmentRole:
   if orientation == Qt.Horizontal:
    return QVariant(int(Qt.AlignCenter))
   return QVariant(int(Qt.AlignRight|Qt.AlignVCenter))
  if role != Qt.DisplayRole:
   return QVariant()
  if orientation == Qt.Horizontal:
   if section == LICENSE:
    return "License"
   elif section == CUSTOMER:
    return "Customer"
   elif section == HIRED:
    return "Hired"
   elif section == MILEAGEOUT:
    return "Mileage #1"
   elif section == RETURNED:
    return "Returned"
   elif section == MILEAGEBACK:
    return "Mileage #2"
   elif section == DAYS:
    return "Days"
   elif section == MILEAGE:
    return "Miles"
   elif section == NOTES:
    return "Notes"
  return section + 1

 def flags(self, index):
  flag = QAbstractTableModel.flags(self, index)
  if index.column() not in (LICENSE, MILEAGE, DAYS):
   flag |= Qt.ItemIsEditable
  return flag

class HireDateColumnDelegate(genericdelegates.DateColumnDelegate):

 def createEditor(self, parent, option, index):
  i = index.sibling(index.row(), RETURNED)
  self.maximum = i.model().data(i, Qt.DisplayRole).addDays(-1)
  return genericdelegates.DateColumnDelegate.createEditor(
    self, parent, option, index)

class ReturnDateColumnDelegate(genericdelegates.DateColumnDelegate):

 def createEditor(self, parent, option, index):
  i = index.sibling(index.row(), HIRED)
  self.minimum = i.model().data(i, Qt.DisplayRole).addDays(1)
  return genericdelegates.DateColumnDelegate.createEditor(
    self, parent, option, index)

class MileageOutColumnDelegate(genericdelegates.IntegerColumnDelegate):

 def createEditor(self, parent, option, index):
  i = index.sibling(index.row(), MILEAGEBACK)
  maximum = i.model().data(i, Qt.DisplayRole)
  self.maximum = 1000000 if maximum == 0 else maximum - 1
  return genericdelegates.IntegerColumnDelegate.createEditor(
    self, parent, option, index)

class MileageBackColumnDelegate(genericdelegates.IntegerColumnDelegate):

 def createEditor(self, parent, option, index):
  i = index.sibling(index.row(), MILEAGEOUT)
  self.minimum = i.model().data(i, Qt.DisplayRole) + 1
  return genericdelegates.IntegerColumnDelegate.createEditor(
    self, parent, option, index)

class MainForm(QMainWindow):

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

  model = CarHireModel(self)

  self.view = QTableView()
  self.view.setModel(model)
  self.view.resizeColumnsToContents()

  delegate = genericdelegates.GenericDelegate(self)
  delegate.insertColumnDelegate(CUSTOMER,
    genericdelegates.PlainTextColumnDelegate())
  earliest = QDate.currentDate().addYears(-3)
  delegate.insertColumnDelegate(HIRED,
    HireDateColumnDelegate(earliest))
  delegate.insertColumnDelegate(MILEAGEOUT,
    MileageOutColumnDelegate(0, 1000000))
  delegate.insertColumnDelegate(RETURNED,
    ReturnDateColumnDelegate(earliest))
  delegate.insertColumnDelegate(MILEAGEBACK,
    MileageBackColumnDelegate(0, 1000000))
  delegate.insertColumnDelegate(NOTES,
    genericdelegates.RichTextColumnDelegate())

  self.view.setItemDelegate(delegate)
  self.setCentralWidget(self.view)

  QShortcut(QKeySequence("Escape"), self, self.close)
  QShortcut(QKeySequence("Ctrl+Q"), self, self.close)

  self.setWindowTitle("Car Hire Logs")

app = QApplication(sys.argv)
form = MainForm()
rect = QApplication.desktop().availableGeometry()
form.resize(int(rect.width() * 0.7), int(rect.height() * 0.8))
form.move(0, 0)
form.show()
app.exec_()

运行结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

您可能感兴趣的文章:

  • python3+PyQt5图形项的自定义和交互 python3实现page Designer应用程序
  • python3+PyQt5实现自定义窗口部件Counters
  • python3+PyQt5实现支持多线程的页面索引器应用程序
  • python3+PyQt5+Qt Designer实现扩展对话框
  • python3+PyQt5+Qt Designer实现堆叠窗口部件
(0)

相关推荐

  • python3+PyQt5+Qt Designer实现扩展对话框

    本文是对<Python Qt GUI快速编程>的第9章的扩展对话框例子Find and replace用Python3+PyQt5+Qt Designer进行改写. 第一部分无借用Qt Designer,完全用代码实现. 第二部分则借用Qt Designer,快速实现. 第一部分: import sys from PyQt5.QtCore import Qt,pyqtSignal from PyQt5.QtWidgets import (QApplication, QCheckBox, QDi

  • python3+PyQt5实现自定义窗口部件Counters

    本文通过Python3+PyQt5实现自定义部件–Counters自定 窗口部件.这个窗口是3*3的网格.本文有两个例子如下: /home/yrd/eric_workspace/chap11/counters.py. /home/yrd/eric_workspace/chap11/counters_dnd.py 第二个例子在第一个例子的基础上实现能通过鼠标拖拽球到不同的网格中. /home/yrd/eric_workspace/chap11/counters.py #!/usr/bin/env

  • python3+PyQt5图形项的自定义和交互 python3实现page Designer应用程序

    本文通过Python3+PyQt5实现<python Qt Gui 快速编程>这本书的page Designer应用程序,采用QGraphicsView,QGraphicsScene,QGraphicsItem,这个程序包含有多个文本,图片和框的页面.有些图形类在PyQt5已过时,所以本代码改动幅度比较大.主要的类或方法的改变如下: QMatrix==>QTransform setMatrix==>setTransform rotate ==> setRotation 本例中

  • python3+PyQt5+Qt Designer实现堆叠窗口部件

    本文是对<Python Qt GUI快速编程>的第9章的堆叠窗口例子Vehicle Rental用Python3+PyQt5+Qt Designer进行改写. 第一部分无借用Qt Designer,完全用代码实现. 第二部分则借用Qt Designer,快速实现. 第一部分: import sys from PyQt5.QtCore import (Qt) from PyQt5.QtWidgets import (QApplication, QComboBox, QDialog, QDialo

  • python3+PyQt5实现支持多线程的页面索引器应用程序

    本文通过Python3+pyqt5实现了python Qt GUI 快速编程的19章的页面索引器应用程序例子. /home/yrd/eric_workspace/chap19/walker_ans.py #!/usr/bin/env python3 import codecs import html.entities import re import sys from PyQt5.QtCore import (QMutex, QThread,pyqtSignal,Qt) class Walker

  • python3+PyQt5泛型委托详解

    自定义委托可以让我们对视图中出现的数据项的外观和行为进行完全控制.如果有很多模型,可能会希望不是全部的大多数模型能够仅用一个自定义委托,如果不能这么做,那么对于这些自定义委托,将很有可能存在大量重复代码.为了使得维护工作变得轻松,更好的方法为不要为每个模型创建一个自定义委托,而是用一系列的通用组件来共同构成一个委托.本文通过Python3+pyqt5实现了python Qt GUI 快速编程的16章的泛型委托例子. /home/yrd/eric_workspace/chap16/richtext

  • c#中的泛型委托详解

    今天学习一下c#中的泛型委托. 1.一般的委托,delegate,可以又传入参数(<=32),声明的方法为  public delegate void SomethingDelegate(int a); using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace delegateSummary { publ

  • python3+PyQt5自定义视图详解

    pyqt提供的几个视图类都可以较好工作,包括QLisView,QTableView和QTreeView.但是对于一些难以用现有的方式来呈现数据,这时,可以创建我们自己的视图子类并将其用做模型数据的可视化来解决这一问题.本文通过Python3+pyqt5实现了python Qt GUI 快速编程的16章的例子. #!/usr/bin/env python3 import gzip import os import platform import sys from PyQt5.QtCore impo

  • python3 shelve模块的详解

    python3 shelve模块的详解 一.简介 在python3中我们使用json或者pickle持久化数据,能dump多次,但只能load一次,因为先前的数据已经被后面dump的数据覆盖掉了.如果我们想要实现dump和load多次,可以使用shelve模块.shelve模块可以持久化所有pickle所支持的数据类型. 二.持久化数据 1.数据持久化 import shelve import datetime info = {'name': 'bigberg', 'age': 22} name

  • java 泛型的详解及实例

    java 泛型的详解及实例 Java在1.5版本中增加了泛型,在没有泛型之前,从集合中读取每一个对象都需要进行强转,如果一不小心插入了类型错误的对象,在运行时就会报错,给日常开发带来了很多不必要的麻烦,比如以下代码: public class TestGeneric { public static void main(String[] args) { List list = new ArrayList(); list.add(" name:"); list.add(" zer

  • Python3 Random模块代码详解

    描述 random() 方法返回随机生成的一个实数,它在[0,1)范围内. import random help(random) FUNCTIONS betavariate(alpha, beta) method of Random instance # 随机实例的方法 Beta distribution. # β分布 Conditions on the parameters are alpha > 0 and beta > 0. # 必须传入大于0的alpha 与beta参数 Returne

  • python3.5安装python3-tk详解

    在python3.5下安装好matplotlib后,准备显示一张图片测试一下,但是控制台报错说需要安装python3-tk,我天真的以为直接: sudo apt-get install python3-tk 就可以了呢.但是不行,说是找不到对应的资源.我就开始各种百度,谷歌,网上各种帖子,依然没有解决我的问题.后来找到一个python3-tk的安装包,deb格式的.我以为这样就行了呢,开始执行: sudo dpkg -i .....deb 发现它依赖blt,这是什么鬼,不管了,先安装再说.这又各

  • linux 编译安装python3.6的教程详解

    1. 首先去官网  https://www.python.org/downloads/source/ 下载 Gzipped source tarball 不要下载 " XZ compressed source tarball " ,目前 python3.6 最新版压缩包名字是:Python-3.6.7.tgz. 把下载的压缩包随便放到一个地方,也可以放到桌面(便于安装过后删除安装包和解压后的安装包). 2. 命令行切换到上面压缩文件所在的目录(比如桌面),然后输入 tar -xzf P

  • Ubuntu16.04安装python3.6.5步骤详解

    下载python3.6.5安装包 1.   上传安装包.打开终端,利用命令cd 进入文件所在文件夹里 python@ubuntu:~/workspace$pwd /home/python/workspace 2.   解压文件 tar xfzPython-3.6.5.tgz 注意:这里使用xfz命令,而不建议使用-xvzf命令,因为其释放的文件夹需要root权限才可以更改或者删除. python@ubuntu:~/workspace$tar xfz Python-3.6.5.tgz python

  • 手动安装python3.6的操作过程详解

    手动安装python3.6只需要将其ppa源加入apt仓库列表即可,但是最近常用的一个源 ppa:jonathonf/python-3.6 突然403拒绝访问了,费劲千辛万苦终于找到另一个可用的源 这里直接介绍安装的步骤,按顺序复制执行即可 先更新一下 sudo apt-get update 然后安装一些前置的依赖 sudo apt-get install -y software-properties-common 添加ppa源 sudo add-apt-repository -y ppa:fk

随机推荐