# MenuDemoApplicationQt.py # http://www.naturalprogramming.com # 2009-12-14 File received from Mikko Karjalainen. # 2010-09-23 Many names made more informative. # 2010-09-30 Last modification by Kari Laitinen. # The first PyQt version of this program was developed by # Mikko Karjalainen. # This program represents a traditional GUI (Graphical # User Interface) application that provides menus # for the user to make selections. # In addition to menus, this program shows how # -- a QMessageBox can be activated # -- how to make a Python Qt application to quit itself import sys from PyQt4.QtCore import * from PyQt4.QtGui import * class MenuDemoWindow( QMainWindow ) : def __init__( self, parent = None ) : QMainWindow.__init__( self ) self.setWindowTitle( "Qt GUI Application with Menus" ) self.resize( 600, 500 ) self.last_selected_action = "No Selections Made" self.font_in_use = QFont( "Serif", 14 ) #Create actions for menu file_open_action = QAction( "&Open", self ) file_open_action.setShortcut( "Ctrl+O" ) file_open_action.setStatusTip( "&Open a file" ) self.connect( file_open_action, SIGNAL("triggered()"), self.file_open_action_selected ) file_save_action = QAction("&Save", self) file_save_action.setShortcut("Ctrl+S") file_save_action.setStatusTip("&Save a file") self.connect( file_save_action, SIGNAL( "triggered()" ), self.file_save_action_selected ) exit_action = QAction( "&Quit", self ) exit_action.setShortcut( "Ctrl+Q" ) exit_action.setStatusTip( "Exit application" ) self.connect( exit_action, SIGNAL( "triggered()" ), SLOT( "close()" ) ) copy_action = QAction( "&Copy", self ) self.connect( copy_action, SIGNAL( "triggered()" ), self.copy_action_selected ) paste_action = QAction( "&Paste", self ) self.connect( paste_action, SIGNAL( "triggered()" ), self.paste_action_selected ) self.cyan_background_action = QAction( "&Cyan background", self ) self.cyan_background_action.setCheckable( True ) self.connect( self.cyan_background_action, SIGNAL( "triggered()" ), self.background_color_changed ) self.small_text_action = QAction( "&Small", self ) self.small_text_action.setCheckable( True ) self.medium_text_action = QAction( "&Medium", self ) self.medium_text_action.setCheckable( True ) self.large_text_action = QAction( "&Large", self ) self.large_text_action.setCheckable( True ) self.connect( self.small_text_action, SIGNAL("triggered()"), self.text_size_changed) self.connect( self.medium_text_action, SIGNAL("triggered()"), self.text_size_changed) self.connect( self.large_text_action, SIGNAL("triggered()"), self.text_size_changed) text_size_actionGroup = QActionGroup( self ) text_size_actionGroup.addAction( self.small_text_action ) text_size_actionGroup.addAction( self.medium_text_action ) text_size_actionGroup.addAction( self.large_text_action ) text_size_actionGroup.setExclusive( True ) text_size_menu = QMenu( "&Text size", self ) text_size_menu.addAction( self.small_text_action ) text_size_menu.addAction( self.medium_text_action ) text_size_menu.addAction( self.large_text_action ) self.medium_text_action.setChecked( True ) about_action = QAction( "&About", self ) self.connect( about_action, SIGNAL( "triggered()" ), self.about_action_selected ) # Create a MenuBar for this window menubar_of_this_window = self.menuBar() file_menu = menubar_of_this_window.addMenu( "&File" ) file_menu.addAction( file_open_action ) file_menu.addAction( file_save_action ) file_menu.addAction( exit_action ) edit_menu = menubar_of_this_window.addMenu( "&Edit" ) edit_menu.addAction( copy_action ) edit_menu.addAction( paste_action ) settings_menu = menubar_of_this_window.addMenu( "&Settings" ) settings_menu.addAction( self.cyan_background_action ) settings_menu.addMenu( text_size_menu ) help_menu = menubar_of_this_window.addMenu( "&Help" ) help_menu.addAction( about_action ) def file_open_action_selected( self ) : self.last_selected_action = "Open" self.update() def file_save_action_selected( self ) : self.last_selected_action = "Save" self.update() def copy_action_selected( self ) : self.last_selected_action = "Copy" self.update() def paste_action_selected( self ) : self.last_selected_action = "Paste" self.update() def about_action_selected( self ) : self.last_selected_action = "About" QMessageBox.about( self, "About this application", "This is a simple GUI application" ) self.update() def text_size_changed( self ) : if self.small_text_action.isChecked() : self.font_in_use.setPointSize( 10 ) self.last_selected_action = "Small text" elif self.medium_text_action.isChecked() : self.font_in_use.setPointSize( 14 ) self.last_selected_action = "Medium text" elif self.large_text_action.isChecked() : self.font_in_use.setPointSize( 18 ) self.last_selected_action = "Large text" self.update() def background_color_changed( self ) : self.last_selected_action = "Cyan background" self.update() def paintEvent( self, event ) : painter = QPainter() painter.begin( self ) if self.cyan_background_action.isChecked() : painter.fillRect( 0, 0, self.width(), self.height(), Qt.cyan ) painter.setFont( self.font_in_use ) painter.drawText( 100, 200, "Last selected action \"" + self.last_selected_action + "\"" ) painter.end() this_application = QApplication( sys.argv ) application_window = MenuDemoWindow() application_window.show() this_application.exec_()