如何将图像和文本添加到 QLabel
How to add both an image and text to a QLabel
我有一个带有 QLabel 的 QHBoxLayout,我正在尝试在 QLabel 中获取图标和 window 标题文本。那可能吗?或者甚至将图标直接添加到 QHBoxLayout,这样就放在 window 标题文本之前?
这是我的代码:
class MyBar(QWidget):
def __init__(self, parent):
super(MyBar, self).__init__()
self.parent = parent
self.layout = QHBoxLayout()
self.layout.setContentsMargins(0,0,0,0)
self.title = QLabel("Main Window")
def changetitle(self, msg):
self.title.setText(msg)
编辑:
这是我并排使用两个标签的代码:
self.label3 = QLabel(self)
self.title = QLabel("Main Window")
self.pixmap = QPixmap('res/myIcon.ico')
self.label3.setPixmap(self.pixmap)
self.label3.setAlignment(Qt.AlignCenter)
self.title.setFixedHeight(35)
self.title.setAlignment(Qt.AlignCenter)
self.layout.addWidget(self.label3)
self.layout.addWidget(self.title)
self.label3.setStyleSheet("""
background-color: black;
""")
self.title.setStyleSheet("""
background-color: black;
color: white;
""")
下面是一个基于您的代码的演示,它应该可以满足您的要求:
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
layout = QHBoxLayout(self)
self.label3 = QLabel(self)
self.title = QLabel("Wild Lion's Browser")
self.pixmap = QPixmap('icon48.png')
self.label3.setPixmap(self.pixmap)
self.label3.setAlignment(Qt.AlignCenter)
self.title.setMinimumHeight(self.pixmap.height())
self.title.setAlignment(Qt.AlignCenter)
layout.addWidget(self.label3)
layout.addWidget(self.title)
self.label3.setStyleSheet("""
background-color: black;
""")
self.title.setStyleSheet("""
background-color: black;
color: white;
padding: 0px 10px 0px 10px;
""")
layout.setSpacing(0)
layout.addStretch()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.setGeometry(600, 100, 200, 30)
window.show()
sys.exit(app.exec_())
我有一个带有 QLabel 的 QHBoxLayout,我正在尝试在 QLabel 中获取图标和 window 标题文本。那可能吗?或者甚至将图标直接添加到 QHBoxLayout,这样就放在 window 标题文本之前?
这是我的代码:
class MyBar(QWidget):
def __init__(self, parent):
super(MyBar, self).__init__()
self.parent = parent
self.layout = QHBoxLayout()
self.layout.setContentsMargins(0,0,0,0)
self.title = QLabel("Main Window")
def changetitle(self, msg):
self.title.setText(msg)
编辑:
这是我并排使用两个标签的代码:
self.label3 = QLabel(self)
self.title = QLabel("Main Window")
self.pixmap = QPixmap('res/myIcon.ico')
self.label3.setPixmap(self.pixmap)
self.label3.setAlignment(Qt.AlignCenter)
self.title.setFixedHeight(35)
self.title.setAlignment(Qt.AlignCenter)
self.layout.addWidget(self.label3)
self.layout.addWidget(self.title)
self.label3.setStyleSheet("""
background-color: black;
""")
self.title.setStyleSheet("""
background-color: black;
color: white;
""")
下面是一个基于您的代码的演示,它应该可以满足您的要求:
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
layout = QHBoxLayout(self)
self.label3 = QLabel(self)
self.title = QLabel("Wild Lion's Browser")
self.pixmap = QPixmap('icon48.png')
self.label3.setPixmap(self.pixmap)
self.label3.setAlignment(Qt.AlignCenter)
self.title.setMinimumHeight(self.pixmap.height())
self.title.setAlignment(Qt.AlignCenter)
layout.addWidget(self.label3)
layout.addWidget(self.title)
self.label3.setStyleSheet("""
background-color: black;
""")
self.title.setStyleSheet("""
background-color: black;
color: white;
padding: 0px 10px 0px 10px;
""")
layout.setSpacing(0)
layout.addStretch()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.setGeometry(600, 100, 200, 30)
window.show()
sys.exit(app.exec_())