⑴ 怎麼利用python代碼繪制k線圖
現在幾乎所有的大網站都在主要的欄目 做了防爬行的處理。 象這樣的還算是簡單的。 大不了你分析一下JS。 如果不想分析JS。就麻煩 些。 你安裝一個pyqt,裡面有一個qtbrowser, 你可以驅動這個瀏覽器去爬行。要幾百行代碼才能搞定。
⑵ 誰會在linux下編譯安裝pyqt4
兩種方法。
第一種: 你已安裝setuptools
然後easy_install pyqt4
第二種:下載pyqt4 然後解壓,進入pyqt4的目錄 python setup.py install
⑶ 學python可以做什麼
從入門級選手到專業級選手都在做的——爬蟲
用 Python 寫爬蟲的教程網上一抓一大把,據我所知很多初學 Python 的人都是使用它編寫爬蟲程序。小到抓取一個小黃圖網站,大到一個互聯網公司的商業應用。通過 Python 入門爬蟲比較簡單易學,不需要在一開始掌握太多太基礎太底層的知識就可以很快上手,而且很快可以做出成果,非常適合小白一開始想做出點看得見的東西的成就感。
除了入門,爬蟲也被廣泛應用到一些需要數據的公司、平台和組織,通過抓取互聯網上的公開數據,來實現一些商業價值是非常常見的做法。當然這些選手的爬蟲就要厲害的多了,需要處理包括路由、存儲、分布式計算等很多問題,與小白的抓黃圖小程序,復雜度差了很多倍。
Web 程序
除了爬蟲,Python 也廣泛應用到了 Web 端程序,比如你現在正在使用的知乎,主站後台就是基於 Python 的 tornado 框架,豆瓣的後台也是基於 Python。除了 tornado (Tornado Web Server),Python 常用的 Web 框架還有 Flask(Welcome | Flask (A Python Microframework)),Django (The Web framework for perfectionists with deadlines) 等等。通過上述框架,你可以很方便實現一個 Web 程序,比如我認識的一些朋友,就通過 Python 自己編寫了自己的博客程序,包括之前的 hu.photo,我就是通過 Flask 實現的後台(出於版權等原因,我已經停掉了這個網站)。除了上述框架,你也可以嘗試自己實現一個 Web 框架。
桌面程序
Python 也有很多 UI 庫,你可以很方便地完成一個 GUI 程序(話說我最開始接觸編程的時候,就覺得寫 GUI 好炫酷,不過搞了好久才在 VC6 搞出一個小程序,後來又輾轉 Delphi、Java等,最後接觸到 Python 的時候,我對 GUI 已經不感興趣了)。Python 實現 GUI 的實例也不少,包括大名鼎鼎的 Dropbox,就是 Python 實現的伺服器端和客戶端程序。
人工智慧(AI)與機器學習
人工智慧是現在非常火的一個方向,AI熱潮讓Python語言的未來充滿了無限的潛力。現在釋放出來的幾個非常有影響力的AI框架,大多是Python的實現,為什麼呢?因為Python足夠動態、具有足夠性能,這是AI技術所需要的技術特點。比如基於Python的深度學習庫、深度學習方向、機器學習方向、自然語言處理方向的一些網站基本都是通過Python來實現的。
機器學習,尤其是現在火爆的深度學習,其工具框架大都提供了Python介面。Python在科學計算領域一直有著較好的聲譽,其簡潔清晰的語法以及豐富的計算工具,深受此領域開發者喜愛。
早在深度學習以及Tensorflow等框架流行之前,Python中即有scikit-learn,能夠很方便地完成幾乎所有機器學習模型,從經典數據集下載到構建模型只需要簡單的幾行代碼。配合Pandas、matplotlib等工具,能很簡單地進行調整。
而Tensorflow、PyTorch、MXNet、Keras等深度學習框架更是極大地拓展了機器學習的可能。使用Keras編寫一個手寫數字識別的深度學習網路僅僅需要寥寥數十行代碼,即可藉助底層實現,方便地調用包括GPU在內的大量資源完成工作。
值得一提的是,無論什麼框架,Python只是作為前端描述用的語言,實際計算則是通過底層的C/C++實現。由於Python能很方便地引入和使用C/C++項目和庫,從而實現功能和性能上的擴展,這樣的大規模計算中,讓開發者更關注邏輯於數據本身,而從內存分配等繁雜工作中解放出來,是Python被廣泛應用到機器學習領域的重要原因。
科學計算
Python 的開發效率很高,性能要求較高的模塊可以用 C 改寫,Python 調用。同時,Python 可以更高層次的抽象問題,所以在科學計算領域也非常熱門。包括 scipy、numpy 等用於科學計算的第三方庫的出現,更是方便了又一定數學基礎,但是計算機基礎一般的朋友。
⑷ mac pyqt5和pyqt4可以同時安裝嗎
安裝PtQt4前你需准備:
Xcode (10.6.1 自帶Xcode)
Qt - 我使用的是qt-mac-cocoa-opensource-4.6.2.dmg
SIP - 我使用的是sip-4.12.1.tar.gz
PyQt4 - 我使用的是PyQt-mac-gpl-snapshot-4.8.4.tar.gz
創建了一個pyqt文件夾在硬碟根目錄下,並將sip-4.12.1.tar.gz和PyQt-mac-gpl-snapshot-4.8.4.tar.gz
放到其中並解壓。
1。先安裝qt,默認安裝,這一步大部分人不會有問題,跳過。
2。接下來,打開終端(Terminal,在 Applications(應用程序)/Utilities(實用工具)/ 下),在終端輸入:export VERSIONER_PYTHON_PREFER_32_BIT=yes。
3.
使用"cd 路徑"命令將當前目錄設為sip-4.12.1,如: cd /pyqt/sip-4.12.1/, 然後進行配置(configure)【Re-build SIP in 32bit 】在終端中執行
python configure.py --arch i386
配置(configure)完成後就開始編譯,在終端中執行引用make clean(清除以前的安裝信息)make
編譯完成後就安裝,在終端中執行引用sudo make install
在這個過程中沒出現錯誤就是安裝成功了
編譯安裝PyQt4,過程和上面的一樣。 【Build PyQt4 in 32bit 】
在終端中使用"cd 路徑"命令將當前目錄設為PyQt-mac-gpl-snapshot-4.8.4,如
引用cd /Users/schi/pyqt/PyQt-mac-gpl-snapshot-4.8.4
然後進行配置(configure),在終端中執行引用python configure.py --use-arch i386
配置時會遇到引用Determining the layout of your Qt installation...
This is the GPL version of PyQt 4.6 (licensed under the GNU General Public
License) for Python 2.6.2 on darwin.
Type '2' to view the GPL v2 license.
Type '3' to view the GPL v3 license.
Type 'yes' to accept the terms of the license.
Type 'no' to decline the terms of the license.
Do you accept the terms of the license?
輸入yes,再按回車就行
配置(configure)完成後就開始編譯,在終端中執行
引用make cleanmake
編譯比較花時間,請耐心等待。
編譯完成後就安裝,在終端中執行
引用sudo make install
在這個過程中沒出現錯誤就是安裝成功了
也可以在IDLE中導入PyQt4的模塊,能成功導入就說明安裝成功
Python代碼
from PyQt4 import QtCore, QtGui
下面分析一下安裝過程中可能遇到的問題:
1.問題如下
##############
ld: warning: in /Library/Frameworks/Python.framework/Python, missing required architecture x86_64 in file
Undefined symbols:
"_Py_Initialize", referenced from:
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
"_PyLong_AsVoidPtr", referenced from:
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
"_PyErr_Print", referenced from:
PyCustomWidgets::getMoleAttr(char const*, char const*)in pluginloader.o
PyCustomWidgets::getMoleAttr(char const*, char const*)in pluginloader.o
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
"_PyString_FromString", referenced from:
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
"_PyType_IsSubtype", referenced from:
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
"_PyMole_GetDict", referenced from:
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
"_PyObject_CallObject", referenced from:
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
"_PyObject_CallFunctionObjArgs", referenced from:
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
"_PyList_Append", referenced from:
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
"_Py_IsInitialized", referenced from:
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
"_PyDict_Next", referenced from:
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
"_PyImport_ImportMole", referenced from:
PyCustomWidgets::getMoleAttr(char const*, char const*)in pluginloader.o
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
"_PyObject_GetAttrString", referenced from:
PyCustomWidgets::getMoleAttr(char const*, char const*)in pluginloader.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make[1]: *** [libpythonplugin.dylib] Error 1
make: *** [all] Error 2
這個問題的原因就是:qt是在32位mac上編譯的,但pyqt默認是在64位機子上編譯的,所以我們在編譯的時候要制定編譯環境:是i386 還是x86_64.
請參考如下的信息:
The problem is that the Qt library is 32bit while, by default, python builds PyQt4 in 64bit. That caused problem when it tried to link 64bit object files with 32bit Qt library. To fix this issue, we need to force python to build PyQt4 in 32bit, and we also need to re-build SIP in 32bit mode. If you use the python version that comes with Mac OS (2.5 for Snow Leopard), make sure that it runs in 32bit mode by issuing the command
2.個問題如下:
>>> from PyQt4 import QtGui
Traceback (most recent call last):
File "<stdin>", line 1, in <mole>
ImportError: dlopen(/Library/Python/2.6/site-packages/PyQt4/QtGui.so, 2): Symbol not found: __
Referenced from: /Library/Python/2.6/site-packages/PyQt4/QtGui.so
Expected in: flat namespace
in /Library/Python/2.6/site-packages/PyQt4/QtGui.so
這個問題是所有的包安裝都沒有問題但是當導入QtGui或其他包時,總報這個問題,這個問題的原因是,當前pyqt release的包本身帶的一個bug,PyQt-mac-gpl-snapshot-4.8.3, 所以你下載下個要release的包就可以了。如我下的是:PyQt-mac-gpl-snapshot-4.8.4。
下面給大家介紹一下我寫的自動安裝這些程序的python源碼:
#!/usr/bin/env python
"""
Automated installer for Python 2.6 (final)
This installer is able to handle any previous
versions of Python.
NOTE: This file *must* be copied to
//nssgsvr/tools/pyInstall_OSX when modified!
"""
import os
import sys
import re
import shutil
import platform
import tarfile
# Constants
PYTHON_PKGS = {"dmgFile" : "python-2.6.1-macosx2008-12-06.dmg",
"mpkgList": ["MacPython.mpkg"] }
#WXPYTHON_PKGS = {"dmgFile" : "wxPython2.8-osx-unicode-2.8.9.2-universal-py2.6.dmg",
# "mpkgList": ["wxPython2.8-osx-unicode-universal-py2.6.pkg"] }
#ANGELIA
XCODE_PKGS = {"dmgFile" : None, "mpkgList": ["Xcode.mpkg"]}
QT_PKGS = {"dmgFile" : "qt-mac-cocoa-opensource-4.6.2.dmg",
"mpkgList": ["Qt.mpkg"] }
THIRD_PARTY_INSTALL = ["sip-4.12.1", "PyQt-mac-gpl-snapshot-4.8.4","Pyro-3.9.1", "pyserial-2.4"]
# Environment constants
TCSH_ENV_FILE = '/etc/csh.login'
BASH_ENV_FILE = '/etc/profile'
SCRIPTS_ROOT_ENV = "TEST_SCRIPTS_ROOT"
TARGET_VER = "2.6"
IS_POSIX = (os.name == 'posix')
IS_MAC = (sys.version.find('Apple') != -1)
IS_OSX = (IS_MAC and IS_POSIX)
IS_OSX_LEOPARD = (IS_OSX and platform.mac_ver()[0].startswith('10.5'))
IS_OSX_SNOWLEOPARD = (IS_OSX and platform.mac_ver()[0].startswith('10.6'))
# NSSGSVR Constants
MOUNT_POINT = "/Volumes/Test"
HOSTSERVER = "NSSGSVR.global.avidww.com" # Server that contains SQA scripts
SHARE = "TEST" # NSSGSVR share that contains the Python scripts directory
USER = "qatest"
PASSWD = "Cmqat/$/$/$" # slashes needed to escape special character '$'
⑸ Quant 應該學習哪些 Python 知識
1. 如果還需要Deep Learning方面的東西的話,可以考慮Theano或者Keras。這兩個東西可能會用在分析新聞數據方面。不過不是很推薦使用這類方法去做量化模型,因為計算量實在是太大,成本很高。
2. 交易框架方面,除了vn.py,還推薦PyAlgoTrade框架,github上可以搜到。私以為這個框架比vn.py牛逼太多了,畢竟是一個在金融IT領域混跡近20年的老妖的作品,架構設計不是一般的優秀。
3. 國內的話,ricequant是個不錯的選擇,雖然使用的是Java,但是團隊我見過,都是做金融IT出身的,基本上都有7、8年以上經驗,底層功底非常扎實,做事情都很靠譜。現在他們也在考慮把SDK擴展到Python這邊。
4. 國內的行情和交易介面,使用的是自己的協議(比如CTP介面使用的是FTD協議),而不是國際上廣泛使用的FIX協議,並且都不開源。如果需要連接行情,還需要考慮將介面SDK為python封裝一下。(修改:評論中有人提到很多券商也開放了FIX介面,不過似乎是在內網使用)
5. 有人談到資料庫了,這里我也說一下,對於高頻tick級別的數據,其量級可以達到每天TB級別,普通的關系資料庫是扛不住的。如果試圖使用傳統的關系資料庫,比如Oracle之類的可以省省了。對付這種級別的數據,採用文件系統+內存索引會更好。不過這種場景,一般也就是機構裡面能碰到了,個人quant可以不用考慮。
⑹ Python培訓哪裡最好
相信在IT領域發展的同學對Java很熟悉。Python編程語言排行中一直處於領先地位,這可以直接體現Python的重要。因此很多同學准備參加Python培訓機構系統學習。那麼,Python培訓機構哪家比較好?下面我們介紹一下。
隨著Python普及,越來越多的人了解py,企業也會對求職者提出更高的要求,他們想招聘一些能馬上開始工作的人,所以往往會招聘一些有項目開發經驗的人。這就是為什麼那麼多計算機專業的大學生找不到工作,所以越來越多的大學生會選擇在畢業前後參加一些專業的Python培訓課程,以增加他們的實踐經驗。只有增強自己的力量,才能立於不敗之地。
Python培訓機構哪家比較好?判斷Python培訓機構好與壞主要看以下幾個方面
1.看教學課程內容
學習Java技術,最主要是與時俱進,掌握的技術點能夠滿足時下企業的用人需求。而想要了解一家培訓機構所提供的課程是否新穎,也可以去機構的官網上看看,了解自己想學習的學科的課程大綱。看看學習路線圖是如何安排的,有沒有從零到一的系統搭建,是不是有強化實訓、實操的比重,有盡量多的項目實戰。因為企業對Java從業者的技術能力和動手實戰能力要求較高。
2.看師資力量
因為Java開發技術知識的專業性很強,如果盲目去學很容易走進誤區。相反,有講師帶領,站在巨人的肩膀上,往往事半功倍。畢竟現在這個時代只要多跟別人交流才能獲得更多更有價值的信息,初學者千萬不能閉門造車。
3.看口碑
行業內口碑比較好,學生對培訓機構比較認可,這種機構把精力放在了學生身上的機構,才是做教育的應有態度。
4.看就業情況
以學生就業為目標的培訓機構現在才是最主要的。要知道就業也是教學成果的體現,沒有好的教學保證是做不到好的就業的。
5.上門免費試聽
試聽是為了更好的去感受培訓機構的課程內容、講課風格、班級氛圍等,同時也能通過和班上在讀同學進行交流,更進一步去了解這家培訓機構各個方面是否符合自己的需要。
⑺ Python String和PyQt QString的區別
以下在python2.5和PyQt4.4.6 for python2.5環境下討論。
在python中有兩種與字元有關的類型:string object和Unicode object。
平時進行輸入輸出的一般都用string
object,當需要顯示一些特殊字元或者中文等文字時候,需要轉換為Unicode編碼。在PyQt中也有兩種字元類型與上面兩者對應:QByteArray和QString,主要是使用QString操作數據。
1) python string
object可以理解為一個接一個位元組的位元組組,至於表示什麼編碼,與表示文字有關,比如「python
string」,「中文」。注意它是有不同編碼區分的。
PyQt中與之對應的是QbyteArray,而不是Qstring。
A built-in string object (plain or Unicode) is a sequence of
characters used to store and represent text-based information
(plain strings are also sometimes used to store and represent
arbitrary sequences of binary bytes). (摘自《Python in a
NutShell》)
QByteArray can be used to store both raw bytes (including '"0's)
and traditional 8-bit '"0'-terminated.(摘自《PyQt手冊》)
2)Python Unicode
object可以理解為固定使用utf-16編碼的位元組組,其中英文和中文都使用兩個位元組(16位)來表示,如:u"Python
Unicode object"、u"中文"。
PyQt中與之對應的就是QString了。
Unicode string literals have the same syntax as other string
literals, with a u or U immediately before the leading quote.
(摘自《Python in a NutShell》)
Qt also provides the QString class to store string data. It stores
16-bit Unicode characters, making it easy to store
non-ASCII/non-Latin-1 characters in your
application.(摘自《PyQt手冊》)
QString stores a string of 16-bit QChars, where each QChar
corresponds one Unicode 4.0 character.(摘自《PyQt手冊》)
2 PyQt內部類型轉換
QString有
toAscii()、toUtf8()函數轉換為QByteArray類型,(這個基本不用,因為很少直接用QByteArray類型)有__init__
(self, QByteArray a)函數將QByteArray類型轉為QString。
3. Python string object和Python Unicode object相互轉換
1)Python string object是原始編碼是有區分的,通過 decode('原始編碼')
函數解碼得到通用utf16編碼即Python Unicode object。
>>>"python
string".decode('ascii')
或者
>>>"python
string".decode()
得到 u"python string"
因為默認按ascii解碼。
>>>"中文".decode('gbk')
得到 u""u4e2d"u6587" ,列印出來就是 中文 二字。(注意結果是2位元組一組,共兩組,對應兩個漢字)
又:"python string".decode('gkb') ,即按漢字來解碼,也可以得到 u"python
string",因為gbk編碼也支持英文字母;
但是"中文".decode('ascii') 即按ascii解碼是錯誤的,因為ascii編碼不支持漢字!
>>>
"dfdf".decode()
u'dfdf'
>>>
"dfdf".decode("ascii")
u'dfdf'
>>>
"dfdf".decode("gbk")
u'dfdf'
>>>
"中文".decode("gbk")
u'"u4e2d"u6587'
>>>print
"中文".decode("gbk")
中文
>>>
"中文".decode("gb2312")
u'"u4e2d"u6587'
>>>
"中文".decode("ascii")
Traceback (most recent call last):
File "<interactive input>", line 1,
in <mole>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd6 in
position 0: ordinal not in range(128)
2)Python Unicode object原始編碼固定是utf16,通過 encode('目的編碼') 編碼來得到Python
string object。
>>>u"unicode
string".encode()
或者
>>>u"unicode
string".encode('ascii')
得到
'unicode string',默認目的編碼為ascii。
>>>u"中文".encode("gbk")
得到'"xd4"xd0"xce"xc4',列印出來就是 中文。(注意結果是1位元組一組,共4組)
>>>
u"sdff".encode()
'sdff'
>>>
u"sdff".encode('ascii')
'sdff'
>>>
u"sdff".encode('gbk')
'sdff'
>>>
u"sdff".encode('gb2312')
'sdff'
>>>
u"中文".encode('gbk')
'"xd6"xd0"xce"xc4'
>>> print
u"中文".encode('gbk')
中文
>>>
u"中文".encode('ascii')
Traceback (most recent call last):
File "<stdin>", line 1, in
<mole>
UnicodeEncodeError: 'ascii' codec can't encode characters in
position 0-1: ordin
al not in range(128)
注意:執行>>>
u"中文".encode('gbk')命令需要你的IDE支持gbk編碼,在官方shell下執行肯定沒問題,但如果你的IDE比如PyWin中文輸入異常,則可能報錯。
4. Python string object和Python Unicode object向QString的轉換。
Qt一般不直接操作QByteArray,只需關注Python string object和Python Unicode
object向QString的轉換。
很多關於PyQt4的英文書籍說:PyQt函數需要QString參數的地方都可以直接用Python string
object或者Python Unicode object,如果非要轉換可以直接用QtCore.QString()構造。比如《GUI
Programming with PyQt》,再如《PyQt手冊》:
Whenever PyQt expects a QString as a function argument, a Python
string object or a Python Unicode object can be provided instead,
and PyQt will do the necessary conversion automatically.
You may also manually convert Python string and Unicode objects to
QString instances by using the QString constructor as demonstrated
in the following code fragment:
qs1 = QtCore.QString("Converted Python string object")
qs2 = QtCore.QString(u"Converted Python Unicode object")
但可惜這只適用於英文即ascii編碼,對於中文則行不通!
直接的QString:
>>>
QtCore.QString('中文')
PyQt4.QtCore.QString(u'"xd6"xd0"xce"xc4')
>>> print
QtCore.QString('中文')
Traceback (most recent call last):
File "<stdin>", line 1, in
<mole>
UnicodeEncodeError: 'ascii' codec can't encode characters in
position 0-3: ordin
al not in range(128)
>>>
>>>
QtCore.QString(u'中文')
PyQt4.QtCore.QString(u'"u4e2d"u6587')
>>> print
QtCore.QString(u'中文')
Traceback (most recent call last):
File "<stdin>", line 1, in
<mole>
UnicodeEncodeError: 'ascii' codec can't encode characters in
position 0-1: ordin
al not in range(128)
>>>
因為它們都是默認按ascii編碼轉換!
GUI編程:
可以創建一個QTextEdit對象myTextEdit, 檢驗:
myTextEdit.append("中文")
或者
myTextEdit.append(u"中文")
或者
myTextEdit.append(QtCore.QString('中文'))
或者
myTextEdit.append(QtCore.QString(u'中文'))
你會發現顯示都是亂碼...因為它們都是默認按ascii編碼進行內部轉換得到QString相應utf16編碼的。
解決方法是:
利用unicode()函數顯示指定gb2312編碼進行中文編碼轉換,轉換後的Python Unicode
object則是可以直接作為QString參數代入用的:
>>> unicode('中文',
'gb2312', 'ignore')
u'"u4e2d"u6587'
>>> print
unicode('中文', 'gb2312', 'ignore')
中文
>>>
myTextEdit.append(unicode('中文', 'gb2312', 'ignore'))
#用以替代myTextEdit.append(u"中文")
或者多此一舉下:
myTextEdit.append(QtCore.QString(unicode('中文', 'gb2312',
'ignore')))
#用以替代myTextEdit.append(QtCore.QString(u'中文'))
5. QString向Python string object和Python Unicode object的轉換。
Python中需要用Python string object和Python Unicode
object的地方可就不一定可以直接用QString了!!!
QString向Python string object轉換可以理解,因為編碼不同。
QString向Python Unicode object的轉換?需要轉換嗎?不都是utf16編碼嗎?
QString是tuf16編碼,但是它的實現並非Python Unicode
object那樣直接的utf16碼,而實際是一個QChar串,每個QChar才對應unicode符,所以地位相當但並不相同。
許多英文書籍寫到:可以使用str()函數直接將QString轉換為Python string
object,可以使用unicode()直接將QString轉換為Python Unicode
object。如《PyQt手冊》:
In order to convert a QString to a Python string object use the
Python str() builtin. Applying str() to a null QString and an empty
QString both result in an empty Python string object.
In order to convert a QString to a Python Unicode object use the
Python unicode() builtin. Applying unicode() to a null QString and
an empty QString both result in an empty Python Unicode
object.
但同樣只適用於英文,具體見下面分別分析。
1)QString向Python Unicode object的轉換。
>>> from PyQt4 import
QtGui, QtCore
>>>
unicode(QtCore.QString('def'))
u'def'
>>> print
unicode(QtCore.QString('def'))
def
對於中文,unicode()必須要指定編碼後有效。(這樣也只針對直接的QString有效?對於Qt
GUI編程中,從QWidget取得的QString無效?)
>>> from PyQt4 import
QtGui, QtCore
>>>
unicode(QtCore.QString('中文'))
u'"xd6"xd0"xce"xc4'
>>> print
unicode(QtCore.QString('中文'))
Traceback (most recent call last):
File "<stdin>", line 1, in
<mole>
UnicodeEncodeError: 'gbk' codec can't encode character u'"xd6' in
position 0: il
legal multibyte sequence
指定原始編碼後:
>>>
unicode(QtCore.QString('中文'),'gbk','ignore')
u'"u4e2d"u6587'
>>> print
unicode(QtCore.QString('中文'),'gbk','ignore')
中文 TEST
⑻ pyqt4支持python2.7嗎
支持的
首先安裝python-2.7.6.amd64.msi,具體的配置步驟這里就不細說了,網路一下就解決了。
緊接著就是安裝pycharm-professional-4.0.4.exe,這是一個python編輯器,比官方的編輯器好用得多,可以自動代碼補全,還能調試運行,功能非常豐富!
再跟著就是安裝PyQt4-4.11.2-gpl-Py2.7-Qt4.8.6-x64.exe,其實pyqt與C++的qt差不多的,用法都非常相似!
接著安裝cx_Freeze-4.3.3.win-amd64-py2.7.msi,這個是用來打包Python程序為exe運行文件來用的,當然你也可以用其他的打包工具,但是我極力推薦這個。
最後要安裝chardet-1.0.1-py2.5.egg.rar,這個是跟Python對文件的編碼檢測的一個開發包,網路一下就有地方下載了,python對文本處理非常強大,有它非常方便!
磨刀不誤砍柴工,正式開工:
用pyqt4的界面設計工具–qt設計師,進行界面開發如下圖:
pyqt4的界面設計工具非常簡單易用,只要你用別的軟體進行過控制項開發,那麼這個自然就會了!
如果還不明白就可以看「PyQt4 精彩實例分析(語法高亮帶書簽).pdf」,裡麵包含了很多示例。
界面設計完成後,保存得到以ui為後綴的文件,我的為「mainText.ui」,「configText.ui」兩個文件,有了界面文件就要轉換成Python能調用的包,具體轉換的方法看「PyQt4
精彩實例分析(語法高亮帶書簽).pdf」中的示例。我的是分別轉換成兩個文件「ConfigText.py」,「SuperProcessText.py」。
至此開始正式編碼的工作,主窗口運行文件是superTextProcessTool.py,配置窗口的運行文件是ConfigTextTool.py。
對於文件編碼轉換,控制項之間如何調用,如何處理消息,都有了完整的示例,界面是仿照網上的一個軟體做出來的-「超級文本處理工具」做出來的。如果碰到不懂的函數,那麼可以查看「PyQt4.chm」,它包含了Pytqt4所有的函數說明!
⑼ 能同時安裝pyqt4和pyqt5嗎
安裝PtQt4前你需准備:
Xcode (10.6.1 自帶Xcode)
Qt - 我使用的是qt-mac-cocoa-opensource-4.6.2.dmg
SIP - 我使用的是sip-4.12.1.tar.gz
PyQt4 - 我使用的是PyQt-mac-gpl-snapshot-4.8.4.tar.gz
創建了一個pyqt文件夾在硬碟根目錄下,並將sip-4.12.1.tar.gz和PyQt-mac-gpl-snapshot-4.8.4.tar.gz
放到其中並解壓。
1。先安裝qt,默認安裝,這一步大部分人不會有問題,跳過。
2。接下來,打開終端(Terminal,在 Applications(應用程序)/Utilities(實用工具)/ 下),在終端輸入:export VERSIONER_PYTHON_PREFER_32_BIT=yes。
3.
使用"cd 路徑"命令將當前目錄設為sip-4.12.1,如: cd /pyqt/sip-4.12.1/, 然後進行配置(configure)【Re-build SIP in 32bit 】在終端中執行
python configure.py --arch i386
配置(configure)完成後就開始編譯,在終端中執行引用make clean(清除以前的安裝信息)make
編譯完成後就安裝,在終端中執行引用sudo make install
在這個過程中沒出現錯誤就是安裝成功了
編譯安裝PyQt4,過程和上面的一樣。 【Build PyQt4 in 32bit 】
在終端中使用"cd 路徑"命令將當前目錄設為PyQt-mac-gpl-snapshot-4.8.4,如
引用cd /Users/schi/pyqt/PyQt-mac-gpl-snapshot-4.8.4
然後進行配置(configure),在終端中執行引用python configure.py --use-arch i386
配置時會遇到引用Determining the layout of your Qt installation...
This is the GPL version of PyQt 4.6 (licensed under the GNU General Public
License) for Python 2.6.2 on darwin.
Type '2' to view the GPL v2 license.
Type '3' to view the GPL v3 license.
Type 'yes' to accept the terms of the license.
Type 'no' to decline the terms of the license.
Do you accept the terms of the license?
輸入yes,再按回車就行
配置(configure)完成後就開始編譯,在終端中執行
引用make cleanmake
編譯比較花時間,請耐心等待。
編譯完成後就安裝,在終端中執行
引用sudo make install
在這個過程中沒出現錯誤就是安裝成功了
也可以在IDLE中導入PyQt4的模塊,能成功導入就說明安裝成功
Python代碼
from PyQt4 import QtCore, QtGui
下面分析一下安裝過程中可能遇到的問題:
1.問題如下
##############
ld: warning: in /Library/Frameworks/Python.framework/Python, missing required architecture x86_64 in file
Undefined symbols:
"_Py_Initialize", referenced from:
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
"_PyLong_AsVoidPtr", referenced from:
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
"_PyErr_Print", referenced from:
PyCustomWidgets::getMoleAttr(char const*, char const*)in pluginloader.o
PyCustomWidgets::getMoleAttr(char const*, char const*)in pluginloader.o
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
"_PyString_FromString", referenced from:
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
"_PyType_IsSubtype", referenced from:
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
"_PyMole_GetDict", referenced from:
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
"_PyObject_CallObject", referenced from:
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
"_PyObject_CallFunctionObjArgs", referenced from:
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
"_PyList_Append", referenced from:
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
"_Py_IsInitialized", referenced from:
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
"_PyDict_Next", referenced from:
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
"_PyImport_ImportMole", referenced from:
PyCustomWidgets::getMoleAttr(char const*, char const*)in pluginloader.o
PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o
"_PyObject_GetAttrString", referenced from:
PyCustomWidgets::getMoleAttr(char const*, char const*)in pluginloader.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make[1]: *** [libpythonplugin.dylib] Error 1
make: *** [all] Error 2
這個問題的原因就是:qt是在32位mac上編譯的,但pyqt默認是在64位機子上編譯的,所以我們在編譯的時候要制定編譯環境:是i386 還是x86_64.
請參考如下的信息:
The problem is that the Qt library is 32bit while, by default, python builds PyQt4 in 64bit. That caused problem when it tried to link 64bit object files with 32bit Qt library. To fix this issue, we need to force python to build PyQt4 in 32bit, and we also need to re-build SIP in 32bit mode. If you use the python version that comes with Mac OS (2.5 for Snow Leopard), make sure that it runs in 32bit mode by issuing the command
2.個問題如下:
>>> from PyQt4 import QtGui
Traceback (most recent call last):
File "<stdin>", line 1, in <mole>
ImportError: dlopen(/Library/Python/2.6/site-packages/PyQt4/QtGui.so, 2): Symbol not found: __
Referenced from: /Library/Python/2.6/site-packages/PyQt4/QtGui.so
Expected in: flat namespace
in /Library/Python/2.6/site-packages/PyQt4/QtGui.so
這個問題是所有的包安裝都沒有問題但是當導入QtGui或其他包時,總報這個問題,這個問題的原因是,當前pyqt release的包本身帶的一個bug,PyQt-mac-gpl-snapshot-4.8.3, 所以你下載下個要release的包就可以了。如我下的是:PyQt-mac-gpl-snapshot-4.8.4。
下面給大家介紹一下我寫的自動安裝這些程序的python源碼:
#!/usr/bin/env python
"""
Automated installer for Python 2.6 (final)
This installer is able to handle any previous
versions of Python.
NOTE: This file *must* be copied to
//nssgsvr/tools/pyInstall_OSX when modified!
"""
import os
import sys
import re
import shutil
import platform
import tarfile
# Constants
PYTHON_PKGS = {"dmgFile" : "python-2.6.1-macosx2008-12-06.dmg",
"mpkgList": ["MacPython.mpkg"] }
#WXPYTHON_PKGS = {"dmgFile" : "wxPython2.8-osx-unicode-2.8.9.2-universal-py2.6.dmg",
# "mpkgList": ["wxPython2.8-osx-unicode-universal-py2.6.pkg"] }
#ANGELIA
XCODE_PKGS = {"dmgFile" : None, "mpkgList": ["Xcode.mpkg"]}
QT_PKGS = {"dmgFile" : "qt-mac-cocoa-opensource-4.6.2.dmg",
"mpkgList": ["Qt.mpkg"] }
THIRD_PARTY_INSTALL = ["sip-4.12.1", "PyQt-mac-gpl-snapshot-4.8.4","Pyro-3.9.1", "pyserial-2.4"]
# Environment constants
TCSH_ENV_FILE = '/etc/csh.login'
BASH_ENV_FILE = '/etc/profile'
SCRIPTS_ROOT_ENV = "TEST_SCRIPTS_ROOT"
TARGET_VER = "2.6"
IS_POSIX = (os.name == 'posix')
IS_MAC = (sys.version.find('Apple') != -1)
IS_OSX = (IS_MAC and IS_POSIX)
IS_OSX_LEOPARD = (IS_OSX and platform.mac_ver()[0].startswith('10.5'))
IS_OSX_SNOWLEOPARD = (IS_OSX and platform.mac_ver()[0].startswith('10.6'))
# NSSGSVR Constants
MOUNT_POINT = "/Volumes/Test"
HOSTSERVER = "NSSGSVR.global.avidww.com" # Server that contains SQA scripts
SHARE = "TEST" # NSSGSVR share that contains the Python scripts directory
USER = "qatest"
PASSWD = "Cmqat/$/$/$" # slashes needed to escape special character '$'