pyqt 状态栏 添加按钮
时间: 2023-07-04 08:22:04 浏览: 44
在 PyQt 中,可以通过 QMainWindow 类中的 statusBar() 方法获取主窗口的状态栏,并在其中添加按钮。下面是一个简单的示例代码:
```python
from PyQt5.QtWidgets import QApplication, QMainWindow, QStatusBar, QPushButton
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 添加状态栏
self.status = self.statusBar()
# 添加按钮
self.button = QPushButton('点击我', self)
self.status.addPermanentWidget(self.button)
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
```
在这个例子中,我们创建了一个 QMainWindow 子类 MainWindow,并在其中添加了一个状态栏和一个按钮。其中,我们使用了 statusBar() 方法获取了主窗口的状态栏,并使用 addPermanentWidget() 方法将按钮添加到状态栏的永久项目中。这样,无论用户如何操作窗口,按钮都会一直显示在状态栏中。
你可以根据自己的需求修改按钮的样式、文本和功能。如果你还有其他问题,请继续提问。