本文通过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 from PyQt5.QtCore import (QRectF, QSize, Qt) from PyQt5.QtWidgets import (QApplication, QSizePolicy,QWidget) from PyQt5.QtGui import QPainter,QPen BLANK, RED, YELLOW = range(3) class CountersWidget(QWidget): def __init__(self, parent=None): super(CountersWidget, self).__init__(parent) self.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)) self.grid = [[BLANK] * 3 for i in range(3)] self.selected = [0, 0] self.setMinimumSize(self.minimumSizeHint()) def sizeHint(self): return QSize(200, 200) def minimumSizeHint(self): return QSize(100, 100) def mousePressEvent(self, event): xOffset = self.width() / 3 yOffset = self.height() / 3 if event.x() < xOffset: x = 0 elif event.x() < 2 * xOffset: x = 1 else: x = 2 if event.y() < yOffset: y = 0 elif event.y() < 2 * yOffset: y = 1 else: y = 2 cell = self.grid[x][y] if cell == BLANK: cell = RED elif cell == RED: cell = YELLOW else: cell = BLANK self.grid[x][y] = cell self.selected = [x, y] self.update() def keyPressEvent(self, event): if event.key() == Qt.Key_Left: self.selected[0] = (2 if self.selected[0] == 0 else self.selected[0] - 1) elif event.key() == Qt.Key_Right: self.selected[0] = (0 if self.selected[0] == 2 else self.selected[0] + 1) elif event.key() == Qt.Key_Up: self.selected[1] = (2 if self.selected[1] == 0 else self.selected[1] - 1) elif event.key() == Qt.Key_Down: self.selected[1] = (0 if self.selected[1] == 2 else self.selected[1] + 1) elif event.key() == Qt.Key_Space: x, y = self.selected cell = self.grid[x][y] if cell == BLANK: cell = RED elif cell == RED: cell = YELLOW else: cell = BLANK self.grid[x][y] = cell self.update() def paintEvent(self, event=None): painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing, True) xOffset = self.width() / 3 yOffset = self.height() / 3 for x in range(3): for y in range(3): cell = self.grid[x][y] rect = (QRectF(x * xOffset, y * yOffset, xOffset, yOffset).adjusted(0.5, 0.5, -0.5, -0.5)) color = None if cell == RED: color = Qt.red elif cell == YELLOW: color = Qt.yellow if color is not None: painter.save() painter.setPen(Qt.black) painter.setBrush(color) painter.drawEllipse(rect.adjusted(2, 2, -2, -2)) painter.restore() if [x, y] == self.selected: painter.setPen(QPen(Qt.blue, 3)) else: painter.setPen(Qt.black) painter.drawRect(rect) if __name__ == "__main__": import sys app = QApplication(sys.argv) form = CountersWidget() form.setWindowTitle("Counters") form.show() app.exec_()
/home/yrd/eric_workspace/chap11/counters_dnd.py
#!/usr/bin/env python3 from PyQt5.QtCore import (QRectF, QSize, Qt) from PyQt5.QtWidgets import (QApplication, QSizePolicy,QWidget) from PyQt5.QtGui import QPainter,QPen,QPixmap,QCursor BLANK, RED, YELLOW = range(3) class CountersWidget(QWidget): def __init__(self, parent=None): super(CountersWidget, self).__init__(parent) self.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)) self.grid = [[BLANK] * 3 for i in range(3)] self.selected = [0, 0] self.setMinimumSize(self.minimumSizeHint()) def sizeHint(self): return QSize(200, 200) def minimumSizeHint(self): return QSize(100, 100) def _xFromEventX(self, event): xOffset = self.width() / 3 if event.x() < xOffset: x = 0 elif event.x() < 2 * xOffset: x = 1 else: x = 2 return x def _yFromEventY(self, event): yOffset = self.width() / 3 if event.y() < yOffset: y = 0 elif event.y() < 2 * yOffset: y = 1 else: y = 2 return y def mouseDoubleClickEvent(self, event): x = self._xFromEventX(event) y = self._yFromEventY(event) cell = self.grid[x][y] if cell == BLANK: cell = RED elif cell == RED: cell = YELLOW else: cell = BLANK self.grid[x][y] = cell self.selected = [x, y] self.update() def keyPressEvent(self, event): if event.key() == Qt.Key_Left: self.selected[0] = (2 if self.selected[0] == 0 else self.selected[0] - 1) elif event.key() == Qt.Key_Right: self.selected[0] = (0 if self.selected[0] == 2 else self.selected[0] + 1) elif event.key() == Qt.Key_Up: self.selected[1] = (2 if self.selected[1] == 0 else self.selected[1] - 1) elif event.key() == Qt.Key_Down: self.selected[1] = (0 if self.selected[1] == 2 else self.selected[1] + 1) elif event.key() == Qt.Key_Space: x, y = self.selected cell = self.grid[x][y] if cell == BLANK: cell = RED elif cell == RED: cell = YELLOW else: cell = BLANK self.grid[x][y] = cell self.update() def paintEvent(self, event=None): painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing, True) xOffset = self.width() / 3 yOffset = self.height() / 3 for x in range(3): for y in range(3): cell = self.grid[x][y] rect = (QRectF(x * xOffset, y * yOffset, xOffset, yOffset).adjusted(0.5, 0.5, -0.5, -0.5)) color = None if cell == RED: color = Qt.red elif cell == YELLOW: color = Qt.yellow if color is not None: painter.save() painter.setPen(Qt.black) painter.setBrush(color) painter.drawEllipse(rect.adjusted(2, 2, -2, -2)) painter.restore() if [x, y] == self.selected: painter.setPen(QPen(Qt.blue, 3)) else: painter.setPen(Qt.black) painter.drawRect(rect) def mousePressEvent(self, event): self.x = self._xFromEventX(event) self.y = self._yFromEventY(event) cell = self.grid[self.x][self.y] color = Qt.darkGray if cell == RED: color = Qt.red elif cell == YELLOW: color = Qt.yellow pixmap = QPixmap(12, 12) pixmap.fill(color) self.setCursor(QCursor(pixmap)) def mouseReleaseEvent(self, event): x = self._xFromEventX(event) y = self._yFromEventY(event) if self.x != x or self.y != y: cell = self.grid[self.x][self.y] self.grid[self.x][self.y] = BLANK self.grid[x][y] = cell self.selected = [x, y] self.update() self.setCursor(Qt.ArrowCursor) if __name__ == "__main__": import sys app = QApplication(sys.argv) form = CountersWidget() form.setWindowTitle("Counters") form.show() app.exec_()
运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年11月15日
2024年11月15日
- 黄乙玲1988-无稳定的爱心肝乱糟糟[日本东芝1M版][WAV+CUE]
- 群星《我们的歌第六季 第3期》[320K/MP3][70.68MB]
- 群星《我们的歌第六季 第3期》[FLAC/分轨][369.48MB]
- 群星《燃!沙排少女 影视原声带》[320K/MP3][175.61MB]
- 乱斗海盗瞎6胜卡组推荐一览 深暗领域乱斗海盗瞎卡组分享
- 炉石传说乱斗6胜卡组分享一览 深暗领域乱斗6胜卡组代码推荐
- 炉石传说乱斗本周卡组合集 乱斗模式卡组最新推荐
- 佟妍.2015-七窍玲珑心【万马旦】【WAV+CUE】
- 叶振棠陈晓慧.1986-龙的心·俘虏你(2006复黑限量版)【永恒】【WAV+CUE】
- 陈慧琳.1998-爱我不爱(国)【福茂】【WAV+CUE】
- 咪咕快游豪礼放送,百元京东卡、海量欢乐豆就在咪咕咪粉节!
- 双11百吋大屏焕新“热”,海信AI画质电视成最大赢家
- 海信电视E8N Ultra:真正的百吋,不止是大!
- 曾庆瑜1990-曾庆瑜历年精选[派森][WAV+CUE]
- 叶玉卿1999-深情之选[飞图][WAV+CUE]