我正在使用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_()
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
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)
Run Code Online (Sandbox Code Playgroud)
这里出了什么问题?我的PATH设置错误了吗?
您可能忘记在脚本中添加一个shebang,告诉shell使用Python解释器实际运行它.尝试添加
#!/usr/bin/python
Run Code Online (Sandbox Code Playgroud)
作为脚本中的第一行,前提是安装了Python解释器.你可能想尝试一下
which python
Run Code Online (Sandbox Code Playgroud)
万一你不确定.
| 归档时间: |
|
| 查看次数: |
5026 次 |
| 最近记录: |