setStyleSheet PyQt5使用实例
时间: 2024-09-18 22:18:19 浏览: 131
在PyQt5中,`setStyleSheet()`是一个用于设置QWidgets样式表的方法,它允许你动态地改变控件的外观,如颜色、字体、布局等。下面是一个简单的使用例子:
```python
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout
class StyledWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('PyQt5 StyleSheet Example')
# 创建一个按钮
button = QPushButton('Click me!')
# 设置样式表
style_sheet = """
QPushButton {
background-color: lightblue;
color: white;
border-radius: 8px;
padding: 10px 20px;
font-size: 16px;
}
QPushButton:hover {
background-color: darkblue;
}
"""
button.setStyleSheet(style_sheet)
# 布局管理
layout = QVBoxLayout()
layout.addWidget(button)
self.setLayout(layout)
if __name__ == '__main__':
app = QApplication([])
window = StyledWindow()
window.show()
# 运行主循环
app.exec_()
```
在这个例子中,我们创建了一个窗口,并在其中放置了一个按钮。通过设置`setStyleSheet()`,我们给按钮添加了自定义的颜色、形状和悬停效果。
阅读全文
相关推荐

















