ubuntu下搭建python2.7+PyQt5並實現一個小詞典


在開始之前,先要介紹一下我的環境-->ubuntu13.10。其他的環境將在下面講述如何搭建

PyQt5的安裝:

    在pyqt的官網上二進制包只有windows的,別的系統需要自己手動編譯pyqt的源代碼。我看了一下windows下pyqt5的安裝包的列表,發現只有PyQt5-5.1.1-gpl-Py3.3-Qt5.1.1*,讓我誤以為pyqt5只有python3的版本,后來才發現其實不是這樣的= = 

    在官網下載好源代碼后,安裝編譯代碼過程比較繁瑣,主要是會遇上一些依賴的問題。我直接把整個下載,編譯,安裝過程寫成一個腳本了,只要在終端執行這個腳本就會完成PyQt的安裝工作。腳本我沒測試過,大家謹慎使用:

#!/bin/sh
#This bash script is created by kongkongyzt and you can contact with me 
#when you have trouble running.E-mail:kongkongyzt@gmail.com
#                                                         Nov 29 2013

echo "###############################################################"
echo "Start the installation of sip,which is an import tool to call "
echo "gcc compile source code of pyqt into binary code"
echo "###############################################################"
wget http://sourceforge.net/projects/pyqt/files/sip/sip-4.15.3/sip-4.15.3.tar.gz
tar -xzf sip-4.15.3.tar.gz
cd sip-4.15.3&&python configure.py --platform linux-g++&&make&&sudo make install
echo "###############################################################"
echo "Start the installation of pyqt "
echo "It will take some time because we need to compile the souece code"
echo "###############################################################"
wget http://sourceforge.net/projects/pyqt/files/PyQt5/PyQt-5.1.1/PyQt-gpl-5.1.1.tar.gz
tar -xzf PyQt-gpl-5.1.1.tar.gz
#This step is import or you will recive error on include<python.h>, details can be found here http://stackoverflow.com/questions/17698877/fatal-error-while-compiling-pyqt5-python-h-does-not-exist
sudo ln -s /usr/include/python2.7 /usr/local/include/python2.7
cd PyQt-gpl-5.1.1&& python configure.py&&make&&make install
echo "###############################################################"
echo "All the installation has done! please check your installation"
echo "###############################################################"

之后打開python shell 試試這句會不會報錯:import PyQt5 如果沒報錯說明安裝成功了

這些做好后我們還需要下載並安裝qt-creator,這個去qt-project.org官網下載就好了。安裝這個的目的是使用里面的qt designer來拖控件以快速完成軟件界面的設計。

安裝完qt-creator后打開並新建一個項目,然后雙擊里面的ui文件,就會進入到一個可以拖拉控件的模式了,把控件拖拉成合適的樣子,保存退出,可以在項目的文件夾下找到這個ui文件,打開后你可以發現其實這是一個xml文件,接下來,我們要把這個xml文件轉換成python代碼,過程很簡單,終端輸入命令 pyuic5 -x -o XXX.py xx.ui,其中xx分別代表你想生成的py文件的名字和原來ui文件的名字

命令執行后你會發現在當前目錄下生成了一個XX.py文件,試試python xx.py運行這個python文件,哇,你之前的設計就變成了真真實實的python實現的代碼了

但是現在有個問題,那就是如果我想要把這個py分享給朋友,但是他們的電腦上並沒有安裝pyqt怎么辦?

很簡單,在ubuntu的軟件中心搜索cxfreeze並安裝

Linux比windows好的地方之一就是有一個強大的包管理器,一般的開發用的軟件包可以在源里面輕松獲得和安裝,這也是我選擇Linux的眾多原因之一。cxfreeze的官方只有centos的rpm包,所以要不你就下載源代碼編譯安裝,要不你就直接在ubuntu的軟件源里面安裝吧~我想絕大多數人都會選后者的

cxfreeze安裝完成后,執行cxfreeze xx.py 會在當前目錄下生成一個dist文件夾,里面有一個二進制文件,點擊這個文件就可以看到剛才的界面彈出來了,這說明你可以將這個文件夾發送給你的朋友,和你的朋友分享你寫的這個軟件,即使TA電腦上沒有裝pyqt

可能你會發現你雙擊這個二進制文件並沒有反應,沒關系,在你的py文件的前面寫上這么一句 import sip,重新執行cxfreeze xx.py就好了,這下你再點擊這個新生成的二進制文件,應該就沒有問題了

下面分享我自己用PyQt5+python2.7寫的一個有道翻譯的小軟件:

# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
import urllib2
import xml.etree.cElementTree as ET
import sip

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(400, 300)
        self.centralWidget = QtWidgets.QWidget(MainWindow)
        self.centralWidget.setObjectName("centralWidget")
        self.pushButton = QtWidgets.QPushButton(self.centralWidget)
        self.pushButton.setGeometry(QtCore.QRect(70, 210, 80, 23))
        self.pushButton.setObjectName("pushButton")
        self.textEdit = QtWidgets.QTextEdit(self.centralWidget)
        self.textEdit.setGeometry(QtCore.QRect(20, 40, 351, 161))
        self.textEdit.setObjectName("textEdit")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralWidget)
        self.pushButton_2.setGeometry(QtCore.QRect(220, 210, 80, 23))
        self.pushButton_2.setObjectName("pushButton_2")
        self.lineEdit = QtWidgets.QLineEdit(self.centralWidget)
        self.lineEdit.setGeometry(QtCore.QRect(80, 10, 291, 23))
        self.lineEdit.setObjectName("lineEdit")
        self.label = QtWidgets.QLabel(self.centralWidget)
        self.label.setGeometry(QtCore.QRect(20, 10, 51, 21))
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralWidget)
        self.menuBar = QtWidgets.QMenuBar(MainWindow)
        self.menuBar.setGeometry(QtCore.QRect(0, 0, 400, 21))
        self.menuBar.setObjectName("menuBar")
        MainWindow.setMenuBar(self.menuBar)
        self.mainToolBar = QtWidgets.QToolBar(MainWindow)
        self.mainToolBar.setObjectName("mainToolBar")
        MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
        self.statusBar = QtWidgets.QStatusBar(MainWindow)
        self.statusBar.setObjectName("statusBar")
        MainWindow.setStatusBar(self.statusBar)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "有道小詞典"))
        self.pushButton.setText(_translate("MainWindow", "查詢"))
        self.pushButton_2.setText(_translate("MainWindow", "退出"))
        self.label.setText(_translate("MainWindow", "單詞:"))
    
    def get_result(self):
        url='http://fanyi.youdao.com/openapi.do?keyfrom=isailfish&key=1053023538&type=data&doctype=xml&version=1.1&q='+urllib2.quote(self.lineEdit.text().encode('utf8'))
        data=urllib2.urlopen(url).read()
        root=ET.fromstring(data)
        result1=root[3][1]
        da=[]
        for i in result1:
            da.append(i.text)
        foo='\n\n'.join(da)
        self.textEdit.setText('\n'+foo)
    
    def exit(self):
        exit()
    
    def bindevent(self):
        self.pushButton.clicked.connect(self.get_result)
        self.pushButton_2.clicked.connect(self.exit)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    ui.bindevent()
    MainWindow.show()
    sys.exit(app.exec_())

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM