Python+PyQt構(gòu)建自動(dòng)化定時(shí)任務(wù)執(zhí)行工具詳細(xì)代碼示例
一、引言
在數(shù)字化轉(zhuǎn)型加速的今天,自動(dòng)化執(zhí)行程序已成為提升工作效率的關(guān)鍵技術(shù)。Python憑借其簡(jiǎn)潔靈活的語(yǔ)法和豐富的生態(tài)庫(kù),成為開(kāi)發(fā)定時(shí)任務(wù)工具的首選語(yǔ)言;而PyQt作為成熟的跨平臺(tái)GUI框架,則為工具提供了直觀的用戶交互界面。將二者結(jié)合構(gòu)建的自動(dòng)化定時(shí)任務(wù)執(zhí)行器,不僅能實(shí)現(xiàn)精準(zhǔn)的任務(wù)觸發(fā)與循環(huán)控制,還能通過(guò)可視化配置降低使用門(mén)檻。
二、GUI界面設(shè)計(jì)
使用PyQt5進(jìn)行界面的搭建,界面如下:


初始界面搭建采用極簡(jiǎn)風(fēng)格。用戶通過(guò)點(diǎn)擊“上傳待執(zhí)行的程序”上傳.exe程序文件(自動(dòng)彈出文件選擇對(duì)話框),再選擇不同的定時(shí)方式(詳盡:精確到年月日時(shí)分秒,均可自定義;粗略:提供3秒后、5秒后、10秒后、30秒后、1分鐘后、3分鐘后、5分鐘后、10分鐘后、30分鐘后、1小時(shí)后多種選項(xiàng)),最后點(diǎn)擊“開(kāi)始倒計(jì)時(shí)”,即可在實(shí)現(xiàn)在指定時(shí)間運(yùn)行指定程序的功能。


1.效果演示

這里以“360瀏覽器.exe”為實(shí)例,選擇相應(yīng)啟動(dòng)方式,若時(shí)間未過(guò)期,點(diǎn)擊“開(kāi)始倒計(jì)時(shí)”,即可在對(duì)應(yīng)時(shí)間到達(dá)后自動(dòng)打開(kāi)360瀏覽器。
2.相關(guān)提示
若未上傳.exe可執(zhí)行文件,直接點(diǎn)擊“開(kāi)始倒計(jì)時(shí)”,會(huì)有以下提示。


若未設(shè)置時(shí)間(兩種方式任選其一),直接點(diǎn)擊“開(kāi)始倒計(jì)時(shí)”,會(huì)有以下提示。


若設(shè)置時(shí)間符合要求,直接點(diǎn)擊“開(kāi)始倒計(jì)時(shí)”,會(huì)有以下提示。


針對(duì)“詳盡”方式,尤其要把握精確時(shí)間,若自定義的時(shí)間小于當(dāng)前時(shí)間,則程序不能成功執(zhí)行,且會(huì)有以下提示。

3.界面設(shè)計(jì).py
通過(guò)pyuic5產(chǎn)生的GUI界面代碼UI_Main.py如下:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'UI_main.ui'
#
# Created by: PyQt5 UI code generator 5.15.11
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Main_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.setEnabled(True)
Form.resize(400, 500)
Form.setMinimumSize(QtCore.QSize(400, 500))
Form.setMaximumSize(QtCore.QSize(400, 500))
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/image1.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
Form.setWindowIcon(icon)
self.label_5 = QtWidgets.QLabel(Form)
self.label_5.setGeometry(QtCore.QRect(230, 70, 141, 31))
font = QtGui.QFont()
font.setFamily("Adobe Arabic")
font.setPointSize(16)
self.label_5.setFont(font)
self.label_5.setObjectName("label_5")
self.label_8 = QtWidgets.QLabel(Form)
self.label_8.setGeometry(QtCore.QRect(80, 10, 261, 61))
font = QtGui.QFont()
font.setFamily("字魂5號(hào)-無(wú)外潤(rùn)黑體")
font.setPointSize(26)
font.setBold(False)
font.setWeight(50)
self.label_8.setFont(font)
self.label_8.setObjectName("label_8")
self.label_10 = QtWidgets.QLabel(Form)
self.label_10.setGeometry(QtCore.QRect(310, 60, 71, 51))
self.label_10.setText("")
self.label_10.setPixmap(QtGui.QPixmap(":/image1.png"))
self.label_10.setObjectName("label_10")
self.label_3 = QtWidgets.QLabel(Form)
self.label_3.setGeometry(QtCore.QRect(20, 210, 181, 21))
font = QtGui.QFont()
font.setFamily("新宋體")
font.setPointSize(12)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(80, 400, 291, 23))
font = QtGui.QFont()
font.setFamily("新宋體")
self.pushButton_2.setFont(font)
self.pushButton_2.setObjectName("pushButton_2")
self.label_4 = QtWidgets.QLabel(Form)
self.label_4.setGeometry(QtCore.QRect(150, 288, 21, 21))
font = QtGui.QFont()
font.setFamily("新宋體")
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.comboBox = QtWidgets.QComboBox(Form)
self.comboBox.setGeometry(QtCore.QRect(90, 288, 61, 22))
font = QtGui.QFont()
font.setFamily("新宋體")
self.comboBox.setFont(font)
self.comboBox.setObjectName("comboBox")
self.label_6 = QtWidgets.QLabel(Form)
self.label_6.setGeometry(QtCore.QRect(250, 288, 21, 21))
font = QtGui.QFont()
font.setFamily("新宋體")
self.label_6.setFont(font)
self.label_6.setObjectName("label_6")
self.comboBox_2 = QtWidgets.QComboBox(Form)
self.comboBox_2.setGeometry(QtCore.QRect(190, 288, 61, 22))
font = QtGui.QFont()
font.setFamily("新宋體")
self.comboBox_2.setFont(font)
self.comboBox_2.setObjectName("comboBox_2")
self.label_7 = QtWidgets.QLabel(Form)
self.label_7.setGeometry(QtCore.QRect(350, 290, 16, 20))
font = QtGui.QFont()
font.setFamily("新宋體")
self.label_7.setFont(font)
self.label_7.setObjectName("label_7")
self.radioButton = QtWidgets.QRadioButton(Form)
self.radioButton.setGeometry(QtCore.QRect(20, 250, 51, 21))
font = QtGui.QFont()
font.setFamily("新宋體")
self.radioButton.setFont(font)
self.radioButton.setObjectName("radioButton")
self.radioButton_2 = QtWidgets.QRadioButton(Form)
self.radioButton_2.setGeometry(QtCore.QRect(20, 350, 51, 21))
font = QtGui.QFont()
font.setFamily("新宋體")
self.radioButton_2.setFont(font)
self.radioButton_2.setObjectName("radioButton_2")
self.comboBox_4 = QtWidgets.QComboBox(Form)
self.comboBox_4.setGeometry(QtCore.QRect(90, 350, 261, 22))
font = QtGui.QFont()
font.setFamily("新宋體")
self.comboBox_4.setFont(font)
self.comboBox_4.setObjectName("comboBox_4")
self.comboBox_4.addItem("")
self.comboBox_4.addItem("")
self.comboBox_4.addItem("")
self.comboBox_4.addItem("")
self.comboBox_4.addItem("")
self.comboBox_4.addItem("")
self.comboBox_4.addItem("")
self.comboBox_4.addItem("")
self.comboBox_4.addItem("")
self.comboBox_4.addItem("")
self.comboBox_4.addItem("")
self.comboBox_3 = QtWidgets.QComboBox(Form)
self.comboBox_3.setGeometry(QtCore.QRect(290, 288, 61, 22))
font = QtGui.QFont()
font.setFamily("新宋體")
self.comboBox_3.setFont(font)
self.comboBox_3.setObjectName("comboBox_3")
self.pushButton_4 = QtWidgets.QPushButton(Form)
self.pushButton_4.setGeometry(QtCore.QRect(80, 440, 291, 23))
font = QtGui.QFont()
font.setFamily("新宋體")
self.pushButton_4.setFont(font)
self.pushButton_4.setObjectName("pushButton_4")
self.label_9 = QtWidgets.QLabel(Form)
self.label_9.setGeometry(QtCore.QRect(80, 240, 291, 81))
self.label_9.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.label_9.setText("")
self.label_9.setObjectName("label_9")
self.label_11 = QtWidgets.QLabel(Form)
self.label_11.setGeometry(QtCore.QRect(80, 340, 291, 41))
self.label_11.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.label_11.setText("")
self.label_11.setObjectName("label_11")
self.label_13 = QtWidgets.QLabel(Form)
self.label_13.setGeometry(QtCore.QRect(20, 120, 201, 21))
font = QtGui.QFont()
font.setFamily("新宋體")
font.setPointSize(12)
self.label_13.setFont(font)
self.label_13.setObjectName("label_13")
self.pushButton_5 = QtWidgets.QPushButton(Form)
self.pushButton_5.setGeometry(QtCore.QRect(80, 150, 291, 23))
font = QtGui.QFont()
font.setFamily("新宋體")
self.pushButton_5.setFont(font)
self.pushButton_5.setObjectName("pushButton_5")
self.comboBox_5 = QtWidgets.QComboBox(Form)
self.comboBox_5.setGeometry(QtCore.QRect(90, 250, 61, 22))
font = QtGui.QFont()
font.setFamily("新宋體")
self.comboBox_5.setFont(font)
self.comboBox_5.setObjectName("comboBox_5")
self.label_14 = QtWidgets.QLabel(Form)
self.label_14.setGeometry(QtCore.QRect(150, 250, 21, 21))
font = QtGui.QFont()
font.setFamily("新宋體")
self.label_14.setFont(font)
self.label_14.setObjectName("label_14")
self.comboBox_6 = QtWidgets.QComboBox(Form)
self.comboBox_6.setGeometry(QtCore.QRect(190, 250, 61, 22))
font = QtGui.QFont()
font.setFamily("新宋體")
self.comboBox_6.setFont(font)
self.comboBox_6.setObjectName("comboBox_6")
self.label_15 = QtWidgets.QLabel(Form)
self.label_15.setGeometry(QtCore.QRect(250, 250, 21, 21))
font = QtGui.QFont()
font.setFamily("新宋體")
self.label_15.setFont(font)
self.label_15.setObjectName("label_15")
self.label_16 = QtWidgets.QLabel(Form)
self.label_16.setGeometry(QtCore.QRect(350, 250, 21, 21))
font = QtGui.QFont()
font.setFamily("新宋體")
self.label_16.setFont(font)
self.label_16.setObjectName("label_16")
self.comboBox_7 = QtWidgets.QComboBox(Form)
self.comboBox_7.setGeometry(QtCore.QRect(290, 250, 61, 22))
font = QtGui.QFont()
font.setFamily("新宋體")
self.comboBox_7.setFont(font)
self.comboBox_7.setObjectName("comboBox_7")
self.label_11.raise_()
self.label_9.raise_()
self.label_5.raise_()
self.label_8.raise_()
self.label_10.raise_()
self.label_3.raise_()
self.pushButton_2.raise_()
self.label_4.raise_()
self.comboBox.raise_()
self.label_6.raise_()
self.comboBox_2.raise_()
self.label_7.raise_()
self.radioButton.raise_()
self.radioButton_2.raise_()
self.comboBox_4.raise_()
self.comboBox_3.raise_()
self.pushButton_4.raise_()
self.label_13.raise_()
self.pushButton_5.raise_()
self.comboBox_5.raise_()
self.label_14.raise_()
self.comboBox_6.raise_()
self.label_15.raise_()
self.label_16.raise_()
self.comboBox_7.raise_()
self.retranslateUi(Form)
self.pushButton_4.clicked.connect(Form.close) # type: ignore
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Timer_exe"))
self.label_5.setText(_translate("Form", "Designed By"))
self.label_8.setText(_translate("Form", "程序定時(shí)執(zhí)行器"))
self.label_3.setText(_translate("Form", "二、設(shè)置程序啟動(dòng)時(shí)間"))
self.pushButton_2.setText(_translate("Form", "開(kāi)始倒計(jì)時(shí)"))
self.label_4.setText(_translate("Form", "時(shí)"))
self.label_6.setText(_translate("Form", "分"))
self.label_7.setText(_translate("Form", "秒"))
self.radioButton.setText(_translate("Form", "詳盡"))
self.radioButton_2.setText(_translate("Form", "粗略"))
self.comboBox_4.setItemText(0, _translate("Form", "請(qǐng)選擇"))
self.comboBox_4.setItemText(1, _translate("Form", "3秒后"))
self.comboBox_4.setItemText(2, _translate("Form", "5秒后"))
self.comboBox_4.setItemText(3, _translate("Form", "10秒后"))
self.comboBox_4.setItemText(4, _translate("Form", "30秒后"))
self.comboBox_4.setItemText(5, _translate("Form", "1分鐘后"))
self.comboBox_4.setItemText(6, _translate("Form", "3分鐘后"))
self.comboBox_4.setItemText(7, _translate("Form", "5分鐘后"))
self.comboBox_4.setItemText(8, _translate("Form", "10分鐘后"))
self.comboBox_4.setItemText(9, _translate("Form", "30分鐘后"))
self.comboBox_4.setItemText(10, _translate("Form", "1個(gè)小時(shí)后"))
self.pushButton_4.setText(_translate("Form", "退出系統(tǒng)"))
self.label_13.setText(_translate("Form", "一、選擇定時(shí)執(zhí)行程序"))
self.pushButton_5.setText(_translate("Form", "選擇待執(zhí)行的程序"))
self.label_14.setText(_translate("Form", "年"))
self.label_15.setText(_translate("Form", "月"))
self.label_16.setText(_translate("Form", "日"))
import ziyuan_rc
三、主要程序詳解
1.導(dǎo)入相關(guān)模塊
import sys, time, os from UI_main import * from PyQt5.QtWidgets import QApplication, QWidget # 保持窗口大小和qtdesigner中的一致 from PyQt5 import QtCore QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
time模塊與時(shí)間設(shè)置有關(guān);os模塊與啟動(dòng)運(yùn)行程序文件有關(guān);UI_main是界面設(shè)計(jì)文件; 不懂為啥需要導(dǎo)入Qtcore的,請(qǐng)看。
2.初始化設(shè)置
def __init__(self):
super(QWidget, self).__init__()
self.setupUi(self)
# 設(shè)置顯示狀態(tài)
self.comboBox.setVisible(False) # 時(shí)
self.comboBox_2.setVisible(False) # 分
self.comboBox_3.setVisible(False) # 秒
self.comboBox_5.setVisible(False) # 年
self.comboBox_6.setVisible(False) # 月
self.comboBox_7.setVisible(False) # 日
self.label_14.setVisible(False) # 年
self.label_15.setVisible(False) # 月
self.label_16.setVisible(False) # 日
self.label_4.setVisible(False) # 時(shí)
self.label_7.setVisible(False) # 秒
self.label_6.setVisible(False) # 分
self.label_9.setVisible(False) # 詳盡框
self.comboBox_4.setVisible(False) # 粗略選項(xiàng)
self.label_11.setVisible(False) # 粗略框
# 綁定信號(hào)
self.pushButton_5.clicked.connect(self.xuanze) # 上傳exe
self.pushButton_2.clicked.connect(self.kaishi) # 開(kāi)始執(zhí)行
self.radioButton.clicked.connect(self.xiangjin) # 選擇詳盡
self.radioButton_2.clicked.connect(self.culue) # 選擇粗略
self.comboBox_4.currentIndexChanged.connect(self.culue_change) # 粗略選項(xiàng)發(fā)生改變
self.comboBox_5.currentIndexChanged.connect(self.day_show) # 年份選項(xiàng)發(fā)生改變
self.comboBox_6.currentIndexChanged.connect(self.day_show) # 月份選項(xiàng)發(fā)生改變
self.shangchuan_flag = False # 是否成功上傳exe文件的標(biāo)志位
self.deng_time = -1 # 默認(rèn)等待時(shí)間為實(shí)現(xiàn)“選擇不同radio button切換顯示相應(yīng)控件”的不同效果,在界面初始化前,需隱藏兩種方式(詳盡與粗略)下的所有控件(主要是combobox與對(duì)應(yīng)的label);
接著將信號(hào)綁定于對(duì)應(yīng)函數(shù):pushbutton_5為“上傳.exe文件”,綁定于xuanze;pushbutton_2為“開(kāi)始倒計(jì)時(shí)”,綁定于kaishi;radiobutton表示“詳盡”,綁定于xiangjin;radiobutton_2為“粗略”,綁定于culue;combobox_4表示“粗略下拉框”,當(dāng)用戶改變其選項(xiàng)時(shí),將其綁定于culue_change;combobox_5表示“年份選擇框”,當(dāng)用戶選擇不同年份時(shí),將其綁定于day_show;combobox_6表示“月份選擇框”,當(dāng)用戶選擇不同月份時(shí),同樣將其綁定于day_show,從而顯示當(dāng)前月份的不同天數(shù)(既與年份有關(guān),又與月份有關(guān))。
最后,設(shè)置是否上傳成功的標(biāo)志位shangchuan_flag;設(shè)定程序倒計(jì)時(shí)時(shí)間初始值deng_time為-1。
3.上傳待執(zhí)行程序文件
def xuanze(self):
# 上傳.exe執(zhí)行文件
self.file, self.shangchuan_flag = shangchuan()通過(guò)調(diào)用自定義函數(shù)shangchuan實(shí)現(xiàn),并返回已選擇文件的文件路徑與上傳成功與否的標(biāo)志位。
4.時(shí)間選擇方式——詳盡
def xiangjin(self):
"""詳盡按鈕"""
# 隱藏掉與粗略選項(xiàng)相關(guān)的
self.comboBox_4.setVisible(False)
self.label_11.setVisible(False)
# 顯示出與詳盡選項(xiàng)相關(guān)的
self.comboBox_5.setVisible(True)
self.comboBox_6.setVisible(True)
self.comboBox_7.setVisible(True)
self.comboBox.setVisible(True)
self.comboBox_2.setVisible(True)
self.comboBox_3.setVisible(True)
self.label_14.setVisible(True)
self.label_15.setVisible(True)
self.label_16.setVisible(True)
self.label_4.setVisible(True)
self.label_6.setVisible(True)
self.label_7.setVisible(True)
self.label_9.setVisible(True)
# 重置所有,防止切換radio button時(shí)出現(xiàn)重復(fù)堆疊
self.comboBox_5.clear()
self.comboBox_6.clear()
self.comboBox_7.clear()
self.comboBox.clear()
self.comboBox_2.clear()
self.comboBox_3.clear()
# 設(shè)置所有首選項(xiàng)為“請(qǐng)選擇”
self.comboBox_5.addItem('請(qǐng)選擇')
self.comboBox_6.addItem('請(qǐng)選擇')
self.comboBox_7.addItem('請(qǐng)選擇')
self.comboBox.addItem('請(qǐng)選擇')
self.comboBox_2.addItem('請(qǐng)選擇')
self.comboBox_3.addItem('請(qǐng)選擇')
# 針對(duì)“年”
self.cur_nian = time.strftime("%Y", time.localtime())
# 使用列表推導(dǎo)式增添選項(xiàng)
self.comboBox_5.addItems([str(int(self.cur_nian)+i) for i in range(0, 6)]) # 年
self.comboBox_6.addItems([str(i) for i in range(1, 13)]) # 月
self.comboBox.addItems([str(i) for i in range(0, 24)]) # 時(shí)
self.comboBox_2.addItems([str(i) for i in range(0, 60)]) # 分
self.comboBox_3.addItems([str(i) for i in range(0, 60)]) # 秒當(dāng)用戶選擇radiobutton時(shí)(既選擇“詳盡”),需要顯示出“詳盡”下的所有控件,同時(shí)隱藏掉“粗略”下的所有控件(因?yàn)橛脩艨赡軙?huì)來(lái)回切換方式);為防止用戶來(lái)回切換方式而造成combobox內(nèi)容重復(fù)堆疊,所以每次需將“詳盡”下的所有combobox內(nèi)容清空,接著設(shè)置“詳盡”下的所有combobox首選項(xiàng)為“請(qǐng)選擇”;為實(shí)現(xiàn)年份的自適應(yīng)更新,這里需要獲取當(dāng)前年份,并使用列表推導(dǎo)式寫(xiě)入往后6年的年份數(shù)字;再使用同樣的方法寫(xiě)入月(1-12)、時(shí)(0-23)、分(0-59)、秒(0-59)數(shù)字選項(xiàng)。
5.時(shí)間選擇方式——詳盡(“日”選項(xiàng)的設(shè)置)
def day_show(self):
"""主要對(duì)日進(jìn)行設(shè)置,因?yàn)槠洳粌H與月份有關(guān),還與年份有關(guān)(閏年的2月)"""
self.comboBox_7.clear()
self.comboBox_7.addItem('請(qǐng)選擇!')
yue_text = self.comboBox_6.currentText() # 判斷用戶選擇的是哪個(gè)月
if yue_text in ['1','3','5','7','8','10','12']: # 31天的月份
self.comboBox_7.addItems([str(i) for i in range(1,32)])
elif yue_text in ['4','6','9','11']: # 30天的月份
self.comboBox_7.addItems(str(i) for i in range(1,31))
elif yue_text == '2': # 針對(duì)2月要特殊處理
if self.comboBox_5.currentIndex() != 0: # 除去年份為“請(qǐng)選擇”的特殊情況
if year_judge(self.comboBox_5.currentText()): # 為閏年,2月為29天
self.comboBox_7.addItems([str(i) for i in range(1,30)])
else: # 不為閏年,2月為28天
self.comboBox_7.addItems([str(i) for i in range(1,29)])
else: # 表示月份的“請(qǐng)選擇”選項(xiàng)
pass與其他combobox一樣,combobox_7“日下拉框”也需要clear()和添加首選項(xiàng)“請(qǐng)選擇”;combobox_7其不僅取決于“年下拉框”combobox_5是否為閏年,而且還取決于“月下拉框”combobox_6的選擇(有31天的月份,也有30天的月份);所以先判斷combobox_6的text是否在31天和30天的月份中,如果存在,直接使用列表推導(dǎo)式additems即可;否則如果為2月,還需通過(guò)自定義函數(shù)year_judge判斷當(dāng)前年份是否為閏年;如果為閏年,則列表推導(dǎo)式產(chǎn)生29天,否則產(chǎn)生28天;此時(shí),combobox_6未討論情況只剩下“請(qǐng)選擇”選項(xiàng),pass即可。
6.時(shí)間選擇方式——粗略
def culue(self):
"""粗略按鈕"""
# 顯示出與粗略選項(xiàng)相關(guān)的
self.comboBox_4.setVisible(True)
self.label_11.setVisible(True)
# 隱藏掉與詳盡選項(xiàng)相關(guān)的
self.comboBox.setVisible(False)
self.comboBox_2.setVisible(False)
self.comboBox_3.setVisible(False)
self.comboBox_3.setVisible(False)
self.comboBox_5.setVisible(False)
self.comboBox_6.setVisible(False)
self.comboBox_7.setVisible(False)
self.label_14.setVisible(False)
self.label_15.setVisible(False)
self.label_16.setVisible(False)
self.label_4.setVisible(False)
self.label_7.setVisible(False)
self.label_6.setVisible(False)
self.label_9.setVisible(False)當(dāng)用戶選擇radiobutton_2時(shí)(既選擇“粗略”),需要顯示出“粗略”下的所有控件,同時(shí)隱藏掉“詳盡”下的所有控件(因?yàn)橛脩艨赡軙?huì)來(lái)回切換方式)。
7.時(shí)間選擇方式——粗略(檢測(cè)選項(xiàng)變化)
def culue_change(self):
"""以index為標(biāo)識(shí),實(shí)時(shí)檢測(cè)用戶是否改變粗略選項(xiàng)"""
selected_index = self.comboBox_4.currentIndex()
match selected_index:
case 1:
self.deng_time = 3 # 3秒鐘,后面依此類(lèi)推,其余特殊情況為默認(rèn)值(-1)
case 2:
self.deng_time = 5
case 3:
self.deng_time = 10
case 4:
self.deng_time = 30
case 5:
self.deng_time = 60
case 6:
self.deng_time = 180
case 7:
self.deng_time = 300
case 8:
self.deng_time = 600
case 9:
self.deng_time = 1800
case 10:
self.deng_time = 3600以combobox_4的index變化來(lái)判斷用戶選擇多長(zhǎng)時(shí)間后開(kāi)始運(yùn)行程序;使用match語(yǔ)句匹配,并將時(shí)間單位統(tǒng)一轉(zhuǎn)換為“秒”。
8.開(kāi)始倒計(jì)時(shí)
def kaishi(self):
"""用戶點(diǎn)擊開(kāi)始倒計(jì)時(shí)后,執(zhí)行操作"""
if self.shangchuan_flag == False: # 判斷是否成功上傳exe文件
QtWidgets.QMessageBox.critical(self, "提示", "請(qǐng)上傳待執(zhí)行.exe文件!")
else:
if self.radioButton.isChecked(): # 詳盡選擇
# 所有詳盡選項(xiàng)均不為“請(qǐng)選擇”時(shí)
if (self.comboBox_5.currentIndex()!=0) and (self.comboBox_6.currentIndex()!=0) and (self.comboBox_7.currentIndex()!=0) and (self.comboBox.currentIndex()!=0) and (self.comboBox_2.currentIndex()!=0) and (self.comboBox_3.currentIndex()!=0):
time_choice = self.comboBox_5.currentText()+"-"+self.comboBox_6.currentText()+"-"+self.comboBox_7.currentText()+" "+self.comboBox.currentText()+":"+self.comboBox_2.currentText()+":"+self.comboBox_3.currentText()
time_output = self.comboBox_5.currentText() + "年" + self.comboBox_6.currentText() + "月" + self.comboBox_7.currentText() + "日" + self.comboBox.currentText() + "時(shí)" + self.comboBox_2.currentText() + "分" + self.comboBox_3.currentText()+"秒"
# 將日期時(shí)間字符串轉(zhuǎn)換為結(jié)構(gòu)化時(shí)間
time_struct = time.strptime(time_choice, "%Y-%m-%d %H:%M:%S")
# 將結(jié)構(gòu)化時(shí)間轉(zhuǎn)換為時(shí)間戳
time_target = time.mktime(time_struct)
# 預(yù)定時(shí)間與當(dāng)前時(shí)間作差
time_cha = time_target-time.time()
if time_cha<0: # 差值小于0,用戶輸入時(shí)間已過(guò)期
QtWidgets.QMessageBox.critical(self, "提示", "時(shí)間已過(guò)期,請(qǐng)重新選擇!")
else:
QtWidgets.QMessageBox.information(self, "準(zhǔn)備"," 點(diǎn)擊OK\n程序?qū)⒃? + time_output + "開(kāi)始執(zhí)行!")
time.sleep(time_cha)
os.system(self.file)
else:
QtWidgets.QMessageBox.critical(self, "提示", "請(qǐng)選擇時(shí)間!")
elif self.radioButton_2.isChecked(): # 粗略選擇
if self.deng_time == -1: # 證明用戶未選擇時(shí)間
QtWidgets.QMessageBox.critical(self, "提示", "請(qǐng)選擇時(shí)間!")
else:
QtWidgets.QMessageBox.information(self, "準(zhǔn)備", " 點(diǎn)擊OK \n開(kāi)始" + str(self.deng_time) + "秒倒計(jì)時(shí)!")
time.sleep(self.deng_time)
os.system(self.file)
else:
QtWidgets.QMessageBox.critical(self, "提示", "請(qǐng)選擇定時(shí)方式!")首先判斷用戶是否成功上傳待執(zhí)行的.exe程序文件,接著判斷用戶選擇的是哪個(gè)radiobutton(radiobutton表示“詳盡”,radiobutton_2表示"粗略")。
在“詳盡”方式中,需要先保證用戶對(duì)所有combobox的值都進(jìn)行了自定義,接著讀取并拼接所有combobox的內(nèi)容并將其轉(zhuǎn)換為結(jié)構(gòu)化時(shí)間;為得到預(yù)定時(shí)間與當(dāng)前時(shí)間的差值(秒),需再將其轉(zhuǎn)換為時(shí)間戳;如果兩者差小于0,則說(shuō)明用戶預(yù)定時(shí)間小于當(dāng)前時(shí)間,即過(guò)期,否則彈出友好提示,準(zhǔn)備通過(guò)time.sleep()延遲函數(shù)實(shí)現(xiàn)倒計(jì)時(shí)執(zhí)行。
在“粗略”方式中,首先排除“請(qǐng)選擇”的默認(rèn)選項(xiàng)這一特殊情況,接著直接通過(guò)time.sleep()延遲函數(shù)實(shí)現(xiàn)倒計(jì)時(shí)執(zhí)行。
9.自定義函數(shù)
def shangchuan():
"""用于上傳.exe文件,最終返回文件路徑和標(biāo)志位"""
flag = False
filepath, _ = QtWidgets.QFileDialog.getOpenFileName(None, "請(qǐng)選擇可執(zhí)行程序文件", "", "可執(zhí)行文件 (*.exe)") # 獲取文件路徑
if filepath:
QtWidgets.QMessageBox.information(None, "成功", "可執(zhí)行程序文件上傳成功!")
flag = True
return filepath, flag
else:
QtWidgets.QMessageBox.critical(None, "提示", "請(qǐng)上傳待執(zhí)行.exe文件!")
return filepath, flag首先,定義標(biāo)志位flag初始值為False,接著通過(guò)QFileDialog返回上傳文件的路徑至filepath;判斷filepath是否為空來(lái)驗(yàn)證用戶是否成功選取,最后返回路徑filepath和標(biāo)志位flag。
def year_judge(year):
"""判斷用戶輸入是否為閏年"""
if (int(year)%4==0) and (int(year)%100!=0):
return True
elif int(year)%400==0:
return True
else:
return False閏年的標(biāo)志:能被4整除的同時(shí),但不能被100整除;或者是能被400直接整除。
四、總程序代碼
import sys, time, os
from UI_main import *
from PyQt5.QtWidgets import QApplication, QWidget
# 保持窗口大小和qtdesigner中的一致
from PyQt5 import QtCore
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
class main_interface(QWidget, Main_Form):
def __init__(self):
super(QWidget, self).__init__()
self.setupUi(self)
# 設(shè)置顯示狀態(tài)
self.comboBox.setVisible(False) # 時(shí)
self.comboBox_2.setVisible(False) # 分
self.comboBox_3.setVisible(False) # 秒
self.comboBox_5.setVisible(False) # 年
self.comboBox_6.setVisible(False) # 月
self.comboBox_7.setVisible(False) # 日
self.label_14.setVisible(False) # 年
self.label_15.setVisible(False) # 月
self.label_16.setVisible(False) # 日
self.label_4.setVisible(False) # 時(shí)
self.label_7.setVisible(False) # 秒
self.label_6.setVisible(False) # 分
self.label_9.setVisible(False) # 詳盡框
self.comboBox_4.setVisible(False) # 粗略選項(xiàng)
self.label_11.setVisible(False) # 粗略框
# 綁定信號(hào)
self.pushButton_5.clicked.connect(self.xuanze) # 上傳exe
self.pushButton_2.clicked.connect(self.kaishi) # 開(kāi)始執(zhí)行
self.radioButton.clicked.connect(self.xiangjin) # 選擇詳盡
self.radioButton_2.clicked.connect(self.culue) # 選擇粗略
self.comboBox_4.currentIndexChanged.connect(self.culue_change) # 粗略選項(xiàng)發(fā)生改變
self.comboBox_5.currentIndexChanged.connect(self.day_show) # 年份選項(xiàng)發(fā)生改變
self.comboBox_6.currentIndexChanged.connect(self.day_show) # 月份選項(xiàng)發(fā)生改變
self.shangchuan_flag = False # 是否成功上傳exe文件的標(biāo)志位
self.deng_time = -1 # 默認(rèn)等待時(shí)間
def xuanze(self):
# 上傳.exe執(zhí)行文件
self.file, self.shangchuan_flag = shangchuan()
def xiangjin(self):
"""詳盡按鈕"""
# 隱藏掉與粗略選項(xiàng)相關(guān)的
self.comboBox_4.setVisible(False)
self.label_11.setVisible(False)
# 顯示出與詳盡選項(xiàng)相關(guān)的
self.comboBox_5.setVisible(True)
self.comboBox_6.setVisible(True)
self.comboBox_7.setVisible(True)
self.comboBox.setVisible(True)
self.comboBox_2.setVisible(True)
self.comboBox_3.setVisible(True)
self.label_14.setVisible(True)
self.label_15.setVisible(True)
self.label_16.setVisible(True)
self.label_4.setVisible(True)
self.label_6.setVisible(True)
self.label_7.setVisible(True)
self.label_9.setVisible(True)
# 重置所有,防止切換radio button時(shí)出現(xiàn)重復(fù)堆疊
self.comboBox_5.clear()
self.comboBox_6.clear()
self.comboBox_7.clear()
self.comboBox.clear()
self.comboBox_2.clear()
self.comboBox_3.clear()
# 設(shè)置所有首選項(xiàng)為“請(qǐng)選擇”
self.comboBox_5.addItem('請(qǐng)選擇')
self.comboBox_6.addItem('請(qǐng)選擇')
self.comboBox_7.addItem('請(qǐng)選擇')
self.comboBox.addItem('請(qǐng)選擇')
self.comboBox_2.addItem('請(qǐng)選擇')
self.comboBox_3.addItem('請(qǐng)選擇')
# 針對(duì)“年”
self.cur_nian = time.strftime("%Y", time.localtime())
# 使用列表推導(dǎo)式增添選項(xiàng)
self.comboBox_5.addItems([str(int(self.cur_nian)+i) for i in range(0, 6)]) # 年
self.comboBox_6.addItems([str(i) for i in range(1, 13)]) # 月
self.comboBox.addItems([str(i) for i in range(0, 24)]) # 時(shí)
self.comboBox_2.addItems([str(i) for i in range(0, 60)]) # 分
self.comboBox_3.addItems([str(i) for i in range(0, 60)]) # 秒
def day_show(self):
"""主要對(duì)日進(jìn)行設(shè)置,因?yàn)槠洳粌H與月份有關(guān),還與年份有關(guān)(閏年的2月)"""
self.comboBox_7.clear()
self.comboBox_7.addItem('請(qǐng)選擇!')
yue_text = self.comboBox_6.currentText() # 判斷用戶選擇的是哪個(gè)月
if yue_text in ['1','3','5','7','8','10','12']: # 31天的月份
self.comboBox_7.addItems([str(i) for i in range(1,32)])
elif yue_text in ['4','6','9','11']: # 30天的月份
self.comboBox_7.addItems(str(i) for i in range(1,31))
elif yue_text == '2': # 針對(duì)2月要特殊處理
if self.comboBox_5.currentIndex() != 0: # 除去年份為“請(qǐng)選擇”的特殊情況
if year_judge(self.comboBox_5.currentText()): # 為閏年,2月為29天
self.comboBox_7.addItems([str(i) for i in range(1,30)])
else: # 不為閏年,2月為28天
self.comboBox_7.addItems([str(i) for i in range(1,29)])
else: # 表示月份的“請(qǐng)選擇”選項(xiàng)
pass
def culue(self):
"""粗略按鈕"""
# 顯示出與粗略選項(xiàng)相關(guān)的
self.comboBox_4.setVisible(True)
self.label_11.setVisible(True)
# 隱藏掉與詳盡選項(xiàng)相關(guān)的
self.comboBox.setVisible(False)
self.comboBox_2.setVisible(False)
self.comboBox_3.setVisible(False)
self.comboBox_3.setVisible(False)
self.comboBox_5.setVisible(False)
self.comboBox_6.setVisible(False)
self.comboBox_7.setVisible(False)
self.label_14.setVisible(False)
self.label_15.setVisible(False)
self.label_16.setVisible(False)
self.label_4.setVisible(False)
self.label_7.setVisible(False)
self.label_6.setVisible(False)
self.label_9.setVisible(False)
def culue_change(self):
"""以index為標(biāo)識(shí),實(shí)時(shí)檢測(cè)用戶是否改變粗略選項(xiàng)"""
selected_index = self.comboBox_4.currentIndex()
match selected_index:
case 1:
self.deng_time = 3 # 3秒鐘,后面依此類(lèi)推,其余特殊情況為默認(rèn)值(-1)
case 2:
self.deng_time = 5
case 3:
self.deng_time = 10
case 4:
self.deng_time = 30
case 5:
self.deng_time = 60
case 6:
self.deng_time = 180
case 7:
self.deng_time = 300
case 8:
self.deng_time = 600
case 9:
self.deng_time = 1800
case 10:
self.deng_time = 3600
def kaishi(self):
"""用戶點(diǎn)擊開(kāi)始倒計(jì)時(shí)后,執(zhí)行操作"""
if self.shangchuan_flag == False: # 判斷是否成功上傳exe文件
QtWidgets.QMessageBox.critical(self, "提示", "請(qǐng)上傳待執(zhí)行.exe文件!")
else:
if self.radioButton.isChecked(): # 詳盡選擇
# 所有詳盡選項(xiàng)均不為“請(qǐng)選擇”時(shí)
if (self.comboBox_5.currentIndex()!=0) and (self.comboBox_6.currentIndex()!=0) and (self.comboBox_7.currentIndex()!=0) and (self.comboBox.currentIndex()!=0) and (self.comboBox_2.currentIndex()!=0) and (self.comboBox_3.currentIndex()!=0):
time_choice = self.comboBox_5.currentText()+"-"+self.comboBox_6.currentText()+"-"+self.comboBox_7.currentText()+" "+self.comboBox.currentText()+":"+self.comboBox_2.currentText()+":"+self.comboBox_3.currentText()
time_output = self.comboBox_5.currentText() + "年" + self.comboBox_6.currentText() + "月" + self.comboBox_7.currentText() + "日" + self.comboBox.currentText() + "時(shí)" + self.comboBox_2.currentText() + "分" + self.comboBox_3.currentText()+"秒"
# 將日期時(shí)間字符串轉(zhuǎn)換為結(jié)構(gòu)化時(shí)間
time_struct = time.strptime(time_choice, "%Y-%m-%d %H:%M:%S")
# 將結(jié)構(gòu)化時(shí)間轉(zhuǎn)換為時(shí)間戳
time_target = time.mktime(time_struct)
# 預(yù)定時(shí)間與當(dāng)前時(shí)間作差
time_cha = time_target-time.time()
if time_cha<0: # 差值小于0,用戶輸入時(shí)間已過(guò)期
QtWidgets.QMessageBox.critical(self, "提示", "時(shí)間已過(guò)期,請(qǐng)重新選擇!")
else:
QtWidgets.QMessageBox.information(self, "準(zhǔn)備"," 點(diǎn)擊OK\n程序?qū)⒃? + time_output + "開(kāi)始執(zhí)行!")
time.sleep(time_cha)
os.system(self.file)
else:
QtWidgets.QMessageBox.critical(self, "提示", "請(qǐng)選擇時(shí)間!")
elif self.radioButton_2.isChecked(): # 粗略選擇
if self.deng_time == -1: # 證明用戶未選擇時(shí)間
QtWidgets.QMessageBox.critical(self, "提示", "請(qǐng)選擇時(shí)間!")
else:
QtWidgets.QMessageBox.information(self, "準(zhǔn)備", " 點(diǎn)擊OK \n開(kāi)始" + str(self.deng_time) + "秒倒計(jì)時(shí)!")
time.sleep(self.deng_time)
os.system(self.file)
else:
QtWidgets.QMessageBox.critical(self, "提示", "請(qǐng)選擇定時(shí)方式!")
def shangchuan():
"""用于上傳.exe文件,最終返回文件路徑和標(biāo)志位"""
flag = False
filepath, _ = QtWidgets.QFileDialog.getOpenFileName(None, "請(qǐng)選擇可執(zhí)行程序文件", "", "可執(zhí)行文件 (*.exe)") # 獲取文件路徑
if filepath:
QtWidgets.QMessageBox.information(None, "成功", "可執(zhí)行程序文件上傳成功!")
flag = True
return filepath, flag
else:
QtWidgets.QMessageBox.critical(None, "提示", "請(qǐng)上傳待執(zhí)行.exe文件!")
return filepath, flag
def year_judge(year):
"""判斷用戶輸入是否為閏年"""
if (int(year)%4==0) and (int(year)%100!=0):
return True
elif int(year)%400==0:
return True
else:
return False
if __name__ == '__main__':
app = QApplication(sys.argv)
w = main_interface()
w.show()
sys.exit(app.exec_())總結(jié)
到此這篇關(guān)于Python+PyQt構(gòu)建自動(dòng)化定時(shí)任務(wù)執(zhí)行工具的文章就介紹到這了,更多相關(guān)Python PyQt構(gòu)建自動(dòng)化定時(shí)任務(wù)執(zhí)行內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Django在Win7下的安裝及創(chuàng)建項(xiàng)目hello word簡(jiǎn)明教程
這篇文章主要介紹了Django在Win7下的安裝及創(chuàng)建項(xiàng)目hello word,需要的朋友可以參考下2014-07-07
關(guān)于Numpy中argsort()函數(shù)的用法解讀
這篇文章主要介紹了關(guān)于Numpy中argsort()函數(shù)的用法解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
Python輕量級(jí)web框架bottle使用方法解析
這篇文章主要介紹了Python輕量級(jí)web框架bottle使用方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
PyCharm中找不到pandas庫(kù)的問(wèn)題解決
本文主要介紹了PyCharm中找不到pandas庫(kù)的問(wèn)題解決,文中通過(guò)幾種解決方法介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-04-04
Python+Selenium實(shí)現(xiàn)自動(dòng)填寫(xiě)問(wèn)卷
本文主要介紹了Python+Selenium實(shí)現(xiàn)自動(dòng)填寫(xiě)問(wèn)卷,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07

