Python PyQt菜单的动态填充
 
                    小妮浅浅
2021-10-09 09:58:023449浏览 · 0收藏 · 0评论

为了继续开发示例应用程序,假设您需要在_文件_下创建_打开最近的_子菜单,并动态填写最近打开的文件或文档。因此,需要操作以下步骤:
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学习指路:python基础教程
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。
 
                    关注公众号,随时随地在线学习
相关文章
