[pyqt5 learning] - Menu bar (QMenu()), toolbar QToolBar learning

Table of contents

1. Menu bar (QMenu()) - usually at the top of the window

1) Steps to create menu bar

2) Signals and methods

3) Practical operation

 2. Toolbar - usually below the menu bar

1) Creation steps

2) Methods and signals

Signal:

method:

 3) Practical examples

3. Status bar QStateBar - used to display status information, usually at the bottom of the window

1) Usage steps

2) Take the menu bar click action to trigger the status bar as an example

 Edit


1. Menu bar (QMenu()) - usually at the top of the window

1) Steps to create menu bar

The menu bar is only available when the window type is QMainwindow. The specific steps are:

1) Get the window menu bar

2) Add items to the menu bar

3) Create new actions

4) Add new actions to the corresponding entries

		# 获取菜单栏
		bar = self.menuBar()
		# 往菜单栏添加菜单项目
		file = bar.addMenu("文件")
		# 给菜单项目添加子菜单
		new = file.addAction("新建")
		save = file.addAction("保存")

2) Signals and methods

Signal: mainly the action trigger signal triggled() on the menu bar

Method: Add shortcut keys to actions (setShortcut("CTRL+S")), etc.

3) Practical operation

from PyQt5.QtWidgets import QMainWindow,QApplication
from PyQt5.QtCore import QDateTime,QDate,QTime
import sys

class QMenuDemo(QMainWindow):
	def __init__(self):
		super(QMenuDemo, self).__init__()

		# 获取菜单栏
		bar = self.menuBar()
		# 往菜单栏添加菜单项目
		file = bar.addMenu("文件")
		# 给菜单项目添加子菜单
		new = file.addAction("新建")
		save = file.addAction("保存")
		save.setShortcut("CTRL+S") # 设置快捷键
		save.triggered.connect(self.saveActionSlot)

	def saveActionSlot(self):
		print(self.sender().text())

if __name__ == '__main__':
	app = QApplication(sys.argv)
	mainWin = QMenuDemo()
	mainWin.show()
	sys.exit(app.exec_())

 2. Toolbar - usually below the menu bar

1) Creation steps

1) Create a toolbar

2) Add actions to the toolbar

		# 添加工具栏
		toolBar = self.addToolBar("File")

		# 创建新的动作,添加到工具栏
		new = QAction(QIcon('icon.jpg'),"new",self)
		save = QAction(QIcon("icon.jpg"),"save",self)

		toolBar.addAction(new)
		toolBar.addAction(save)

2) Methods and signals

Signal:

1) Action trigger signal, the same as action.triggered() in the menu bar;

2) An action on the toolbar is triggered by the signal toolBar.actionTriggered

method:

1) Add a shortcut key to the action (setShortcut());

2) Set the action display mode on the toolbar:

①Only display the icon—— toolBar.setToolButtonStyle(Qt.ToolButtonTextOnly)

②Only display text—— toolBar.setToolButtonStyle(Qt.ToolButtonTextOnly)

③The icon text is displayed

Text is displayed under the icon - toolBar.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)

Text is displayed next to the icon - toolBar.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)

 

Note: The display mode of all actions on a toolbar is the same. If you need different display modes, you can create multiple toolbars and then set different display modes for different toolbars.

 3) Practical examples

from PyQt5.QtWidgets import QMainWindow,QApplication,QAction
from PyQt5.QtCore import QDateTime,QDate,QTime,Qt
from PyQt5.QtGui import QIcon
import sys

class QToolBarDemo(QMainWindow):
	def __init__(self):
		super(QToolBarDemo, self).__init__()

		# 添加工具栏
		toolBar = self.addToolBar("File")

		# 创建新的动作,添加到工具栏
		new = QAction(QIcon('icon.jpg'),"new",self)
		save = QAction(QIcon("icon.jpg"),"save",self)
		edit = QAction(QIcon("icon.jpg"),"edit",self)

		toolBar.addAction(new)
		toolBar.addAction(save)
		toolBar.addAction(edit)

		toolBar.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)

		toolBar.actionTriggered.connect(self.showAction) # 工具栏上的动作被触发的信号,自动传回触发的动作对象

		self.setWindowTitle("工具栏演示")

	def showAction(self,a):
		print(a.text())


if __name__ == '__main__':
	app = QApplication(sys.argv)
	mainWin = QToolBarDemo()
	mainWin.show()
	sys.exit(app.exec_())

3. Status bar QStateBar - used to display status information, usually at the bottom of the window

1) Usage steps

1) First create a status bar

self.statuBar = QStatusBar()

2) Then set the status bar to the window

self.setStatusBar(self.statusBar)

3) Display the information that needs to be displayed in the window

self.statuBar.showMessage(self.sender().text(), 5000)

2) Take the menu bar click action to trigger the status bar as an example

from PyQt5.QtWidgets import QMainWindow,QApplication,QStatusBar
from PyQt5.QtCore import QDateTime,QDate,QTime
import sys

class StatusBarDemo(QMainWindow):
	def __init__(self):
		super(StatusBarDemo, self).__init__()

		# 获取菜单栏
		bar = self.menuBar()
		# 往菜单栏添加菜单项目
		file = bar.addMenu("状态栏演示")
		# 给菜单项目添加子菜单
		new = file.addAction("显示状态栏")

		file.triggered.connect(self.showStatu) # 给菜单栏条目设置触发,会自动将触发的动作名称传给槽函数
		new.triggered.connect(self.showAction) # 给菜单栏动作设置触发,不会自动将触发的动作名称传给槽函数

		# 创建一个状态栏
		self.statuBar = QStatusBar()
		# 给窗口设置状态栏
		self.setStatusBar(self.statuBar)

	def showStatu(self,a):
		# 将消息显示在状态栏上5s
		self.statuBar.showMessage(a.text(),5000)

	def showAction(self):
		self.statuBar.showMessage(self.sender().text(), 5000)

if __name__ == '__main__':
	app = QApplication(sys.argv)
	mainWin = StatusBarDemo()
	mainWin.show()
	sys.exit(app.exec_())

 

 

Guess you like

Origin blog.csdn.net/qq_45769063/article/details/124992097