Advertisement
bojjenclon

PyQt4 Picture Viewer

Nov 21st, 2011
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. import sys
  2. from PyQt4 import QtGui
  3.  
  4. class PictureViewer( QtGui.QMainWindow ):
  5.     def __init__( self ):
  6.         super( PictureViewer, self ).__init__()
  7.        
  8.         self.initUI()
  9.        
  10.     def initUI( self ):
  11.         openAction = QtGui.QAction( '&Open', self )
  12.         openAction.setShortcut( 'Ctrl+O' )
  13.         openAction.triggered.connect( self.onOpen )
  14.        
  15.         menubar = self.menuBar()
  16.         fileMenu = menubar.addMenu( '&File' )
  17.         fileMenu.addAction( openAction )
  18.        
  19.         main = QtGui.QWidget( self )
  20.        
  21.         self.pic = QtGui.QLabel()
  22.        
  23.         scrollArea = QtGui.QScrollArea()
  24.         scrollArea.setWidgetResizable( True )
  25.         scrollArea.setWidget( self.pic )
  26.        
  27.         hbox = QtGui.QHBoxLayout()
  28.        
  29.         hbox.addWidget( scrollArea )
  30.        
  31.         main.setLayout( hbox )
  32.        
  33.         self.setCentralWidget( main )
  34.        
  35.         self.resize( 640, 480 )
  36.         self.center()
  37.        
  38.         self.setWindowTitle( 'Picture Viewer' )    
  39.         self.show()
  40.  
  41.     def center( self ):
  42.         qr = self.frameGeometry()
  43.         cp = QtGui.QDesktopWidget().availableGeometry().center()
  44.         qr.moveCenter( cp )
  45.         self.move( qr.topLeft() )
  46.  
  47.     def onOpen( self ):
  48.         fname = QtGui.QFileDialog.getOpenFileName( self, 'Open image', '', 'Images (*.png *.jpg *.bmp)' )
  49.        
  50.         if( fname != None and fname != "" ):
  51.             pixmap = QtGui.QPixmap( fname )
  52.             self.pic.setPixmap( pixmap )
  53.  
  54. def main():
  55.     app = QtGui.QApplication( sys.argv )
  56.     pv = PictureViewer()
  57.     sys.exit( app.exec_() )
  58.  
  59. if __name__ == '__main__':
  60.     main()
  61.  
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement