歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Ubuntu 14.04 LTS 搭建 Python2.7+Qt 5.5+PyQt5開發環境

Ubuntu 14.04 LTS 搭建 Python2.7+Qt 5.5+PyQt5開發環境

日期:2017/2/28 13:44:39   编辑:Linux教程

Python有許許多多的GUI開發包,PyQt正式其中之一。那麼什麼是PyQt呢,PyQt是基於Qt框架開發的一個用於創建GUI程序的工具包。它將python語言和Qt庫成功融合在了一起。PyQt有數量龐大的類、函數和方法。它是一個跨平台的工具包,目前PyQt官網提供PyQt4和 PyQt5兩種不同版本的文檔說明。

說到Qt,就必須解釋一下什麼是Qt,Qt是一個跨平台的C++圖形用戶界面應用開發程序,它既可以用於開發GUI程序,也可以用於開發非GUI程序。更多信息請到Qt官網去了解。

下面來記錄一下我搭建python2.7 + Qt5.5 + PyQt5開發環境的過程,作為備忘。

Ubuntu 14.04LTS下默認是安裝了python2.76和python3.4的,並且默認的python運行版本是2.76,我們可以在命令行下輸入python命令來查看。


如果你常在命令行下鍵入python命令的話,建議安裝Ipython或者Bpython
或者可以直接在命令行下輸入 python -V 查看。
PyQt雖然是基於Qt開發的工具包,但是並不包含Qt,所以要安裝PyQt必須先安裝Qt。
Qt的安裝需要滿足一些基本的環境,在ubuntu下可以使用apt-get來配置基本的環境

sudo apt-get install build-essential libgl1-mesa-dev

滿足以上條件之後就可以經行Qt的下載安裝了。
在Qt下載頁面選擇適合自己平台的版本,Qt有多種安裝方式,在線安裝器,線下安裝器,源碼編譯安裝,Qt creator的安裝,我選擇的是Qt5.5.1的在線安裝器。
下載Qt5.5.1在線安裝器qt-unified-Linux-x64-2.0.2-2-online .run文件到電腦中,此時文件並沒有可執行權限,需要使用chmod命令來將x權限添加給該文件。

$chmod a+x /path/to/qt-unified-linux-x64-2.0.2-2-online.run

然後就可以在命令行下運行該文件進行安裝,安裝不需要sudo也可。

$bash /path/to/qt-unified-linux-x64-2.0.2-2-online.run

接下來就是類似與Windows下的圖形安裝過程,在這裡就不做截圖解釋了。
使用在線安裝器安裝完畢之後Qt Creator也被安裝在電腦中。

安裝好Qt之後,下一步要安裝的是SIP,SIP也是安裝PyQt的必備組件,我安裝的是sip-4.17,下載地址https://www.riverbankcomputing.com/software/sip/download 。將下載好的sip的壓縮包解壓到你的電腦裡,打開解壓後的文件,你會在裡邊發現一個doc 文件夾,這個文件夾就是sip文檔,裡邊有詳細的不同平台下的安裝教程,ubuntu14.04LTS下,只需要簡單的三個步驟。

$python configure.py
$make
$sudo make install

安裝完SIP之後就是PyQt的安裝了,同樣下載PyQt5安裝包,下載地址https://www.riverbankcomputing.com/software/pyqt/download5
下載之後與sip一樣,解壓後在解壓包中尋找doc文檔,裡邊有詳細的安裝教程,類似的三個命令

$python configure.py --qmake /path/to/you/Qt/5.5/gcc_64/bin/qmake
$make
$sudo make install

我在運行第一條命令的時候遇到了一個頭文件的缺失

error: 'qgeolocation.h' file not found
     #include <qgeolocation.h>
               ^
      1 error generated.
      make[1]: *** [sipQtPositioningcmodule.o] Error 1
      make: *** [sub-QtPositioning-make_first-ordered] Error 2

stackoverflow之後得以解決。問題鏈接
解決方案是在/PyQt-gpl-5.5.1/QtPositioning下創建一個qgeolocation.h 文件,並將以下代碼拷貝進去,保存,重新運行即可。

/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtPositioning module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QGEOLOCATION_H
#define QGEOLOCATION_H

#include <QtCore/QSharedDataPointer>
#include <QtCore/QMetaType>
#include <QtPositioning/qpositioningglobal.h>

QT_BEGIN_NAMESPACE

class QGeoAddress;
class QGeoCoordinate;
class QGeoRectangle;
class QGeoLocationPrivate;

class Q_POSITIONING_EXPORT QGeoLocation
{
public:
    QGeoLocation();
    QGeoLocation(const QGeoLocation &other);

    ~QGeoLocation();

    QGeoLocation &operator=(const QGeoLocation &other);

    bool operator==(const QGeoLocation &other) const;
    bool operator!=(const QGeoLocation &other) const {
        return !(other == *this);
    }

    QGeoAddress address() const;
    void setAddress(const QGeoAddress &address);
    QGeoCoordinate coordinate() const;
    void setCoordinate(const QGeoCoordinate &position);
    QGeoRectangle boundingBox() const;
    void setBoundingBox(const QGeoRectangle &box);

    bool isEmpty() const;

private:
    QSharedDataPointer<QGeoLocationPrivate> d;
};

Q_DECLARE_TYPEINFO(QGeoLocation, Q_MOVABLE_TYPE);

QT_END_NAMESPACE

Q_DECLARE_METATYPE(QGeoLocation)

#endif

以上是PyQt5開發環境配置詳細過程。

Mac OS X 10.11.1下搭建Python3.4 + PyQt5.5.1 +Eric6.1.1開發平台 http://www.linuxidc.com/Linux/2016-01/127677.htm

CentOS 7 下搭建Python2.7 + PyQt4.11.3 + Eric4.0開發平台 http://www.linuxidc.com/Linux/2016-07/133470.htm

Ubuntu 14.04下搭建Python3.4 + PyQt5.3.2 + Eric6.0開發平台 http://www.linuxidc.com/Linux/2015-12/126176.htm

PyQt 的詳細介紹:請點這裡
PyQt 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved