生活随笔
收集整理的这篇文章主要介绍了
pyqt5讲解6:菜单栏,工具栏,状态栏
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
菜单栏QMenuBar
方法描述
| menuBar() | 用于返回主窗口的QMenuBar对象 |
| addMenu() | 将菜单添加到菜单栏; |
| addAction() | 在菜单中进行添加某些操作; |
| setEnabled | 将操作按钮设为禁用或者启用 |
| addSeperator | 在菜单中添加一条分界线 |
| clear() | 删除菜单,菜单栏的内容 |
| setShortcut() | 设置快捷键 |
| setText() | 设置菜单栏的文本 |
| setTitle() | 设置Qmenu小控件的标题 |
| text() | 返回QAction对象关联的文本 |
| title() | 返回Qmenu对象的标题 |
'''【简介】PyQt5中 Qmenu 例子'''import sys
from PyQt5
.QtCore
import *
from PyQt5
.QtGui
import *
from PyQt5
.QtWidgets
import *class MenuDemo(QMainWindow
):def __init__(self
, parent
=None):super(MenuDemo
, self
).__init__
(parent
)layout
= QHBoxLayout
()bar
= self
.menuBar
()file = bar
.addMenu
("File")file.addAction
("New")save
= QAction
("Save",self
)save
.setShortcut
("Ctrl+S")file.addAction
(save
)edit
= file.addMenu
("Edit")edit
.addAction
("copy")edit
.addAction
("paste")quit
= QAction
("Quit",self
)file.addAction
(quit
)file.triggered
[QAction
].connect
(self
.processtrigger
)quit
.triggered
.connect
(self
.process
)self
.setLayout
(layout
)self
.setWindowTitle
("menu 例子")self
.resize
(350,300)def processtrigger(self
,q
):print(q
.text
()+" is triggered")if q
.text
()=='Save':print('save按下啦')def process(self
):print('quit被按下啦')if __name__
== '__main__':app
= QApplication
(sys
.argv
)demo
= MenuDemo
()demo
.show
()sys
.exit
(app
.exec_
())
信号绑定两种方式:
triggered:信号发射
file.triggered
[QAction
].connect
(self
.processtrigger
)
quit
.triggered
.connect
(self
.process
)
工具栏QToolBar
方法描述
| addAction() | 添加具有文本或图标的按钮 |
| addSeperator() | 分组显示工具按钮 |
| addWight() | 添加工具栏按钮以外的按钮 |
| addToolbar() | 使用Qmainwindow类的方法添加一个新的工具栏 |
| setMovable() | 设置工具栏变得可移动 |
| setOrientation() | 工具栏的方向 Qt.Horizontal或者Qt.vertical |
import sys
from PyQt5
.QtCore
import *
from PyQt5
.QtGui
import *
from PyQt5
.QtWidgets
import *class ToolBarDemo(QMainWindow
):def __init__(self
, parent
=None):super(ToolBarDemo
, self
).__init__
(parent
)self
.setWindowTitle
("toolbar 例子")self
.resize
(300, 200)layout
= QVBoxLayout
()tb
= self
.addToolBar
("File")new
= QAction
(QIcon
("limi.jpg"), "new", self
)tb
.addAction
(new
)open = QAction
(QIcon
("41.png"), "open", self
)open.triggered
.connect
(self
.open)tb
.addAction
(open)save
= QAction
(QIcon
("3.jpg"), "save", self
)tb
.addAction
(save
)tb
.actionTriggered
[QAction
].connect
(self
.toolbtnpressed
)self
.setLayout
(layout
)def toolbtnpressed(self
, a
):print("pressed tool button is", a
.text
())def open(self
):print('打开啦')if __name__
== '__main__':app
= QApplication
(sys
.argv
)demo
= ToolBarDemo
()demo
.show
()sys
.exit
(app
.exec_
())
信号绑定发射两种方法:
open.triggered
.connect
(self
.open)tb
.actionTriggered
[QAction
].connect
(self
.toolbtnpressed
)
状态栏QStatusBar
方法描述
| addWight() | 在状态栏中添加指定的窗口小控件对象 |
| addPermanentWidget | 在状态栏中永久添加给定的小窗口对象 |
| showMessage() | 在状态栏中显示一条临时信息指定时间间隔 |
| clearMessage() | 删除正在显示的临时信息 |
| removeWidget() | 删除状态栏中指定的小控件 |
import sys
from PyQt5
.QtCore
import *
from PyQt5
.QtGui
import *
from PyQt5
.QtWidgets
import *class StatusDemo(QMainWindow
):def __init__(self
, parent
=None):super(StatusDemo
, self
).__init__
(parent
)bar
= self
.menuBar
()file = bar
.addMenu
("File")file.addAction
("show")file.triggered
[QAction
].connect
(self
.processTrigger
)self
.setCentralWidget
(QTextEdit
())self
.statusBar
= QStatusBar
() self
.setWindowTitle
("QStatusBar 例子")self
.setStatusBar
(self
.statusBar
)def processTrigger(self
,q
):if (q
.text
()=="show"):self
.statusBar
.showMessage
(q
.text
()+" 菜单选项被点击了",5000)if __name__
== '__main__':app
= QApplication
(sys
.argv
)demo
= StatusDemo
()demo
.show
()sys
.exit
(app
.exec_
())
本例子指定啦一个菜单对象和一个文本框
和状态栏有关的
self
.statusBar
= QStatusBar
()
self
.setStatusBar
(self
.statusBar
)def processTrigger(self
,q
):if (q
.text
()=="show"):self
.statusBar
.showMessage
(q
.text
()+" 菜单选项被点击了",5000)
状态信息很快消失
电气专业的计算机萌新,写博文不容易。如果你觉得本文对你有用,请点个赞支持下,谢谢。
总结
以上是生活随笔为你收集整理的pyqt5讲解6:菜单栏,工具栏,状态栏的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。