2016-04-11 63 views

回答

0

這是改編自薩默的PyQt的書,他的alert.py例如第4章的腳本。它是免費提供在線瀏覽: http://www.informit.com/articles/article.aspx?p=1149122

基本上你設定一個目標QDateTime,然後就可以通過目標比較currentQDateTime觸發任何你想要調用的:

from PySide import QtCore, QtGui 
import sys 
import time 

#Prepare alarm 
alarmMessage = "Wake up, sleepyhead!" 
year = 2016 
month = 4 
day = 23 
hour = 12 
minute = 40  
alarmDateTime = QtCore.QDateTime(int(year), int(month), int(day), int(hour), int(minute), int(0)) 
#Wait for alarm to trigger at appropriate time (check every 5 seconds) 
while QtCore.QDateTime.currentDateTime() < alarmDateTime: 
    time.sleep(5) 

#Once alarm is triggered, create and show QLabel, and then exit application 
qtApp = QtGui.QApplication(sys.argv) 
label = QtGui.QLabel(alarmMessage) 
label.setStyleSheet("QLabel { color: rgb(255, 0, 0); font-weight: bold; font-size: 25px; \ 
        background-color: rgb(0,0,0); border: 5px solid rgba(0 , 255, 0, 200)}") 
label.setWindowFlags(QtCore.Qt.SplashScreen | QtCore.Qt.WindowStaysOnTopHint) 
label.show() 
waitTime = 10000 #in milliseconds 
QtCore.QTimer.singleShot(waitTime, qtApp.quit) 
sys.exit(qtApp.exec_()) 

這其中有一個QLabel彈出在你想要的日子和時間,但那是任意的。你可以讓它做任何你想做的事。如果您希望它每天同時運行,則必須適當修改它,但這應該足以讓您開始使用QDateTime來觸發事件。

注意:如果您在Windows中工作,要在後臺運行,而不顯示在屏幕上一個命令行窗口,然後按照這裏的建議是:

How can I hide the console window in a PyQt app running on Windows?

也就是說,保存程序與.pyw擴展名,並確保它與pythonw.exe而不是python.exe運行。

相關問題