📅  最后修改于: 2023-12-03 15:33:52.024000             🧑  作者: Mango
PyQt5 is a Python library that lets you create desktop applications that is compatible with Windows, MacOS, and Linux. QCalendarWidget is a widget to display a calendar, and in this tutorial, we will show you how to add a border to a QWidget child in QCalendarWidget.
from PyQt5.QtWidgets import QApplication, QCalendarWidget, QVBoxLayout, QWidget
from PyQt5.QtCore import Qt
class Calendar(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.cal = QCalendarWidget(self)
self.cal.setGridVisible(True)
self.cal.setGeometry(0, 0, 250, 250)
layout = QVBoxLayout()
layout.addWidget(self.cal)
self.setLayout(layout)
self.setStyleSheet("""QWidget {
border: 2px solid black;
border-radius: 10px;
padding: 5px;
}""")
if __name__ == '__main__':
app = QApplication([])
window = Calendar()
window.show()
app.exec_()
The Calendar
widget contains a QCalendarWidget as its child, and the stylesheet applied to self
affects its child as well. The stylesheet sets a 2-pixel-wide solid black border with a 10-pixel border radius and 5-pixel padding on all sides.
By default, the border is applied to the entire widget, which doesn't look very nice since the QCalendarWidget already has some padding and border built-in. Therefore, we specify that the border is only for the top-level QWidget by using the QWidget
selector.
In addition, setting the border-radius
property gives the border rounded corners, which is a nice visual touch.
By following this tutorial, you have learned how to add a border to a QWidget child in QCalendarWidget using PyQt5. This is just one example of the many customizations you can do with PyQt5's stylesheet feature.