圆月山庄资源网 Design By www.vgjia.com
本文通过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.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
2024年11月07日
2024年11月07日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]