O κώδικας πρέπει να τρέχει τόσο σε PySide όσο και σε PyQt. Απλά αλλάξτε τα imports.
To "ζουμί" είναι στις μεθόδους create_action και add_action. H υλοποίηση τους είναι από
ΕΔΩ
from __future__ import division
from __future__ import print_function
import sys
import PySide.QtCore as QtCore
import PySide.QtGui as QtGui
class MenuExample(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MenuExample, self).__init__(parent)
self.setWindowTitle("PyQt/PySide menus example")
self.statusBar()
self.create_menu()
def on_load_file(self, filename=None):
print("You clicked the filemenu option 'Load file")
def on_new_file(self):
print("You clicked the filemenu option 'New file")
def on_about(self):
msg = """Text appearing in the about dialog of our application."""
QtGui.QMessageBox.about(self, "About the demo", msg.strip())
def create_menu(self):
# Create the menu Bar.
self.menubar = self.menuBar() # Inheriting from QMainWindow
# Add menus to the menu Bar.
self.file_menu = self.menubar.addMenu("&File")
self.edit_menu = self.menubar.addMenu("&Edit")
self.help_menu = self.menubar.addMenu("&Help")
# Create File menu's actions
new_action = self.create_action("&New file",
shortcut = "Ctrl+N",
slot = self.on_new_file,
tip = "Create a new file")
load_action = self.create_action("&Load file",
shortcut="Ctrl+L",
slot=self.on_load_file,
tip="Load a file")
quit_action = self.create_action("&Quit",
shortcut="Ctrl+Q",
slot=self.close,
tip="Close the application")
# Add file menu's actions to the menu
self.add_actions(self.file_menu,
new_action, load_action, None, quit_action)
#Create Help Menu's actions.
about_action = self.create_action("&About",
shortcut='F1',
slot=self.on_about,
tip='About the demo')
# Add Help menu's actions to the menu
self.add_actions(self.help_menu, about_action)
def add_actions(self, target, *actions):
for action in actions:
if action is None:
target.addSeparator()
else:
target.addAction(action)
def create_action(self, text, slot=None, shortcut=None, icon=None, tip=None,
checkable=False, signal="triggered()"):
action = QtGui.QAction(text, self)
if icon is not None:
action.setIcon(QIcon(":/%s.png" % icon))
if shortcut is not None:
action.setShortcut(shortcut)
if tip is not None:
action.setToolTip(tip)
action.setStatusTip(tip)
if slot is not None:
self.connect(action, QtCore.SIGNAL(signal), slot)
if checkable:
action.setCheckable(True)
return action
def main():
app = QtGui.QApplication(sys.argv)
form = MenuExample()
form.show()
sys.exit(app.exec_())
if __name__=='__main__':
main()