欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Python開發(fā)電腦定時(shí)關(guān)機(jī)工具

 更新時(shí)間:2025年01月22日 09:09:03   作者:黑客白澤  
這篇文章主要為大家詳細(xì)介紹了如何基于Python開發(fā)一個(gè)電腦定時(shí)關(guān)機(jī)工具,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

1. 簡(jiǎn)介

這個(gè)程序就像一個(gè)“忠實(shí)的管家”,幫你按時(shí)關(guān)掉電腦,而且全程不需要你多做什么。你只需要設(shè)定好時(shí)間,它就能在指定的時(shí)刻執(zhí)行關(guān)機(jī)任務(wù),絕對(duì)精準(zhǔn),絕對(duì)準(zhǔn)時(shí)。如果你突然改變主意,管家也非常懂事,立刻取消任務(wù),并且讓你確認(rèn)是否真的要放棄關(guān)機(jī)。

功能概述

1.選擇關(guān)機(jī)時(shí)間:

你可以選擇一個(gè)未來的時(shí)間點(diǎn)(絕對(duì)時(shí)間),或者從現(xiàn)在開始延遲一段時(shí)間(相對(duì)時(shí)間),就像預(yù)約鬧鐘一樣,精確到秒!

2.定時(shí)關(guān)機(jī):

設(shè)置好關(guān)機(jī)時(shí)間后,程序會(huì)自動(dòng)計(jì)算倒計(jì)時(shí),不管你去做什么,它都會(huì)提醒你:“再過 XX 小時(shí) XX 分鐘 XX 秒,電腦就會(huì)乖乖關(guān)機(jī)。”

3.隨時(shí)取消:

你改變了主意,突然不想關(guān)機(jī)了?沒問題!隨時(shí)點(diǎn)擊取消,它會(huì)立馬停止關(guān)機(jī)進(jìn)程,不會(huì)讓你后悔。

4.倒計(jì)時(shí)提醒:

在你設(shè)置的時(shí)間到來之前,它會(huì)在界面上顯示倒計(jì)時(shí),讓你時(shí)刻知道剩余的時(shí)間。想象一下,一邊工作一邊看著電腦準(zhǔn)備入睡的樣子。
簡(jiǎn)而言之,這個(gè)工具就像是一個(gè)非常懂事的“關(guān)機(jī)助手”,不會(huì)打擾你,卻能在你需要它的時(shí)候,安靜地完成任務(wù)。

2. 運(yùn)行效果

3. 相關(guān)源碼

import sys
import datetime
import subprocess
from PyQt6.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout, 
                             QHBoxLayout, QLabel, QPushButton, QRadioButton, 
                             QDateTimeEdit, QTimeEdit, QMessageBox, QGroupBox, 
                             QSpacerItem, QSizePolicy)
from PyQt6.QtCore import Qt, QTimer, QDateTime
from PyQt6.QtGui import QFont, QColor

# 常量定義
BUTTON_EXECUTE_TEXT = '執(zhí)行'
BUTTON_CANCEL_TEXT = '取消'
BUTTON_EXECUTING_TEXT = '執(zhí)行中...'
LABEL_COUNTDOWN_DEFAULT = '未設(shè)置定時(shí)關(guān)機(jī)'
ALERT_FUTURE_TIME = '請(qǐng)選擇一個(gè)未來的時(shí)間!'
ALERT_CONFIRM_CANCEL = '是否要修改定時(shí)關(guān)機(jī)設(shè)置?'


class ShutdownTimer(QMainWindow):
    def __init__(self):
        super().__init__()
        self.shutdown_time = None
        self.timer = QTimer()
        self.timer.timeout.connect(self.update_countdown)
        self.init_ui()

    def init_ui(self):
        """初始化界面元素"""
        self.setWindowTitle('定時(shí)關(guān)機(jī)工具')
        screen = QApplication.primaryScreen().geometry()
        self.setGeometry(screen.width() // 4, screen.height() // 4, 585, 253)  # 修改窗口尺寸為583x614

        # 主布局
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        layout = QVBoxLayout(central_widget)

        # 倒計(jì)時(shí)顯示區(qū)域
        self.countdown_label = QLabel(LABEL_COUNTDOWN_DEFAULT)
        self.countdown_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.countdown_label.setStyleSheet("""
            QLabel {
                background-color: black;
                color: red;
                padding: 20px;
                border: 2px solid gray;
                border-radius: 10px;
                font-size: 30px;
            }
        """)
        layout.addWidget(self.countdown_label)

        # 時(shí)間設(shè)置區(qū)域
        self.setup_time_settings(layout)

        # 信號(hào)連接
        self.absolute_radio.toggled.connect(self.toggle_time_input)
        self.execute_btn.clicked.connect(self.execute_shutdown)
        self.cancel_btn.clicked.connect(self.cancel_shutdown)

    def setup_time_settings(self, layout):
        """設(shè)置時(shí)間選擇控件和按鈕"""
        settings_widget = QWidget()
        settings_layout = QHBoxLayout(settings_widget)

        # 時(shí)間選擇區(qū)域
        time_widget = QWidget()
        time_layout = QVBoxLayout(time_widget)

        # 單選按鈕
        self.absolute_radio = QRadioButton('絕對(duì)時(shí)間')
        self.relative_radio = QRadioButton('相對(duì)時(shí)間')
        self.absolute_radio.setChecked(True)
        time_layout.addWidget(self.absolute_radio)
        time_layout.addWidget(self.relative_radio)

        # 時(shí)間選擇器
        self.datetime_edit = QDateTimeEdit()
        self.datetime_edit.setDateTime(QDateTime.currentDateTime().addSecs(3600))  # 默認(rèn)一小時(shí)后
        self.time_edit = QTimeEdit()
        self.time_edit.setTime(self.time_edit.time().addSecs(3600))  # 默認(rèn)一個(gè)小時(shí)后

        time_layout.addWidget(self.datetime_edit)
        time_layout.addWidget(self.time_edit)
        self.time_edit.hide()  # 默認(rèn)隱藏相對(duì)時(shí)間

        settings_layout.addWidget(time_widget)

        # 按鈕區(qū)域
        button_widget = QWidget()
        button_layout = QVBoxLayout(button_widget)

        self.execute_btn = QPushButton(BUTTON_EXECUTE_TEXT)
        self.cancel_btn = QPushButton(BUTTON_CANCEL_TEXT)

        button_layout.addWidget(self.execute_btn)
        button_layout.addWidget(self.cancel_btn)

        settings_layout.addWidget(button_widget)
        layout.addWidget(settings_widget)

        # 添加空白間距
        spacer = QSpacerItem(20, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
        layout.addItem(spacer)

    def toggle_time_input(self):
        """切換時(shí)間輸入方式"""
        if self.absolute_radio.isChecked():
            self.datetime_edit.show()
            self.time_edit.hide()
        else:
            self.datetime_edit.hide()
            self.time_edit.show()

    def execute_shutdown(self):
        """執(zhí)行定時(shí)關(guān)機(jī)"""
        shutdown_time = self.calculate_shutdown_time()
        if not shutdown_time:
            return

        # 設(shè)置關(guān)機(jī)命令
        seconds = int((shutdown_time - datetime.datetime.now()).total_seconds())
        subprocess.run(['shutdown', '/s', '/t', str(seconds)])

        # 更新UI
        self.update_button_state(is_executing=True)

        # 啟動(dòng)倒計(jì)時(shí)
        self.shutdown_time = shutdown_time
        self.timer.start(1000)

    def calculate_shutdown_time(self):
        """計(jì)算定時(shí)關(guān)機(jī)時(shí)間"""
        if self.absolute_radio.isChecked():
            shutdown_time = self.datetime_edit.dateTime().toPyDateTime()
            if shutdown_time <= datetime.datetime.now():
                self.show_warning(ALERT_FUTURE_TIME)
                return None
        else:
            current_time = datetime.datetime.now()
            time_delta = datetime.timedelta(
                hours=self.time_edit.time().hour(),
                minutes=self.time_edit.time().minute(),
                seconds=self.time_edit.time().second()
            )
            shutdown_time = current_time + time_delta

        return shutdown_time

    def show_warning(self, message):
        """顯示警告信息"""
        QMessageBox.warning(self, '警告', message)

    def update_button_state(self, is_executing):
        """更新按鈕狀態(tài)"""
        if is_executing:
            self.execute_btn.setEnabled(False)
            self.execute_btn.setText(BUTTON_EXECUTING_TEXT)
            self.execute_btn.setStyleSheet('background-color: #90EE90; color: gray;')
        else:
            self.execute_btn.setEnabled(True)
            self.execute_btn.setText(BUTTON_EXECUTE_TEXT)
            self.execute_btn.setStyleSheet('')

    def cancel_shutdown(self):
        """取消定時(shí)關(guān)機(jī)"""
        reply = QMessageBox.question(self, '確認(rèn)', ALERT_CONFIRM_CANCEL,
                                     QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No)

        if reply == QMessageBox.StandardButton.Yes:
            # 取消當(dāng)前關(guān)機(jī)命令
            subprocess.run(['shutdown', '/a'])
            self.reset_ui()
        else:
            subprocess.run(['shutdown', '/a'])
            self.close()

    def reset_ui(self):
        """重置UI狀態(tài)"""
        self.shutdown_time = None
        self.timer.stop()
        self.countdown_label.setText(LABEL_COUNTDOWN_DEFAULT)
        self.update_button_state(is_executing=False)

    def update_countdown(self):
        """更新倒計(jì)時(shí)"""
        if self.shutdown_time:
            remaining = self.shutdown_time - datetime.datetime.now()
            if remaining.total_seconds() <= 0:
                self.timer.stop()
                return

            hours = remaining.seconds // 3600
            minutes = (remaining.seconds % 3600) // 60
            seconds = remaining.seconds % 60

            self.countdown_label.setText(
                f'距離關(guān)機(jī)還有 {hours:02d}小時(shí) {minutes:02d}分 {seconds:02d}秒')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setStyle('Fusion')  # 使用 Fusion 風(fēng)格,接近 Windows 10 風(fēng)格
    window = ShutdownTimer()
    window.show()
    sys.exit(app.exec())

到此這篇關(guān)于基于Python開發(fā)電腦定時(shí)關(guān)機(jī)工具的文章就介紹到這了,更多相關(guān)Python電腦定時(shí)關(guān)機(jī)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用python創(chuàng)建圖片格式轉(zhuǎn)換器的實(shí)現(xiàn)步驟

    使用python創(chuàng)建圖片格式轉(zhuǎn)換器的實(shí)現(xiàn)步驟

    本教程將指導(dǎo)如何使用 Python 編寫的圖片格式轉(zhuǎn)換工具 ImaCon_ter.py,該工具能夠?qū)D片從一種格式轉(zhuǎn)換為另一種格式,文章通過代碼示例講解的非常詳細(xì),感興趣的小伙伴跟著小編一起來看看吧
    2024-12-12
  • python批量插入數(shù)據(jù)到mysql的3種方法

    python批量插入數(shù)據(jù)到mysql的3種方法

    這篇文章主要給大家介紹了關(guān)于python批量插入數(shù)據(jù)到mysql的3種方法,在日常處理數(shù)據(jù)的過程中,我們都有批量寫入數(shù)據(jù)庫(kù)的需求,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-10-10
  • 總結(jié)分析Python的5個(gè)硬核函數(shù)

    總結(jié)分析Python的5個(gè)硬核函數(shù)

    今天看到一篇很好的 Python 博文,結(jié)合自己的經(jīng)驗(yàn)總結(jié),分享給大家一篇關(guān)于eval, exec, compile, locals, globals這些函數(shù)的文章
    2021-11-11
  • Python中ini配置文件讀寫的實(shí)現(xiàn)

    Python中ini配置文件讀寫的實(shí)現(xiàn)

    本文主要介紹了Python中ini配置文件讀寫的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • 在vscode使用jupyter notebook出現(xiàn)bug及解決

    在vscode使用jupyter notebook出現(xiàn)bug及解決

    這篇文章主要介紹了在vscode使用jupyter notebook出現(xiàn)bug及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • nlp自然語(yǔ)言處理基于SVD的降維優(yōu)化學(xué)習(xí)

    nlp自然語(yǔ)言處理基于SVD的降維優(yōu)化學(xué)習(xí)

    這篇文章主要為大家介紹了nlp自然語(yǔ)言處理基于SVD的降維優(yōu)化學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2022-04-04
  • Python包資源下載路徑報(bào)404解決方案

    Python包資源下載路徑報(bào)404解決方案

    這篇文章主要介紹了Python包資源下載路徑報(bào)404解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • python生成每日?qǐng)?bào)表數(shù)據(jù)(Excel)并郵件發(fā)送的實(shí)例

    python生成每日?qǐng)?bào)表數(shù)據(jù)(Excel)并郵件發(fā)送的實(shí)例

    今天小編就為大家分享一篇python生成每日?qǐng)?bào)表數(shù)據(jù)(Excel)并郵件發(fā)送的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • Numpy中的shape、reshape函數(shù)的區(qū)別

    Numpy中的shape、reshape函數(shù)的區(qū)別

    本文主要介紹了Numpy中的shape、reshape函數(shù)的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Python視頻處理模塊之moviepy的用法教程

    Python視頻處理模塊之moviepy的用法教程

    隨著自媒體時(shí)代,現(xiàn)在對(duì)視頻的處理變得越來越常見。而?Python?有一個(gè)專門用于處理視頻的第三方庫(kù):moviepy,可以非常方便地對(duì)視頻進(jìn)行一些簡(jiǎn)單處理,下面我們就來看一看
    2022-07-07

最新評(píng)論