如何在标签中显示图片和文字(PyQt)
how to show picture and text in a label (PyQt)
我需要在标签中显示图片和文字,这是我的代码:
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_())
标签只显示文字,缺少图片。
如何在标签中同时显示图像和文本。
感谢eyllanesc解决这个问题。
不过,我还有两个问题。
我发现如果我在MyLable的paintEvent中显示图片和文字,喜欢:
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)
即使我先显示文本然后显示图像,文本也会显示在图像上。为什么?
其次,当我在没有super(MyLabel, self).paintEvent(QPaintEvent)的MyLabel的paintEvent中显示图片和文字时,发现只显示文字,没有图片:
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
方法总是做的事情,然后只绘制文本。
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()
,因此仅在必要时绘制。
在你的第一种情况下,第一次显示时,文本被绘制,然后你设置 QPixmap
并且由于第一次没有重绘 QPixmap
它调用 paintEvent()
,它又画了一遍文字,然后你又设置了QPixmap
,但是和上一个一样,我不画了,画了缓存中保存的那个,所以在接下来的时间里,paintEvent()
被调用时,它只在缓存的初始图像上绘制文本。
在第二种情况下,不使用父级的paintEvent()
,不使用缓存,所以不会绘制QPixmap
,在这种情况下只绘制文本。
注意:不建议在paintEvent()
方法中做除绘图以外的任务,可能会导致无限循环等问题.
我需要在标签中显示图片和文字,这是我的代码:
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_())
标签只显示文字,缺少图片。 如何在标签中同时显示图像和文本。
感谢eyllanesc解决这个问题。
不过,我还有两个问题。
我发现如果我在MyLable的paintEvent中显示图片和文字,喜欢:
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)
即使我先显示文本然后显示图像,文本也会显示在图像上。为什么?
其次,当我在没有super(MyLabel, self).paintEvent(QPaintEvent)的MyLabel的paintEvent中显示图片和文字时,发现只显示文字,没有图片:
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
方法总是做的事情,然后只绘制文本。
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()
,因此仅在必要时绘制。
在你的第一种情况下,第一次显示时,文本被绘制,然后你设置 QPixmap
并且由于第一次没有重绘 QPixmap
它调用 paintEvent()
,它又画了一遍文字,然后你又设置了QPixmap
,但是和上一个一样,我不画了,画了缓存中保存的那个,所以在接下来的时间里,paintEvent()
被调用时,它只在缓存的初始图像上绘制文本。
在第二种情况下,不使用父级的paintEvent()
,不使用缓存,所以不会绘制QPixmap
,在这种情况下只绘制文本。
注意:不建议在paintEvent()
方法中做除绘图以外的任务,可能会导致无限循环等问题.