ITPub博客

首页 > IT职业 > IT生活 > Python PyQt菜单的动态填充

Python PyQt菜单的动态填充

IT生活 作者:bigstarr 时间:2021-09-11 09:46:15 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基础教程

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3330/viewspace-2828023/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-14

  • 博文量
    83
  • 访问量
    1108