[PyQt] PyQt4 for Python 3
Phil Thompson
phil at riverbankcomputing.com
Tue Jun 9 18:54:30 BST 2009
On Sat, 6 Jun 2009 08:36:44 +0100, Mark Summerfield <list at qtrac.plus.com>
wrote:
> Hi,
>
> I've started converting the examples for "Rapid GUI Programming with
> Python and Qt" to Python 3. I've only hit two problems, one of which I
> think is a PyQt bug.
>
> (1) Bug?
> Every .ui file converted by pyuic4 gives me an error message like
> this:
> $ ./imagechanger.pyw
> Traceback (most recent call last):
> File "./imagechanger.pyw", line 264, in fileNew
> dialog = newimagedlg.NewImageDlg(self)
> File "/home/mark/books/rapid-gui-programming-with-python-and-
> qt/eg3/chap06/newimagedlg.py", line 22, in __init__
> self.setupUi(self)
> File "/home/mark/books/rapid-gui-programming-with-python-and-
> qt/eg3/chap06/ui_newimagedlg.py", line 71, in setupUi
> self.retranslateUi(NewImageDlg)
> AttributeError: 'NewImageDlg' object has no attribute 'retranslateUi'
>
> In Python 2, pyuic4 adds the retranslateUi() method to the Ui_*
> class, but this is not happening for Python 3.
>
> Python 3.0.1 Qt 4.5.1 PyQt 4.5 on Linux
I can't reproduce this. Any chance you were hitting the tab order bug and
not noticing and the .py file was truncated?
> (2) Behaviour change.
> QImageReader.supportedImageFormats() and
> QImageWriter.supportedImageFormats() both return a list of
> QByteArrays. With Python 2 you can convert each QByteArray to a
> unicode string like this:
> text = unicode(byte_array)
> If you change this for Python 3 version to:
> text = str(byte_array) # str is Unicode in Python 3
> it will "work", but the string you'll get back will be the repr,
> e.g., "PyQt4.QtCore.QByteArray(b'bmp').
> For Python 3 I'm now doing it like this:
> text = byte_array.data().decode("ascii") # text == "bmp"
> I'm not saying this is the best way, just a way that works.
Given that Python 3 wants you to be explicit about encodings then it
doesn't seem unreasonable that you have to do this.
Happy to debate this as it is the most difficult area of the Python 3
support.
Phil
More information about the PyQt
mailing list