Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PySide,Qdate;每天在特定时间运行特定功能_Python_Qt_Pyqt_Pyside - Fatal编程技术网

Python PySide,Qdate;每天在特定时间运行特定功能

Python PySide,Qdate;每天在特定时间运行特定功能,python,qt,pyqt,pyside,Python,Qt,Pyqt,Pyside,我正在尝试用PySide开发gui,我想知道是否有办法在Python(QDate)中调用函数并在特定日期和时间运行它 例如: #run this function in everyday at 8:00 am. def print_something(): print "Hello" 下面是一个改编自Summerfield的PyQt书第4章的脚本,他的alert.py示例。可在以下网站免费获取: 基本上,您可以设置一个目标QDateTime,然后通过将目标与currentQDate

我正在尝试用PySide开发gui,我想知道是否有办法在Python(
QDate
)中调用函数并在特定日期和时间运行它

例如:

#run this function in everyday at 8:00 am. 
def print_something():
    print "Hello"

下面是一个改编自Summerfield的PyQt书第4章的脚本,他的
alert.py
示例。可在以下网站免费获取:

基本上,您可以设置一个目标
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_())
从PySide导入QtCore、QtGui
导入系统
导入时间
#准备警报
alarmMessage=“醒醒,瞌睡虫!”
年份=2016年
月份=4
日=23
小时=12
分钟=40
alarmDateTime=QtCore.QDateTime(整数(年)、整数(月)、整数(日)、整数(小时)、整数(分钟)、整数(0))
#等待警报在适当的时间触发(每5秒检查一次)
而QtCore.QDateTime.currentDateTime()
这一个在您想要的日期和时间弹出QLabel,但这是任意的。你可以让它做你想做的任何事情。如果希望它每天同时运行,则必须对其进行适当的修改,但这应该足以让您开始使用
QDateTime
触发事件

注意:如果您在Windows中工作,并且希望在后台运行,而屏幕上没有显示命令窗口,请按照此处的建议操作:


也就是说,用扩展名
.pyw
保存程序,并确保它是用
pythonw.exe
运行的,而不是
python.exe

这里有一个脚本,改编自Summerfield的PyQt书的第4章,他的
alert.py
示例。可在以下网站免费获取:

基本上,您可以设置一个目标
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_())
从PySide导入QtCore、QtGui
导入系统
导入时间
#准备警报
alarmMessage=“醒醒,瞌睡虫!”
年份=2016年
月份=4
日=23
小时=12
分钟=40
alarmDateTime=QtCore.QDateTime(整数(年)、整数(月)、整数(日)、整数(小时)、整数(分钟)、整数(0))
#等待警报在适当的时间触发(每5秒检查一次)
而QtCore.QDateTime.currentDateTime()
这一个在您想要的日期和时间弹出QLabel,但这是任意的。你可以让它做你想做的任何事情。如果希望它每天同时运行,则必须对其进行适当的修改,但这应该足以让您开始使用
QDateTime
触发事件

注意:如果您在Windows中工作,并且希望在后台运行,而屏幕上没有显示命令窗口,请按照此处的建议操作:

也就是说,用扩展名
.pyw
保存程序,并确保它是用
pythonw.exe
而不是
python.exe
运行的