How to realize the dynamic filling function of PyQt menu in Python
This article is about how to implement the dynamic filling function of PyQt menus in Python. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
To continue developing the sample application, suppose you need to create _ open the nearest _ submenu under _ file _ and dynamically fill in the recently opened file or document. Therefore, you need to do the following steps:
1. Create the most recent submenu of _ Open under _ File_.
2. Write the dynamic generation operation and fill in the custom slot of the menu.
3. Connect the .aboutToShow () menu signal and the custom slot.
Example
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) Thank you for reading! This is the end of this article on "how to achieve the dynamic filling function of the PyQt menu in Python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!