pyqt 菜单栏代码 menuBar

QMainWindow 创建菜单栏

menubar = self.menuBar()
menubar.setNativeMenuBar(False)
fileMenu = menubar.addMenu('File')

# 给menu创建一个Action
exitAction = QAction(QIcon('exit.png'), 'Exit', self)
exitAction.setShortcut('Ctr+Q')
exitAction.setStatusTip('Exit Application')
exitAction.triggered.connect(qApp.quit)
 
# 将这个Action添加到fileMenu上
fileMenu.addAction(exitAction)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PyQt Designer中,可以通过以下步骤来为菜单栏添加触发事件: 1. 在设计界面中选择菜单栏组件,并在属性编辑器中设置菜单栏的名称。 2. 在代码中创建一个继承自QMainWindow的类,并在该类的构造函数中初始化UI。 3. 在initUI()方法中,使用menuBar()方法创建一个菜单栏对象,并使用addMenu()方法添加菜单。 4. 使用addAction()方法为菜单添加动作,并将其与相应的槽函数关联起来。 5. 在槽函数中编写相应的逻辑代码。 以下是一个示例代码,演示了如何在PyQt Designer中为菜单栏添加触发事件: ```python import sys from PyQt5.QtWidgets import QMainWindow, QAction, QApplication class Example(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): menubar = self.menuBar() fileMenu = menubar.addMenu('File') # 添加动作并关联槽函数 newAct = QAction('New', self) newAct.triggered.connect(self.newFile) fileMenu.addAction(newAct) impMenu = fileMenu.addMenu('Import') impAct = QAction('Import mail', self) impAct.triggered.connect(self.importMail) impMenu.addAction(impAct) self.setGeometry(300, 300, 300, 200) self.setWindowTitle('Menu Example') self.show() def newFile(self): # 新建文件的逻辑代码 print("New file created.") def importMail(self): # 导入邮件的逻辑代码 print("Mail imported.") if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) ``` 在上述示例中,我们创建了一个菜单栏,并添加了一个"File"菜单。在"File"菜单下,我们添加了一个"New"动作和一个"Import"子菜单。当用户点击"New"动作时,会触发newFile()槽函数,当用户点击"Import mail"动作时,会触发importMail()槽函数。你可以根据自己的需求在槽函数中编写相应的逻辑代码

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值