生活随笔
收集整理的這篇文章主要介紹了
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:菜单栏,工具栏,状态栏的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。