How do you set the window border color in PyQt5?

To change the border color of a PyQt5 window, you can use QSS (Qt Style Sheets).

First, we need to import the classes required for QSS.

from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import Qt

Before creating the QApplication object, set the global style sheet.

app = QApplication([])
app.setStyleSheet("QMainWindow{border: 2px solid red;}")

The QMainWindow here can be replaced with another window class, such as QWidget.

Finally, create a window and display it.

window = QMainWindow()
window.setWindowTitle("Window with colored border")
window.show()

After running the program, the border color of the window will change to red.

Note: The code above can only change the color of the border. If you want to change the style, width, etc. of the border, you can use more style properties in QSS.

bannerAds