📅  最后修改于: 2023-12-03 15:18:50.270000             🧑  作者: Mango
PyQt5是一个基于Python的GUI框架,它提供了大量的UI组件和工具等,使得开发GUI程序变得非常容易和简单。本文将介绍如何在PyQt5中为关闭状态下的组合框添加边框。
from PyQt5.QtWidgets import QApplication, QComboBox
from PyQt5.QtGui import QPainter, QColor, QBrush, QPen
from PyQt5.QtCore import Qt
combo_box = QComboBox(self)
def paintEvent(self, event):
painter = QPainter(self)
painter.setPen(QPen(Qt.black, 2))
painter.drawRect(self.rect())
完整示例代码:
from PyQt5.QtWidgets import QApplication, QComboBox
from PyQt5.QtGui import QPainter, QColor, QBrush, QPen
from PyQt5.QtCore import Qt
class ComboboxWithBorder(QComboBox):
def paintEvent(self, event):
painter = QPainter(self)
painter.setPen(QPen(Qt.black, 2))
painter.drawRect(self.rect())
if __name__ == '__main__':
app = QApplication([])
combo_box = ComboboxWithBorder()
combo_box.addItems(['item1', 'item2', 'item3'])
combo_box.show()
app.exec_()
下面是添加边框后的组合框效果图,可以看到在关闭状态下有了边框。
本文介绍了如何在PyQt5中为关闭状态下的组合框添加边框。通过重写paintEvent()方法并使用QPainter绘制矩形框即可实现。