Python und QT

Python und das Qt-Toolkit, erstellen von GUIs mittels des Qt-Designers.
Antworten
hans
User
Beiträge: 728
Registriert: Sonntag 22. September 2002, 08:32
Wohnort: Sauerland
Kontaktdaten:

Nach Jahren der Abstinenz von Linux, Python und Co. versuche ich mich wieder an Python Als Entwicklerumgebung nutze ich Kdevelop. Das Ganze läuft auf Fedora23 mit KDE.

Irgendwie habe ich das Gefühl, dass das meine Probleme konfigurationsbedingt sind, denn es ist sowohl Python 2.7 als auch Pathon 3.4 installiert. Beide Versionen wurden mit dnf aus Fedora Packages installiert.

Ich habe einmal versucht, folgenden Code auszuführen (mit beiden pythonversionen)

Code: Alles auswählen

#!/usr/bin/env python3
# -*- coding:utf-8 -*-

import sys

from PyQt4.QtGui import QApplication
from PyQt4.uic import loadUi

def main():
    app = QApplication(sys.argv)
    window = loadUi("hello.ui")  
    window.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
  main()
 
sowohl in Python2.7 als auch Python 3.4 erhalte ich Fehlermeldungen.

Code: Alles auswählen

[nor@localhost hello]$ python hello.py
Traceback (most recent call last):
  File "hello.py", line 22, in <module>
    main()
  File "hello.py", line 15, in main
    window = loadUi("hello.ui")  
  File "/usr/lib64/python2.7/site-packages/PyQt4/uic/__init__.py", line 236, in loadUi
    return DynamicUILoader(package).loadUi(uifile, baseinstance, resource_suffix)
  File "/usr/lib64/python2.7/site-packages/PyQt4/uic/Loader/loader.py", line 71, in loadUi
    return self.parse(filename, resource_suffix, basedir)
  File "/usr/lib64/python2.7/site-packages/PyQt4/uic/uiparser.py", line 974, in parse
    document = parse(filename)
  File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 1182, in parse
    tree.parse(source, parser)
  File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 647, in parse
    source = open(source, "rb")
IOError: [Errno 2] Datei oder Verzeichnis nicht gefunden: 'hello.ui'
[nor@localhost hello]$ python hello.py
Traceback (most recent call last):
  File "hello.py", line 22, in <module>
    main()
  File "hello.py", line 15, in main
    window = loadUi("hello.ui")  
  File "/usr/lib64/python2.7/site-packages/PyQt4/uic/__init__.py", line 236, in loadUi
    return DynamicUILoader(package).loadUi(uifile, baseinstance, resource_suffix)
  File "/usr/lib64/python2.7/site-packages/PyQt4/uic/Loader/loader.py", line 71, in loadUi
    return self.parse(filename, resource_suffix, basedir)
  File "/usr/lib64/python2.7/site-packages/PyQt4/uic/uiparser.py", line 974, in parse
    document = parse(filename)
  File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 1182, in parse
    tree.parse(source, parser)
  File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 647, in parse
    source = open(source, "rb")
IOError: [Errno 2] Datei oder Verzeichnis nicht gefunden: 'hello.ui'
[nor@localhost hello]$ python3 hello.py
Traceback (most recent call last):
  File "hello.py", line 8, in <module>
    from PyQt4.QtGui import QApplication
ImportError: No module named 'PyQt4'
[nor@localhost hello]$
[nor@localhost hello]$ 
[nor@localhost hello]$ 
[nor@localhost hello]$ 
[nor@localhost hello]$ 
[nor@localhost hello]$ 
[nor@localhost hello]$ python3 hello.py
Traceback (most recent call last):
  File "hello.py", line 8, in <module>
    from PyQt4.QtGui import QApplication
ImportError: No module named 'PyQt4'
[nor@localhost hello]$
In der PyQt Dokumentation steht etwas von Konfigurationsdateien. Wo habe ich die zu speichern und vor allem welche Variablen muss ich definieren? Nachfolgend das offizielle Beispiel einer config.py

Code: Alles auswählen

# The target Python installation.
py_platform = linux
py_inc_dir = %(sysroot)/usr/include/python%(py_major).%(py_minor)
py_pylib_dir = %(sysroot)/usr/lib/python%(py_major).%(py_minor)/config
py_pylib_lib = python%(py_major).%(py_minor)mu

# The target PyQt installation.
pyqt_module_dir = %(sysroot)/usr/lib/python%(py_major)/dist-packages
pyqt_bin_dir = %(sysroot)/usr/bin
pyqt_sip_dir = %(sysroot)/usr/share/sip/PyQt4
pyuic_interpreter = /usr/bin/python%(py_major).%(py_minor)
pyqt_disabled_features = PyQt_Desktop_OpenGL PyQt_qreal_double

# Qt configuration common to all versions.
qt_shared = True

[Qt 5.1]
pyqt_modules = QtCore QtDBus QtDesigner QtGui QtHelp QtMultimedia
    QtMultimediaWidgets QtNetwork QtOpenGL QtPrintSupport QtQml QtQuick
    QtSensors QtSerialPort QtSql QtSvg QtTest QtWebKit QtWebKitWidgets
    QtWidgets QtXmlPatterns _QOpenGLFunctions_ES2
Sirius3
User
Beiträge: 17738
Registriert: Sonntag 21. Oktober 2012, 17:20

@hans: beide Fehlermeldungen sind doch eindeutig. Python2 beschwert sich, dass es keine Datei "hello.ui" gibt. Wenn sie doch existiert, liegt sie nicht im aktuellen Verzeichnis.

Für Python3 wurde kein PyQt4 installiert. Das muß man für jede Version separat machen.
hans
User
Beiträge: 728
Registriert: Sonntag 22. September 2002, 08:32
Wohnort: Sauerland
Kontaktdaten:

Danke Sirius3

Da sieht man vor lauter Bäumen den Wals nicht mehr. Da die UIDatei von kdvelop angelegt wurde, heißt die auch nicht helllo.ui. Die heißt in jedem kdevelop Projekt mainwindow.ui.

Für python3 muss ich mal sehen ob dnf was hergibt. Könnte es eventuell auch mit symbolischen Links probieren.
hans
User
Beiträge: 728
Registriert: Sonntag 22. September 2002, 08:32
Wohnort: Sauerland
Kontaktdaten:

Noch ein Nachtrag zu dem Problem in Python3.

Habe die PyQt4 von Python 2.7 nach Python 3.4 verlinkt. Ergebnis:

Code: Alles auswählen

[nor@localhost hello]$ python3 hello.py 
Traceback (most recent call last):
  File "hello.py", line 8, in <module>
    from PyQt4.QtGui import QApplication
ImportError: /usr/lib64/python3.4/site-packages/PyQt4/QtGui.so: undefined symbol: PyString_FromString
[nor@localhost hello]$
Google verweist für PyString_FromString nach https://docs.python.org/2/c-api/string.html
Zitat dort:

Note:
These functions have been renamed to PyBytes_* in Python 3.x. Unless otherwise noted, the PyBytes functions available in 3.x are aliased to their PyString_* equivalents to help porting.

Ist also nichts mit verlinken oder kopieren. Dann bleibt es erst einmal bei Python2.
Benutzeravatar
cofi
Python-Forum Veteran
Beiträge: 4432
Registriert: Sonntag 30. März 2008, 04:16
Wohnort: RGFybXN0YWR0

Sirius3 sagte es doch schon: PyQt muss fuer jede Python-Version extra installiert werden. Hintergrund ist, dass es gegen die spezielle Version gelinkt werden muss, da es eine C-Bibliothek ist.
Und dieses linken meint den Linker, keine symbolischen Links.

Fuer Python3 gibt es AFAIR nur PyQt5 Versionen.
hans
User
Beiträge: 728
Registriert: Sonntag 22. September 2002, 08:32
Wohnort: Sauerland
Kontaktdaten:

Wegen dnf installiere ich ungern aus Sourcecode, weil man kaum eine Übersicht hat, was wohin gepackt wurde. Muss mal sehen, ob checkinstall noch läuft.
Antworten