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

在開始以前,先要介紹一下個人環境-->ubuntu13.10。其餘的環境將在下面講述如何搭建python

PyQt5的安裝:linux

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

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

#!/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 若是沒報錯說明安裝成功了windows

這些作好後咱們還須要下載並安裝qt-creator,這個去qt-project.org官網下載就行了。安裝這個的目的是使用裏面的qt designer來拖控件以快速完成軟件界面的設計。centos

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

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

可是如今有個問題,那就是若是我想要把這個py分享給朋友,可是他們的電腦上並無安裝pyqt怎麼辦?app

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

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_())
相關文章
相關標籤/搜索