杰瑞科技汇

Python3.5如何用PythonQt实现GUI开发?

Of course. This is a very specific request, as PythonQt is no longer actively developed, and Python 3.5 is long past its end-of-life. However, it is possible to get them to work together, and I'll guide you through the process, the challenges, and a modern alternative.

Python3.5如何用PythonQt实现GUI开发?-图1
(图片来源网络,侵删)

Executive Summary

Can you use PythonQt with Python 3.5?

  • Yes, but it's not officially supported and requires a specific build.
  • The most reliable version is PythonQt 3.2.
  • You cannot use the newer PythonQt 4.0 with Python 3.5.
  • The process involves downloading a pre-compiled binary for your specific Python 3.5 version and architecture (32-bit or 64-bit) or building it from source, which is complex.

Step-by-Step Guide to Installing PythonQt for Python 3.5

The easiest and most recommended method is to use a pre-compiled wheel (.whl) file.

Step 1: Check Your Python and Qt Versions

This is the most critical step. You need to know the exact version of Python 3.5 you are using and the version of Qt you want to link against.

  1. Check Python Version: Open your command prompt or terminal and run:

    Python3.5如何用PythonQt实现GUI开发?-图2
    (图片来源网络,侵删)
    python --version
    # Or, if you have multiple Python versions:
    python3.5 --version

    You should see something like Python 3.5.9.

  2. Check Python Architecture (32-bit or 64-bit): Run this Python command:

    python -c "import struct; print(struct.calcsize('P') * 8)"
    • If it prints 64, you have a 64-bit Python.
    • If it prints 32, you have a 32-bit Python.
  3. Decide on a Qt Version: PythonQt needs to be built against a specific version of the Qt libraries (e.g., Qt5, Qt4). For Python 3.5, Qt5 is the most common and recommended choice. You will need to have the Qt5 development libraries installed on your system.

Step 2: Download the Correct PythonQt Wheel

You need to find a .whl file that matches your Python version, architecture, and Qt version. The best place to look is the PythonQt Releases page on GitHub or the PyPI archive.

Python3.5如何用PythonQt实现GUI开发?-图3
(图片来源网络,侵删)

A good source for older wheels is Christoph Gohlke's Unofficial Windows Binaries. Note: Use this at your own risk, but it's often the most reliable for older, unsupported packages.

  1. Go to the Unofficial Windows Binaries for Extension Packages page.
  2. Scroll down to the PythonQt section.
  3. Look for a file named like PythonQt3-3.2-cp35-none-win_amd64.whl.
    • PythonQt3-3.2: This is the package name and version (PythonQt 3.2 is the one you want).
    • cp35: This indicates it's for CPython 3.5.
    • none: This means it's not a pure Python package (it has compiled code).
    • win_amd64: This is for 64-bit Windows. Use win32 for 32-bit.

If you are on Linux or macOS, the process is different. You would typically need to build from source or find a package in your distribution's repository (e.g., sudo apt-get install pythonqt on Debian/Ubuntu, but this will likely be for an old Python 2 version).

Step 3: Install the Wheel

  1. Place the downloaded .whl file in a convenient location.
  2. Open your command prompt as an Administrator (on Windows) or use sudo (on Linux/macOS) if necessary.
  3. Navigate to the directory where you saved the file.
  4. Use pip to install it:
    pip install PythonQt3-3.2-cp35-none-win_amd64.whl

    Replace the filename with the exact name of the file you downloaded.

Step 4: Verify the Installation

Create a simple Python script to test if everything is working. This script will create a basic Qt window using PythonQt.

# test_pyqt.py
import sys
import PythonQt
from PythonQt import QtCore, QtGui, QtWidgets
# Create the application
app = QtWidgets.QApplication(sys.argv)
# Create a main window
window = QtWidgets.QMainWindow()
window.setWindowTitle("PythonQt with Python 3.5")
window.setGeometry(100, 100, 400, 300)
# Add a simple label
label = QtWidgets.QLabel("Hello from PythonQt!", parent=window)
label.setAlignment(QtCore.Qt.AlignCenter)
window.setCentralWidget(label)
# Show the window
window.show()
# Start the application's event loop
sys.exit(app.exec_())

Run this script from your terminal:

python test_pyqt.py

If a window appears, you have successfully installed PythonQt for Python 3.5!


Challenges and Important Considerations

  1. End-of-Life Support: Neither Python 3.5 nor PythonQt are maintained. You will not receive security updates or bug fixes.
  2. Qt Version Mismatch: The biggest headache is ensuring the version of Qt that PythonQt was compiled against matches the version of Qt libraries installed on your system. If they don't match, your application will fail to start (often with a "DLL not found" or a silent crash).
  3. No Modern Features: You are missing out on years of Python and Qt improvements, like f-strings, type hints, modern signal/slot syntax, and performance enhancements.
  4. PythonQt 4.0 is Incompatible: Do not try to use PythonQt 4.0. It dropped support for Python 2.x but requires a much newer C++ compiler and is not compatible with the older toolchain used for Python 3.5.

A Modern Alternative: PySide6 / PyQt6

For any new project, you should strongly consider using a modern, actively maintained binding. The most popular choice is PySide6 (the official binding from Qt Company) or PyQt6 (from Riverbank Computing).

Why use PySide6 instead of PythonQt?

  • Actively Maintained: Receives regular updates for new Python and Qt versions.
  • Official Support: Backed by The Qt Company.
  • Rich Ecosystem: Excellent documentation, tutorials, and community support.
  • Modern Python Features: Works seamlessly with Python 3.6+.

Here's how you would write the same "Hello World" app using PySide6:

# test_pyside6.py
import sys
from PySide6 import QtWidgets, QtCore
# Create the application
app = QtWidgets.QApplication(sys.argv)
# Create a main window
window = QtWidgets.QMainWindow()
window.setWindowTitle("PySide6 App")
window.setGeometry(100, 100, 400, 300)
# Add a simple label
label = QtWidgets.QLabel("Hello from PySide6!", parent=window)
label.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
window.setCentralWidget(label)
# Show the window
window.show()
# Start the application's event loop
sys.exit(app.exec())

To install PySide6 for a modern Python (e.g., 3.9+):

pip install PySide6

Conclusion

Feature PythonQt 3.2 (for Python 3.5) PySide6 / PyQt6 (Modern)
Status Legacy, Unmaintained Active, Recommended
Ease of Use Can be tricky to install Very easy (pip install)
Python Support Python 2.7, 3.3-3.7 Python 3.6+
Qt Support Primarily Qt4, Qt5 Qt6 (and Qt5 via PySide2)
Community Very small Large and active
Recommendation Only for maintaining old codebases For all new projects

If you are working on an existing project that requires PythonQt and Python 3.5, the guide above should help you get it running. However, for any new development, please invest the time to learn PySide6 or PyQt6. It will save you immense headaches in the long run.

分享:
扫描分享到社交APP
上一篇
下一篇