pyqt5工具栏用法
时间: 2023-06-29 22:05:02 浏览: 100
PyQt5中的工具栏是一种常用的用户界面元素,可以包含多个按钮、工具、菜单等,以方便用户操作。下面是一个简单的PyQt5工具栏的使用示例:
```python
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QToolBar, QAction
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 创建工具栏
toolbar = QToolBar(self)
self.addToolBar(toolbar)
# 创建动作
new_action = QAction("New", self)
new_action.triggered.connect(self.new_file)
open_action = QAction("Open", self)
open_action.triggered.connect(self.open_file)
save_action = QAction("Save", self)
save_action.triggered.connect(self.save_file)
# 添加动作到工具栏
toolbar.addAction(new_action)
toolbar.addAction(open_action)
toolbar.addAction(save_action)
self.setWindowTitle("PyQt5 ToolBar Demo")
self.show()
def new_file(self):
print("New file is clicked")
def open_file(self):
print("Open file is clicked")
def save_file(self):
print("Save file is clicked")
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
```
在上面的示例中,我们创建了一个MainWindow类,在该类中创建了一个工具栏,并添加了三个动作(New、Open、Save),每个动作都连接了一个对应的槽函数。最后,我们将工具栏添加到窗口中,并显示出来。
通过这个简单的例子,你可以了解到如何在PyQt5中使用工具栏,并添加动作到工具栏中。
阅读全文
相关推荐

















