PyQt6 python. Howto set text into a QLabel where a part of the text has a different color?
- User
PyQt6 python. Howto set text into a QLabel where a part of the text has a different color?
- Assistant
In PyQt6, you can set different text styles including color within a
QLabelby using HTML-like tags. To change the color of a part of the text, you can wrap that part in<span>tags with astyleattribute specifying the color.Here's an example of how to do this:
import sys from PyQt6.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget app = QApplication(sys.argv) # Create a main window window = QWidget() layout = QVBoxLayout() # Create a label with mixed color text label = QLabel() label.setText('This is <span style="color:red;">red</span> and this is <span style="color:blue;">blue</span>.') # Add the label to the layout and set the layout on the main window layout.addWidget(label) window.setLayout(layout) # Show the window window.show() # Run the application sys.exit(app.exec())
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.