为了继续开发示例应用程序,假设您需要在_文件_下创建_打开最近的_子菜单,并动态填写最近打开的文件或文档。因此,需要操作以下步骤:
1、在_File_下创建_Open最近的_子菜单。
2、编写动态生成操作,填写菜单的定制插槽。
3、连接.aboutToShow()菜单信号和自定义插槽。
实例
from functools import partial # Snip... class Window(QMainWindow): # Snip... def populateOpenRecent(self): # Step 1. Remove the old options from the menu self.openRecentMenu.clear() # Step 2. Dynamically create the actions actions = [] filenames = [f"File-{n}" for n in range(5)] for filename in filenames: action = QAction(filename, self) action.triggered.connect(partial(self.openRecentFile, filename)) actions.append(action) # Step 3. Add the actions to the menu self.openRecentMenu.addActions(actions)
以上就是Python PyQt菜单的动态填充的方法,希望对大家有所帮助。更多Python学习指路:
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。
Copyright © 2019- net188.cn 版权所有 湘ICP备2022005869号-2
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务