pyqt 给widget加边界线
时间: 2023-07-23 22:44:59 浏览: 67
要给 PyQt 中的 widget 加边框线,可以使用 QFrame 控件。QFrame 是一个空间框架,可以用来包含其他小部件。对于想要添加边框线的 widget,可以将其放在一个 QFrame 中,并设置 QFrame 的边框样式为需要的样式。
以下是一个示例代码块,可以给一个 QLabel 添加一个边框线:
```python
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QFrame
from PyQt5.QtGui import QPainter, QColor
from PyQt5.QtCore import Qt
class MyLabel(QLabel):
def __init__(self, parent=None):
super().__init__(parent)
self.setFrameStyle(QFrame.Panel | QFrame.Sunken)
def paintEvent(self, event):
painter = QPainter(self)
painter.setPen(QColor(Qt.red))
painter.drawRect(self.rect())
super().paintEvent(event)
if __name__ == '__main__':
app = QApplication([])
widget = QWidget()
label = MyLabel(widget)
label.setText('Hello World!')
widget.show()
app.exec_()
```
在上述代码中,自定义了一个名为 MyLabel 的子类继承 QLabel,重写了 paintEvent 方法,使其在 widget 上绘制一个红色的边框线。在 MyLabel 的构造函数中,设置了 QFrame 的样式为 Panel 和 Sunken,这样就给 QLabel 添加了一个边框线。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)