[PyQt] Moving PyQt programs to Python 3

Algis Kabaila akabaila at pcug.org.au
Thu Jul 14 05:26:40 BST 2011


1. Is it possible to detect which Python version PyQt has been built/compiled?  
Just as the version of PyQt can be seen from PYQT_VERSION_STR and Qt from 
QT_VERSION_STR, it would be handy to be able to see version of Python (perhaps 
it is possible  :)

2. I do not know if it is intended to minimise the changes when moving with 
PyQt from Python2.x to Python3.x.  It would be nice if that was the 
case.

The following example shows a file name open dialog that works in PyQt version 
with Python2.x, but fails in PyQt version with Python3.2:

****************************
#!/usr/bin/env python3.2
''' 
openButNotPython3.py - shows a file open dialog that works ok in python2.7
but fails with PyQt4 bound to Python3.2.
Platform - kubuntu 11.04 "natty"
Python 3.2
Qt 4.7.2 (from "natty" binaries.)
PyQt4 4.8.4 - bound to Python 3.2 whilst compiling
'''

import sys
#from PyQt4.QtCore import QMetaObject
from PyQt4.QtGui  import (QDialog, QFileDialog, QApplication, 
                          QGridLayout, QPushButton, QSpacerItem, 
                          QSizePolicy, QPlainTextEdit)

class Dialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setObjectName("Dialog")
        self.resize(300, 200)
        gridLayout = QGridLayout()
        self.pushButton = QPushButton()
        gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
        spacerItem = QSpacerItem(300, 20, QSizePolicy.Expanding, 
QSizePolicy.Minimum)
        gridLayout.addItem(spacerItem, 0, 1, 1, 1)
        self.plainTextEdit = QPlainTextEdit()
        self.plainTextEdit.setObjectName("plainTextEdit")        
        gridLayout.addWidget(self.plainTextEdit, 1, 0, 1, 2)
        self.setWindowTitle("Dialog")
        self.pushButton.setText("Open File")        
        self.pushButton.clicked.connect(self.open)
        self.setLayout(gridLayout)

#
# With Python3.2 this fails with the message:  TypeError
# "QFileDialog.getOpenFileName(QWidget parent=None, str caption='', 
# str directory='', str filter='', QFileDialog.Options options=0): 
# argument 5 has unexpected type 'NoneType'"     
    def open(self):
        name = QFileDialog.getOpenFileName(
                self, "Open File", ".", "Files (*.*)", None,
                QFileDialog.DontUseNativeDialog)
        self.plainTextEdit.appendPlainText(filename)
        
if __name__ == '__main__':
    app = QApplication(sys.argv)
    frame = Dialog()
    frame.show()
    app.exec_()
****************************

Your comments will be appreciated!
OldAl.


More information about the PyQt mailing list