PyQt5 - Add border to ComboBox when it get pressed

PyQt5 - Add border to ComboBox when it get pressed

If you want to add a border to a QComboBox when it's pressed, you can make use of the PyQt5 StyleSheet mechanism to modify its appearance.

Here's how you can set a style for the QComboBox to have a border when it's pressed:

import sys from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QComboBox class AppDemo(QWidget): def __init__(self): super().__init__() self.resize(400, 300) layout = QVBoxLayout() combobox = QComboBox() combobox.addItems(['Option 1', 'Option 2', 'Option 3']) # Set the style for the combobox combobox.setStyleSheet(""" QComboBox:pressed { border: 2px solid red; } """) layout.addWidget(combobox) self.setLayout(layout) if __name__ == '__main__': app = QApplication(sys.argv) demo = AppDemo() demo.show() sys.exit(app.exec_()) 

In the above code, the QComboBox:pressed pseudo-state is used to modify the appearance of the QComboBox when it is pressed. The style is set to have a 2px solid red border when the combobox is pressed.

You can adjust the border properties (e.g., color, width) as per your needs.


More Tags

nodemon android-coordinatorlayout mat-table squarespace jose4j spring-boot webclient android-scrollview template-engine laravel-artisan

More Programming Guides

Other Guides

More Programming Examples