最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python+PyQt構(gòu)建自動化定時任務(wù)執(zhí)行工具詳細代碼示例

 更新時間:2025年09月28日 08:36:44   作者:Goona_  
在日常工作中,我們常常會用到需要周期性執(zhí)行的任務(wù),這篇文章主要介紹了Python+PyQt構(gòu)建自動化定時任務(wù)執(zhí)行工具的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下

一、引言

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

二、GUI界面設(shè)計

使用PyQt5進行界面的搭建,界面如下:

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

1.效果演示

這里以“360瀏覽器.exe”為實例,選擇相應(yīng)啟動方式,若時間未過期,點擊“開始倒計時”,即可在對應(yīng)時間到達后自動打開360瀏覽器。

2.相關(guān)提示

若未上傳.exe可執(zhí)行文件,直接點擊“開始倒計時”,會有以下提示。

若未設(shè)置時間(兩種方式任選其一),直接點擊“開始倒計時”,會有以下提示。

若設(shè)置時間符合要求,直接點擊“開始倒計時”,會有以下提示。

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

3.界面設(shè)計.py

通過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號-無外潤黑體")
        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", "程序定時執(zhí)行器"))
        self.label_3.setText(_translate("Form", "二、設(shè)置程序啟動時間"))
        self.pushButton_2.setText(_translate("Form", "開始倒計時"))
        self.label_4.setText(_translate("Form", "時"))
        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", "請選擇"))
        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個小時后"))
        self.pushButton_4.setText(_translate("Form", "退出系統(tǒng)"))
        self.label_13.setText(_translate("Form", "一、選擇定時執(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è)置有關(guān);os模塊與啟動運行程序文件有關(guān);UI_main是界面設(shè)計文件; 不懂為啥需要導(dǎo)入Qtcore的,請看

2.初始化設(shè)置

    def __init__(self):
        super(QWidget, self).__init__()
        self.setupUi(self)
        # 設(shè)置顯示狀態(tài)
        self.comboBox.setVisible(False) # 時
        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) # 時
        self.label_7.setVisible(False) # 秒
        self.label_6.setVisible(False) # 分
        self.label_9.setVisible(False) # 詳盡框

        self.comboBox_4.setVisible(False) # 粗略選項
        self.label_11.setVisible(False) # 粗略框
        # 綁定信號
        self.pushButton_5.clicked.connect(self.xuanze) # 上傳exe
        self.pushButton_2.clicked.connect(self.kaishi) # 開始執(zhí)行
        self.radioButton.clicked.connect(self.xiangjin) # 選擇詳盡
        self.radioButton_2.clicked.connect(self.culue) # 選擇粗略
        self.comboBox_4.currentIndexChanged.connect(self.culue_change) # 粗略選項發(fā)生改變
        self.comboBox_5.currentIndexChanged.connect(self.day_show) # 年份選項發(fā)生改變
        self.comboBox_6.currentIndexChanged.connect(self.day_show) # 月份選項發(fā)生改變

        self.shangchuan_flag = False # 是否成功上傳exe文件的標志位
        self.deng_time = -1 # 默認等待時間

為實現(xiàn)“選擇不同radio button切換顯示相應(yīng)控件”的不同效果,在界面初始化前,需隱藏兩種方式(詳盡與粗略)下的所有控件(主要是combobox與對應(yīng)的label);

接著將信號綁定于對應(yīng)函數(shù):pushbutton_5為“上傳.exe文件”,綁定于xuanze;pushbutton_2為“開始倒計時”,綁定于kaishi;radiobutton表示“詳盡”,綁定于xiangjin;radiobutton_2為“粗略”,綁定于culue;combobox_4表示“粗略下拉框”,當用戶改變其選項時,將其綁定于culue_change;combobox_5表示“年份選擇框”,當用戶選擇不同年份時,將其綁定于day_show;combobox_6表示“月份選擇框”,當用戶選擇不同月份時,同樣將其綁定于day_show,從而顯示當前月份的不同天數(shù)(既與年份有關(guān),又與月份有關(guān))。

最后,設(shè)置是否上傳成功的標志位shangchuan_flag;設(shè)定程序倒計時時間初始值deng_time為-1。

3.上傳待執(zhí)行程序文件

    def xuanze(self):
        # 上傳.exe執(zhí)行文件
        self.file, self.shangchuan_flag = shangchuan()

通過調(diào)用自定義函數(shù)shangchuan實現(xiàn),并返回已選擇文件的文件路徑與上傳成功與否的標志位。

4.時間選擇方式——詳盡

    def xiangjin(self):
        """詳盡按鈕"""
        # 隱藏掉與粗略選項相關(guān)的
        self.comboBox_4.setVisible(False)
        self.label_11.setVisible(False)
        # 顯示出與詳盡選項相關(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時出現(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è)置所有首選項為“請選擇”
        self.comboBox_5.addItem('請選擇')
        self.comboBox_6.addItem('請選擇')
        self.comboBox_7.addItem('請選擇')
        self.comboBox.addItem('請選擇')
        self.comboBox_2.addItem('請選擇')
        self.comboBox_3.addItem('請選擇')

        # 針對“年”
        self.cur_nian = time.strftime("%Y", time.localtime())
        # 使用列表推導(dǎo)式增添選項
        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)]) # 時
        self.comboBox_2.addItems([str(i) for i in range(0, 60)]) # 分
        self.comboBox_3.addItems([str(i) for i in range(0, 60)]) # 秒

當用戶選擇radiobutton時(既選擇“詳盡”),需要顯示出“詳盡”下的所有控件,同時隱藏掉“粗略”下的所有控件因為用戶可能會來回切換方式);為防止用戶來回切換方式而造成combobox內(nèi)容重復(fù)堆疊,所以每次需將“詳盡”下的所有combobox內(nèi)容清空,接著設(shè)置“詳盡”下的所有combobox首選項為“請選擇”;為實現(xiàn)年份的自適應(yīng)更新,這里需要獲取當前年份,并使用列表推導(dǎo)式寫入往后6年的年份數(shù)字;再使用同樣的方法寫入月(1-12)、時(0-23)、分(0-59)、秒(0-59)數(shù)字選項。

5.時間選擇方式——詳盡(“日”選項的設(shè)置)

    def day_show(self):
        """主要對日進行設(shè)置,因為其不僅與月份有關(guān),還與年份有關(guān)(閏年的2月)"""
        self.comboBox_7.clear()
        self.comboBox_7.addItem('請選擇!')
        yue_text = self.comboBox_6.currentText() # 判斷用戶選擇的是哪個月
        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': # 針對2月要特殊處理
            if self.comboBox_5.currentIndex() != 0: # 除去年份為“請選擇”的特殊情況
                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: # 表示月份的“請選擇”選項
            pass

與其他combobox一樣,combobox_7“日下拉框”也需要clear()和添加首選項“請選擇”;combobox_7其不僅取決于“年下拉框”combobox_5是否為閏年,而且還取決于“月下拉框”combobox_6的選擇(有31天的月份,也有30天的月份);所以先判斷combobox_6的text是否在31天和30天的月份中,如果存在,直接使用列表推導(dǎo)式additems即可;否則如果為2月,還需通過自定義函數(shù)year_judge判斷當前年份是否為閏年;如果為閏年,則列表推導(dǎo)式產(chǎn)生29天,否則產(chǎn)生28天;此時,combobox_6未討論情況只剩下“請選擇”選項,pass即可。

6.時間選擇方式——粗略

    def culue(self):
        """粗略按鈕"""
        # 顯示出與粗略選項相關(guān)的
        self.comboBox_4.setVisible(True)
        self.label_11.setVisible(True)
        # 隱藏掉與詳盡選項相關(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)

當用戶選擇radiobutton_2時(既選擇“粗略”),需要顯示出“粗略”下的所有控件,同時隱藏掉“詳盡”下的所有控件(因為用戶可能會來回切換方式)。

7.時間選擇方式——粗略(檢測選項變化)

    def culue_change(self):
        """以index為標識,實時檢測用戶是否改變粗略選項"""
        selected_index = self.comboBox_4.currentIndex()
        match selected_index:
            case 1:
                self.deng_time = 3 # 3秒鐘,后面依此類推,其余特殊情況為默認值(-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變化來判斷用戶選擇多長時間后開始運行程序;使用match語句匹配,并將時間單位統(tǒng)一轉(zhuǎn)換為“”。

8.開始倒計時

    def kaishi(self):
        """用戶點擊開始倒計時后,執(zhí)行操作"""
        if self.shangchuan_flag == False: # 判斷是否成功上傳exe文件
            QtWidgets.QMessageBox.critical(self, "提示", "請上傳待執(zhí)行.exe文件!")
        else:
            if self.radioButton.isChecked(): # 詳盡選擇
                # 所有詳盡選項均不為“請選擇”時
                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() + "時" + self.comboBox_2.currentText() + "分" + self.comboBox_3.currentText()+"秒"
                    # 將日期時間字符串轉(zhuǎn)換為結(jié)構(gòu)化時間
                    time_struct = time.strptime(time_choice, "%Y-%m-%d %H:%M:%S")
                    # 將結(jié)構(gòu)化時間轉(zhuǎn)換為時間戳
                    time_target = time.mktime(time_struct)
                    # 預(yù)定時間與當前時間作差
                    time_cha = time_target-time.time()
                    if time_cha<0: # 差值小于0,用戶輸入時間已過期
                        QtWidgets.QMessageBox.critical(self, "提示", "時間已過期,請重新選擇!")
                    else:
                        QtWidgets.QMessageBox.information(self, "準備","                              點擊OK\n程序?qū)⒃? + time_output + "開始執(zhí)行!")
                        time.sleep(time_cha)
                        os.system(self.file)
                else:
                    QtWidgets.QMessageBox.critical(self, "提示", "請選擇時間!")

            elif self.radioButton_2.isChecked(): # 粗略選擇
                if self.deng_time == -1: # 證明用戶未選擇時間
                    QtWidgets.QMessageBox.critical(self, "提示", "請選擇時間!")
                else:
                    QtWidgets.QMessageBox.information(self, "準備", "      點擊OK    \n開始" + str(self.deng_time) + "秒倒計時!")
                    time.sleep(self.deng_time)
                    os.system(self.file)
            else:
                QtWidgets.QMessageBox.critical(self, "提示", "請選擇定時方式!")

首先判斷用戶是否成功上傳待執(zhí)行的.exe程序文件,接著判斷用戶選擇的是哪個radiobutton(radiobutton表示“詳盡”,radiobutton_2表示"粗略")。

在“詳盡”方式中,需要先保證用戶對所有combobox的值都進行了自定義,接著讀取并拼接所有combobox的內(nèi)容并將其轉(zhuǎn)換為結(jié)構(gòu)化時間;為得到預(yù)定時間與當前時間的差值(秒),需再將其轉(zhuǎn)換為時間戳;如果兩者差小于0,則說明用戶預(yù)定時間小于當前時間,即過期,否則彈出友好提示,準備通過time.sleep()延遲函數(shù)實現(xiàn)倒計時執(zhí)行。

在“粗略”方式中,首先排除“請選擇”的默認選項這一特殊情況,接著直接通過time.sleep()延遲函數(shù)實現(xiàn)倒計時執(zhí)行。

9.自定義函數(shù)

def shangchuan():
    """用于上傳.exe文件,最終返回文件路徑和標志位"""
    flag = False
    filepath, _ = QtWidgets.QFileDialog.getOpenFileName(None, "請選擇可執(zhí)行程序文件", "", "可執(zhí)行文件 (*.exe)")  # 獲取文件路徑
    if filepath:
        QtWidgets.QMessageBox.information(None, "成功", "可執(zhí)行程序文件上傳成功!")
        flag = True
        return filepath, flag
    else:
        QtWidgets.QMessageBox.critical(None, "提示", "請上傳待執(zhí)行.exe文件!")
        return filepath, flag

首先,定義標志位flag初始值為False,接著通過QFileDialog返回上傳文件的路徑至filepath;判斷filepath是否為空來驗證用戶是否成功選取,最后返回路徑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

閏年的標志:能被4整除的同時,但不能被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) # 時
        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) # 時
        self.label_7.setVisible(False) # 秒
        self.label_6.setVisible(False) # 分
        self.label_9.setVisible(False) # 詳盡框

        self.comboBox_4.setVisible(False) # 粗略選項
        self.label_11.setVisible(False) # 粗略框
        # 綁定信號
        self.pushButton_5.clicked.connect(self.xuanze) # 上傳exe
        self.pushButton_2.clicked.connect(self.kaishi) # 開始執(zhí)行
        self.radioButton.clicked.connect(self.xiangjin) # 選擇詳盡
        self.radioButton_2.clicked.connect(self.culue) # 選擇粗略
        self.comboBox_4.currentIndexChanged.connect(self.culue_change) # 粗略選項發(fā)生改變
        self.comboBox_5.currentIndexChanged.connect(self.day_show) # 年份選項發(fā)生改變
        self.comboBox_6.currentIndexChanged.connect(self.day_show) # 月份選項發(fā)生改變

        self.shangchuan_flag = False # 是否成功上傳exe文件的標志位
        self.deng_time = -1 # 默認等待時間

    def xuanze(self):
        # 上傳.exe執(zhí)行文件
        self.file, self.shangchuan_flag = shangchuan()

    def xiangjin(self):
        """詳盡按鈕"""
        # 隱藏掉與粗略選項相關(guān)的
        self.comboBox_4.setVisible(False)
        self.label_11.setVisible(False)
        # 顯示出與詳盡選項相關(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時出現(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è)置所有首選項為“請選擇”
        self.comboBox_5.addItem('請選擇')
        self.comboBox_6.addItem('請選擇')
        self.comboBox_7.addItem('請選擇')
        self.comboBox.addItem('請選擇')
        self.comboBox_2.addItem('請選擇')
        self.comboBox_3.addItem('請選擇')

        # 針對“年”
        self.cur_nian = time.strftime("%Y", time.localtime())
        # 使用列表推導(dǎo)式增添選項
        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)]) # 時
        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):
        """主要對日進行設(shè)置,因為其不僅與月份有關(guān),還與年份有關(guān)(閏年的2月)"""
        self.comboBox_7.clear()
        self.comboBox_7.addItem('請選擇!')
        yue_text = self.comboBox_6.currentText() # 判斷用戶選擇的是哪個月
        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': # 針對2月要特殊處理
            if self.comboBox_5.currentIndex() != 0: # 除去年份為“請選擇”的特殊情況
                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: # 表示月份的“請選擇”選項
            pass

    def culue(self):
        """粗略按鈕"""
        # 顯示出與粗略選項相關(guān)的
        self.comboBox_4.setVisible(True)
        self.label_11.setVisible(True)
        # 隱藏掉與詳盡選項相關(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為標識,實時檢測用戶是否改變粗略選項"""
        selected_index = self.comboBox_4.currentIndex()
        match selected_index:
            case 1:
                self.deng_time = 3 # 3秒鐘,后面依此類推,其余特殊情況為默認值(-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):
        """用戶點擊開始倒計時后,執(zhí)行操作"""
        if self.shangchuan_flag == False: # 判斷是否成功上傳exe文件
            QtWidgets.QMessageBox.critical(self, "提示", "請上傳待執(zhí)行.exe文件!")
        else:
            if self.radioButton.isChecked(): # 詳盡選擇
                # 所有詳盡選項均不為“請選擇”時
                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() + "時" + self.comboBox_2.currentText() + "分" + self.comboBox_3.currentText()+"秒"
                    # 將日期時間字符串轉(zhuǎn)換為結(jié)構(gòu)化時間
                    time_struct = time.strptime(time_choice, "%Y-%m-%d %H:%M:%S")
                    # 將結(jié)構(gòu)化時間轉(zhuǎn)換為時間戳
                    time_target = time.mktime(time_struct)
                    # 預(yù)定時間與當前時間作差
                    time_cha = time_target-time.time()
                    if time_cha<0: # 差值小于0,用戶輸入時間已過期
                        QtWidgets.QMessageBox.critical(self, "提示", "時間已過期,請重新選擇!")
                    else:
                        QtWidgets.QMessageBox.information(self, "準備","                              點擊OK\n程序?qū)⒃? + time_output + "開始執(zhí)行!")
                        time.sleep(time_cha)
                        os.system(self.file)
                else:
                    QtWidgets.QMessageBox.critical(self, "提示", "請選擇時間!")

            elif self.radioButton_2.isChecked(): # 粗略選擇
                if self.deng_time == -1: # 證明用戶未選擇時間
                    QtWidgets.QMessageBox.critical(self, "提示", "請選擇時間!")
                else:
                    QtWidgets.QMessageBox.information(self, "準備", "      點擊OK    \n開始" + str(self.deng_time) + "秒倒計時!")
                    time.sleep(self.deng_time)
                    os.system(self.file)
            else:
                QtWidgets.QMessageBox.critical(self, "提示", "請選擇定時方式!")

def shangchuan():
    """用于上傳.exe文件,最終返回文件路徑和標志位"""
    flag = False
    filepath, _ = QtWidgets.QFileDialog.getOpenFileName(None, "請選擇可執(zhí)行程序文件", "", "可執(zhí)行文件 (*.exe)")  # 獲取文件路徑
    if filepath:
        QtWidgets.QMessageBox.information(None, "成功", "可執(zhí)行程序文件上傳成功!")
        flag = True
        return filepath, flag
    else:
        QtWidgets.QMessageBox.critical(None, "提示", "請上傳待執(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)建自動化定時任務(wù)執(zhí)行工具的文章就介紹到這了,更多相關(guān)Python PyQt構(gòu)建自動化定時任務(wù)執(zhí)行內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python使用PyAutoGUI模擬鼠標鍵盤操作實現(xiàn)消息發(fā)送

    Python使用PyAutoGUI模擬鼠標鍵盤操作實現(xiàn)消息發(fā)送

    本文介紹了如何使用Python的PyAutoGUI庫模擬鼠標和鍵盤操作,以微信為例,自動打開軟件、搜索好友、輸入消息和發(fā)送消息,通過本文,讀者可以了解PyAutoGUI的核心思路,需要的朋友可以參考下
    2026-04-04
  • python3讓print輸出不換行的方法

    python3讓print輸出不換行的方法

    在本篇內(nèi)容里小編給大家整理的是關(guān)于python3讓print輸出不換行的方法,有需要的朋友們可以學(xué)習(xí)參考下。
    2020-08-08
  • Python基于docker部署的Mysql備份查詢腳本

    Python基于docker部署的Mysql備份查詢腳本

    這篇文章主要來和大家分享Python基于docker部署的Mysql備份查詢的腳本,文中的示例代碼講解詳細,有需要的小伙伴可以跟隨小編一起了解下
    2024-04-04
  • numpy 數(shù)組拷貝地址所引起的同步替換問題

    numpy 數(shù)組拷貝地址所引起的同步替換問題

    本文主要介紹了numpy 數(shù)組拷貝地址所引起的同步替換問題,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • Python實現(xiàn)自動訪問網(wǎng)頁的例子

    Python實現(xiàn)自動訪問網(wǎng)頁的例子

    今天小編就為大家分享一篇Python實現(xiàn)自動訪問網(wǎng)頁的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • python獲取txt文件詞向量過程詳解

    python獲取txt文件詞向量過程詳解

    這篇文章主要介紹了python獲取txt文件詞向量過程詳解,如何讀取完整的大文件,而不會出現(xiàn)內(nèi)存不足memery error等問題,將讀取出來的文件,保存為npy文件,根據(jù)詞找到對應(yīng)的向量,需要的朋友可以參考下
    2019-07-07
  • Python使用python-pptx自動化操作和生成PPT

    Python使用python-pptx自動化操作和生成PPT

    這篇文章主要為大家詳細介紹了如何使用python-pptx庫實現(xiàn)PPT自動化,并提供實用的代碼示例和應(yīng)用場景,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-08-08
  • python實現(xiàn)測試工具(二)——簡單的ui測試工具

    python實現(xiàn)測試工具(二)——簡單的ui測試工具

    這篇文章主要介紹了python如何實現(xiàn)簡單的ui測試工具,幫助大家更好的利用python進行測試工作,感興趣的朋友可以了解下
    2020-10-10
  • python3.9實現(xiàn)pyinstaller打包python文件成exe

    python3.9實現(xiàn)pyinstaller打包python文件成exe

    這篇文章主要介紹了python3.9實現(xiàn)pyinstaller打包python文件成exe,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Django一小時寫出賬號密碼管理系統(tǒng)

    Django一小時寫出賬號密碼管理系統(tǒng)

    這篇文章主要介紹了Django一小時寫出賬號密碼管理系統(tǒng),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04

最新評論

无锡市| 龙胜| 辰溪县| 葵青区| 新巴尔虎左旗| 邵阳市| 东安县| 东兴市| 青浦区| 承德市| 浪卡子县| 岑溪市| 平乐县| 三都| 视频| 德庆县| 平度市| 安福县| 建昌县| 永嘉县| 张家港市| 盱眙县| 辽中县| 安顺市| 丰都县| 彭水| 齐齐哈尔市| 靖西县| 舟山市| 永泰县| 红安县| 安徽省| 天全县| 湘潭县| 丹寨县| 尼玛县| 磐安县| 英吉沙县| 潮州市| 宁阳县| 沙雅县|