目录
一、Qt Designer 在窗口中添加菜单和工具栏
二、源代码
1、界面代码
2、逻辑代码
3、演示
三、相关功能
一、Qt Designer 在窗口中添加菜单和工具栏
右键可以选择创建菜单栏。创建后双击【在这里输入】,然后输入菜单名,敲下回车确认,如图所示
二、源代码
1、界面代码
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(630, 369)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(50, 90, 511, 121))
font = QtGui.QFont()
font.setPointSize(50)
self.label.setFont(font)
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.menuBar = QtWidgets.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 630, 22))
self.menuBar.setObjectName("menuBar")
self.A = QtWidgets.QMenu(self.menuBar)
self.A.setObjectName("A")
self.C = QtWidgets.QMenu(self.A)
self.C.setObjectName("C")
MainWindow.setMenuBar(self.menuBar)
self.B = QtWidgets.QAction(MainWindow)
self.B.setObjectName("B")
self.D = QtWidgets.QAction(MainWindow)
self.D.setObjectName("D")
self.C.addAction(self.D)
self.A.addAction(self.B)
self.A.addAction(self.C.menuAction())
self.menuBar.addAction(self.A.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "没有结果"))
self.A.setTitle(_translate("MainWindow", "A"))
self.C.setTitle(_translate("MainWindow", "C"))
self.B.setText(_translate("MainWindow", "B"))
self.D.setText(_translate("MainWindow", "D"))
2、逻辑代码
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from untitled import Ui_MainWindow
class MyMainForm(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MyMainForm, self).__init__(parent)
self.setupUi(self)
self.B.triggered.connect(self.select_change2)
self.D.triggered.connect(self.select_change4)
def select_change2(self,):
self.label.setText("2")
def select_change4(self,):
self.label.setText("4")
if __name__ == '__main__':
app = QApplication(sys.argv)
myWin = MyMainForm()
myWin.show()
sys.exit(app.exec_())
3、演示
我们选中,则会触发相应操作
三、相关功能
我们可以通过在界面文件的retranslateUi函数中,添加以下代码增加快捷键
self.B.setShortcut('Alt+Insert')
连接槽函数,我们的动作信号为triggered
self.B.triggered.connect(self.select_change2)