刚接触python没多久,不免会弄出些奇怪的问题。今天在eric环境下调试python时发现在设置sys.argv[1]的参数
时莫名奇妙调试不过去,非得从sys.argv[2]起始才能解析参数。
1.参数若设置如下2开始在eric debug的时候F2运行如下界面
hours,mins = sys.argv[2].split(":")
2.在hours,mins = sys.argv[2].split(":")设置为1在F2的时候
3.在Konsole中运行的时候 python ui.py 16:32;hours,mins = sys.argv[1].split(":")
4.若改为hours,mins = sys.argv[2].split(":"),运行出错
xtramus@ubuntu:/usr/local/share/gnuradio/N210/UI$ python ui.py 16:32
Traceback (most recent call last):
File "ui.py", line 18, in
hours,mins = sys.argv[2].split(":")
IndexError: list index out of range
xtramus@ubuntu:/usr/local/share/gnuradio/N210/UI$
难道是eric的sys.argv的参数解析是从下标1开始的?
附代码
#!/usr/bin/env python
import sys
import time
from PyQt4.QtCore import (QTime, QTimer, Qt, SIGNAL)
from PyQt4.QtGui import (QApplication, QLabel)
app =QApplication(sys.argv)
print "sys.argv[0]%s\n"%sys.argv[0]
print "sys.argv[1]%s\n"%sys.argv[1]
print "sys.argv[2]%s\n"%sys.argv[2:]
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)
label =QLabel("{0}" .format(message))
label.setWindowFlags(Qt.SplashScreen)
label.show()
QTimer.singleShot(20000, app.quit)
app.exec_()
阅读(501) | 评论(0) | 转发(0) |