[英]Need to draw a image to replace the rectangle in PyQt5
I am writing a labyrinth on PyQt5, and it looks a labyrinth like this: 我在PyQt5上写了一个迷宫,看起来像这样的迷宫:
That is, the walls, start, finish, and portals look just colored rectangles. 也就是说,墙壁,起点,终点和入口看起来只是彩色的矩形。 And I had a question, how can I change them into images? 我有一个问题,如何将它们转换为图像?
Here I get the color for these rectangles. 在这里,我得到了这些矩形的颜色。 How can I change this drawRect on the same drawImage to draw pictures. 如何在同一drawImage上更改此drawRect以绘制图片。
Walls and Floor 墙壁和地板
def draw_maze(self, drawer):
for x in range(self.maze.width):
for y in range(self.maze.height):
if self.maze[x][y] == self.maze.WALL:
drawer.setPen(Qt.white)
drawer.setBrush(Qt.black)
drawer.drawRect(x * self.maze.scale,
y * self.maze.scale,
self.maze.scale,
self.maze.scale)
elif self.maze[x][y] == self.maze.EMPTY:
drawer.setPen(QColor(200, 200, 200))
drawer.setBrush(Qt.white)
drawer.drawRect(x * self.maze.scale,
y * self.maze.scale,
self.maze.scale,
self.maze.scale)
Start and Finish 开始和结束
def draw_start_and_finish(self, drawer):
if self.maze.start:
drawer.setPen(Qt.white)
drawer.setBrush(Qt.green)
drawer.drawRect(self.maze.start[0] * self.maze.scale,
self.maze.start[1] * self.maze.scale,
self.maze.scale,
self.maze.scale)
if self.maze.finishes:
drawer.setPen(Qt.white)
drawer.setBrush(Qt.blue)
for finish in self.maze.finishes:
drawer.drawRect(finish[0] * self.maze.scale,
finish[1] * self.maze.scale,
self.maze.scale,
self.maze.scale)
Portals 门户
def draw_portals(self, drawer):
colors = [QColor(0, 255, 255), QColor(255, 128, 0),
QColor(255, 0, 128), QColor(0, 102, 0),
QColor(0, 0, 102), QColor(153, 204, 255)]
drawer.setPen(Qt.black)
if self.portal1:
drawer.setBrush(colors[len(self.maze.portals)])
drawer.drawRect(self.portal1[0] * self.maze.scale,
self.portal1[1] * self.maze.scale,
self.maze.scale,
self.maze.scale)
for i in range(len(self.maze.portals)):
drawer.setBrush(colors[i])
for portal in self.maze.portals[i]:
drawer.drawRect(portal[0] * self.maze.scale,
portal[1] * self.maze.scale,
self.maze.scale,
self.maze.scale)
The path is also drawn with colored rectangles. 该路径也用彩色矩形绘制。
def draw_path(self, drawer):
if self.path:
if self.draw_instantly:
for point in self.path:
if self.maze.is_wall(point[0], point[1]):
self.set_color(drawer, Qt.red)
else:
self.set_color(drawer, Qt.yellow)
drawer.drawRect(point[0] * self.maze.scale,
point[1] * self.maze.scale,
self.maze.scale,
self.maze.scale)
else:
for i in range(self.count):
if self.maze.is_wall(self.path[i][0], self.path[i][1]):
self.set_color(drawer, Qt.red)
else:
self.set_color(drawer, Qt.yellow)
drawer.drawRect(self.path[i][0] * self.maze.scale,
self.path[i][1] * self.maze.scale,
self.maze.scale,
self.maze.scale)
self.count += 1
if self.count > len(self.path) - 1:
self.count = len(self.path) - 1
self.update()
Method to get color: 获得颜色的方法:
@staticmethod
def set_color(drawer, color):
drawer.setPen(Qt.black)
drawer.setBrush(color)
I apologize that I can not attach all the code, it is quite large, and scattered across many files. 抱歉,我无法附加所有代码,因为代码很大,而且分散在许多文件中。
Ps Added a few hours later. 附言:几个小时后添加。
I provide code that works just like my: 我提供的代码就像我一样:
import sys
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter, QColor, QBrush
class MapEditor(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 350, 100)
self.setWindowTitle('MapEditor')
self.show()
def paintEvent(self, e):
drawer = QPainter()
drawer.begin(self)
self.drawRectangles(drawer)
drawer.end()
def drawRectangles(self, drawer):
col = QColor(0, 0, 0)
col.setNamedColor('#d4d4d4')
drawer.setPen(col)
drawer.setBrush(Qt.white)
drawer.setBrush(QColor(200, 0, 0))
drawer.drawRect(10, 15, 60, 60)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MapEditor()
sys.exit(app.exec_())
I need instead of this rectangle to have a picture of the same size. 我需要代替这个矩形来具有相同大小的图片。 I just do not know which method to use correctly. 我只是不知道正确使用哪种方法。 I tried all sorts of methods like DrawImage or something like: 我尝试了诸如DrawImage之类的各种方法:
label = QLabel(self)
pixmap = QPixmap(image_path)
label.setPixmap(pixmap)
But it didn't work out for me, and it gave out very incomprehensible errors that I unfortunately already lost. 但这对我没有用,它给出了非常不幸的错误,不幸的是我已经丢失了。
You have to use the drawPixmap()
method of QPainter
but before them you must scale the QPixmap
with the scaled()
method as shown below: 您必须使用QPainter
的drawPixmap()
方法,但是在使用它们之前,必须使用scaled()
方法scaled()
QPixmap
,如下所示:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class MapEditor(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 350, 100)
self.setWindowTitle('MapEditor')
def paintEvent(self, e):
drawer = QtGui.QPainter(self)
drawer.setRenderHint(QtGui.QPainter.Antialiasing)
r = QtCore.QRect(10, 15, 60, 60)
image_path = "red.png"
pixmap = QtGui.QPixmap(image_path)
pixmap = pixmap.scaled(r.size())
drawer.drawPixmap(r, pixmap)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
ex = MapEditor()
ex.show()
sys.exit(app.exec_())
red.png red.png
Output: 输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.