我需要在标签中显示图片和文字,这是我的代码:
I need to show a picture and text in a label, and this is my code:
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class MyLabel(QLabel):
def __init__(self):
super(MyLabel, self).__init__()
def paintEvent(self, QPaintEvent):
pos = QPoint(50, 50)
painter = QPainter(self)
painter.drawText(pos, 'hello,world')
painter.setPen(QColor(255, 255, 255))
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
layout = QHBoxLayout(self)
self.label = MyLabel()
self.pixmap = QPixmap('icon.png')
self.label.setPixmap(self.pixmap)
layout.addWidget(self.label)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
标签只显示文字,缺少图片.如何在标签中同时显示图像和文本.
The label only display the text, and the picture is missing. How to display both image and text in the label.
感谢 eyllanesc 解决了这个问题.
Thanks for eyllanesc to solve this problem.
不过,我还有两个问题.
However, I have another two questions.
发现如果我在MyLable的paintEvent中显示图片和文字,点赞:
I found that if I display the image and text in the paintEvent of MyLable, likes:
def paintEvent(self, QPaintEvent):
super(MyLabel, self).paintEvent(QPaintEvent)
pos = QPoint(50, 50)
painter = QPainter(self)
painter.drawText(pos, 'hello,world')
painter.setPen(QColor(255, 255, 255))
self.pixmap = QPixmap('C:\Users\zhq\Desktop\DicomTool\icon.png')
self.setPixmap(self.pixmap)
即使我先显示文本然后显示图像,文本也会显示在图像上.为什么?
The text was display over the image even I firstly display the text and then display the image. Why?
其次,当我在MyLabel的paintEvent中显示图片和文字没有super(MyLabel, self).paintEvent(QPaintEvent)时,发现只显示文字,而且图片不见了:
Second, when I display the image and text in the paintEvent of MyLabel without the super(MyLabel, self).paintEvent(QPaintEvent), I found only the text is shown, and the picture is missing:
def paintEvent(self, QPaintEvent):
pos = QPoint(50, 50)
painter = QPainter(self)
painter.drawText(pos, 'hello,world')
painter.setPen(QColor(255, 255, 255))
self.pixmap = QPixmap('C:\Users\zhq\Desktop\DicomTool\icon.png')
self.setPixmap(self.pixmap)
覆盖 paintEvent
方法你已经移除了显示 QPixmap
的行为,因此图像不可见.你应该做的是首先做 QLabel
的 paintEvent
方法总是做的,然后只绘制文本.
Overwriting the paintEvent
method you have removed the behavior of displaying the QPixmap
so the image is not visible. What you should do is first do what the paintEvent
method of QLabel
always does and then just paint the text.
class MyLabel(QLabel):
def __init__(self):
super(MyLabel, self).__init__()
def paintEvent(self, event):
super(MyLabel, self).paintEvent(event)
pos = QPoint(50, 50)
painter = QPainter(self)
painter.drawText(pos, 'hello,world')
painter.setPen(QColor(255, 255, 255))
<小时>
QLabel
出于优化的原因,仅在图像不同时才更新图像,因为它使用 QPixmap的cacheKey" rel="nofollow noreferrer">cacheKey()
,所以只在必要时绘制.
QLabel
for reasons of optimization only updates the image if it is different for it uses the cacheKey()
of QPixmap
, so only draw when necessary.
在第一次显示时,文本被绘制,然后设置 QPixmap
并且由于第一次调用时没有重绘 QPixmap
paintEvent()
,它再次绘制文本,然后您再次设置QPixmap
,但与上一个相同,我不绘制它,而是绘制一个保存在缓存中,所以在后面调用 paintEvent()
时,它只在缓存的初始图像上绘制文本.
In your first case the first time it is displayed, the text is painted, then you set the QPixmap
and since no QPixmap
is redrawn for the first time it calls paintEvent()
, it draws the text again, then you set the QPixmap
again but being the same as the previous one, I do not draw it but draw the one that is saved in the cache, so in the following times that paintEvent()
is called, it only draws the text on the initial image of the cache.
在第二种情况下,不使用父级的paintEvent()
,不使用缓存,所以QPixmap
不会被绘制,在这种情况下仅绘制文本.
In the second case, by not using the paintEvent()
of the parent, the cache is not used, so the QPixmap
will not be drawn and in that case only the text is drawn.
注意:不建议在 paintEvent()
方法中执行绘图以外的任务,这可能会导致类似无限循环的问题.
Note: it is not advisable to do a task other than drawing in the paintEvent()
method, you could cause problems like an infinite loop.
这篇关于如何在标签中显示图片和文本(PyQt)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!