PyQt入门指南
我正在测试一些《用Python和Qt快速开发图形界面》中的例子,但在某些地方遇到了一些问题。当我照着书上的内容逐字复制以下练习时:
import sys
import time
from PyQt4.QtCore import *
from PyQt4.QtGui import *
app = QApplication(sys.argv)
try:
due = QTime.currentTime()
message = "Alert!"
if len(sys.argv) < 2:
raise ValueError
hours, mins = sys.argv[1].split(":")
due = QTime(int(hours), int(mins))
if not due.isValid():
raise ValueError
if len(sys.argv) > 2:
message = " ".join(sys.argv[2:])
except ValueError:
message = "Usage: alert.pyw HH:MM [optional message*]" # 24hr Clock
while QTime.currentTime() < due:
time.sleep(20) # 20 seconds
label = QLabel("<font color=red size=72><b>" + message + "</b></font>")
label.setWindowFlags(Qt.SplashScreen)
label.show()
QTimer.singleShot(60000, app.quit) # 1 minute
app.exec_()
我得到了以下错误:
andy@ASUSix:~/Documents/Programming/Python/PyQt$ from: can't read /var/mail/PyQt4.QtCore
from: can't read /var/mail/PyQt4.QtGui
./alert.pyw: line 6: syntax error near unexpected token `('
./alert.pyw: line 6: `app = QApplication(sys.argv)
这里出了什么问题?我的路径设置不正确吗?
1 个回答
8
你可能忘了在你的脚本开头加一个“shebang”,这个是用来告诉你的系统用Python解释器来运行这个脚本的。试着在你的脚本第一行加上
#!/usr/bin/python
,前提是你的Python解释器安装在那个位置。如果你不确定,可以试试
which python
。