PyQt入门

时间:2010-09-16 23:12:07

标签: python qt pyqt

我正在测试使用Python和Qt进行快速GUI编程中的一些示例,但是在这里或那里遇到了绊脚石。当我复制到下面的练习时(逐字,从书中):

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)

这里出了什么问题?我的PATH设置不正确吗?

2 个答案:

答案 0 :(得分:8)

您可能忘记在脚本中添加一个shebang,告诉您的shell实际使用Python解释器运行它。尝试添加

#!/usr/bin/python

作为脚本中的第一行,前提是安装了Python解释器。您可能想尝试

which python

如果您不确定。

答案 1 :(得分:2)

在我的Windows 7和Linux机器上它完美运行。发布PATH变量的内容并添加/usr/bin/env python作为脚本的第一行。还尝试使用python script.py运行它。它有效吗?

相关问题