python 3+PyQt 5 사용자 정의 창 위 젯 구현 Counters
/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_()
실행 결과:이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Jupyter 공식 DockerHub에 대한 메모에 기재되어 있다. base-notebook minimal-notebook scipy-notebook tensorflow-notebook datascience-notebook pyspark-notebook all-s...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.