python3+PyQt5实现自定义窗口部件Counters
程序员文章站
2023-10-18 20:49:22
本文通过python3+pyqt5实现自定义部件–counters自定 窗口部件。这个窗口是3*3的网格。本文有两个例子如下:
/home/yrd/eric_wor...
本文通过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_()
运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 椰子鸡汤孕妇能喝吗,是可以的
下一篇: flash制作简单的电子相册
推荐阅读