Цветной текст в PyQt5
Как мне вывести цветной текст в menu_bar?
Пробовал через Colorama - ничего не получилось.
from PyQt5.QtWidgets import QMainWindow, QTextEdit, QMenuBar, QApplication
from PyQt5.QtCore import QObject
from PyQt5.QtCore import pyqtSignal as Signal
class OutputLogger(QObject):
emit_write = Signal(str, int)
class Severity:
DEBUG = 0
ERROR = 1
def __init__(self, io_stream, severity):
super().__init__()
self.io_stream = io_stream
self.severity = severity
def write(self, text):
self.io_stream.write(text)
self.emit_write.emit(text, self.severity)
def flush(self):
self.io_stream.flush()
import sys
OUTPUT_LOGGER_STDOUT = OutputLogger(sys.stdout, OutputLogger.Severity.DEBUG)
OUTPUT_LOGGER_STDERR = OutputLogger(sys.stderr, OutputLogger.Severity.ERROR)
sys.stdout = OUTPUT_LOGGER_STDOUT
sys.stderr = OUTPUT_LOGGER_STDERR
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.text_edit = QTextEdit()
OUTPUT_LOGGER_STDOUT.emit_write.connect(self.append_log)
OUTPUT_LOGGER_STDERR.emit_write.connect(self.append_log)
menu_bar = QMenuBar()
menu = menu_bar.addMenu('Say')
menu.addAction('hello', lambda: print('Hello!'))
menu.addAction('fail', lambda: print('Fail!', file=sys.stderr))
self.setMenuBar(menu_bar)
self.setCentralWidget(self.text_edit)
def append_log(self, text, severity):
text = repr(text)
if severity == OutputLogger.Severity.ERROR:
self.text_edit.append('<b>{}</b>'.format(text))
else:
self.text_edit.append(text)
if __name__ == '__main__':
app = QApplication([])
mw = MainWindow()
mw.show()
print('Go!')
app.exec()
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
Поиграйтесь со Style Sheet
from PyQt5 import QtWidgets, QtCore, QtGui
class Window(QtWidgets.QMainWindow):
def __init__(self):
super(Window, self).__init__()
mainMenu = self.menuBar()
mainMenu.setNativeMenuBar(False)
self.fileMenu = mainMenu.addMenu('Menu1')
action1 = QtWidgets.QWidgetAction(self)
self.label1 = QtWidgets.QLabel("action1")
action1.setDefaultWidget(self.label1);
action1.setText('action1')
action2 = QtWidgets.QWidgetAction(self)
self.label2 = QtWidgets.QLabel("action2")
action2.setDefaultWidget(self.label2);
action2.setText('action2')
action3 = QtWidgets.QWidgetAction(self)
self.label3 = QtWidgets.QLabel("action3")
action3.setDefaultWidget(self.label3);
action3.setText('action3')
self.fileMenu.addAction(action1)
self.fileMenu.addAction(action2)
self.fileMenu.addAction(action3)
self.fileMenu.triggered.connect(self.print_stuff)
def print_stuff(self, q):
print('whatever->', q.text() )
self.label1.setStyleSheet("""
QLabel { background-color : #ABABAB; padding: 10px 12px 10px 12px;}
QLabel:hover { background-color: #654321;}
""")
self.label2.setStyleSheet("""
QLabel { background-color : #ABABAB; padding: 10px 12px 10px 12px;}
QLabel:hover { background-color: #654321;}
""")
self.label3.setStyleSheet("""
QLabel { background-color : #ABABAB; padding: 10px 12px 10px 12px;}
QLabel:hover { background-color: #654321;}
""")
if q.text() == 'action1':
self.label1.setStyleSheet("""
QLabel { background-color : red; padding: 10px 12px 10px 12px;}
QLabel:hover { background-color: #C10000;}
""")
elif q.text() == 'action2':
self.label2.setStyleSheet("""
QLabel { background-color : red; padding: 10px 12px 10px 12px;}
QLabel:hover { background-color: #C10000;}
""")
elif q.text() == 'action3':
self.label3.setStyleSheet("""
QLabel { background-color : red; padding: 10px 12px 10px 12px;}
QLabel:hover { background-color: #C10000;}
""")
qss = """
QMenuBar {
background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,
stop:0 lightgray, stop:1 darkgray);
}
QMenuBar::item {
spacing: 3px;
padding: 2px 10px;
background-color: rgb(210,105,30);
color: rgb(255,255,255);
border-radius: 5px;
}
QMenuBar::item:selected {
background-color: rgb(244,164,96);
}
QMenuBar::item:pressed {
background: rgb(128,0,0);
}
QLabel {
background-color: #ABABAB;
color: rgb(255,255,255);
font: 12px;
padding: 10px 12px 10px 12px;
}
QLabel:hover {
background-color: #654321;
}
"""
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.setStyleSheet(qss) # <---
w = Window()
w.resize(500, 300)
w.show()
sys.exit(app.exec_())

