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

基于Python實現(xiàn)自動關機小工具

 更新時間:2022年10月25日 08:10:33   作者:Python集中營  
上班族經(jīng)常會遇到這樣情況,著急下班結(jié)果將關機誤點成重啟,或者臨近下班又通知開會,開完會已經(jīng)遲了還要去給電腦關機。今天使用PyQt5做了個自動關機的小工具,設置好關機時間然后直接提交即可,需要的可以參考一下

上班族經(jīng)常會遇到這樣情況,著急下班結(jié)果將關機誤點成重啟,或者臨近下班又通知開會,開完會已經(jīng)遲了還要去給電腦關機。

今天使用PyQt5做了個自動關機的小工具,設置好關機時間然后直接提交即可,下班就可以直接走人了。

有直接需要.exe可執(zhí)行應用的話,直接到文末處獲取下載鏈接!

自動關機小工具也支持了清除已經(jīng)設置好的關機時間,防止已經(jīng)設置好了關機時間重新調(diào)整時不知道怎么調(diào)整。

本應用除了使用os的python標準庫來設置關機,還引入了PyQt5的桌面應用框架,通過實現(xiàn)自動設置關機命令以及清除操作來完成。

# Importing the QThread, QDateTime, and pyqtSignal classes from the PyQt5.QtCore module.
from PyQt5.QtCore import QThread, QDateTime, pyqtSignal

# Importing the QIcon and QFont classes from the PyQt5.QtGui module.
from PyQt5.QtGui import QIcon, QFont

# Importing the QWidget, QLabel, QDateTimeEdit, QPushButton, QFormLayout, and QApplication classes from the
# PyQt5.QtWidgets module.
from PyQt5.QtWidgets import QWidget, QLabel, QDateTimeEdit, QPushButton, QFormLayout, QApplication

# Importing the os, sys, and time modules.
import os, sys, time

# Importing the images.py file.
import images

創(chuàng)建CloseCompUI的class類,用來實現(xiàn)自動關機應用的頁面布局,將UI相關以及對應的槽函數(shù)寫到這個類中。

# This class is a widget that contains a button and a text box. When the button is clicked, the text box is filled with
# the closest company name to the one entered
class CloseCompUI(QWidget):
    def __init__(self):
        """
        A constructor. It is called when an object is created from a class and it allows the class to initialize the
        attributes of a class.
        """
        super(CloseCompUI, self).__init__()
        self.init_ui()

    def init_ui(self):
        """
        This function initializes the UI.
        """
        self.setWindowTitle('自動關機小工具  公眾號:Python 集中營')
        self.setWindowIcon(QIcon(':/comp.ico'))
        self.setFixedWidth(380)
        self.setFixedHeight(120)

        self.is_close = False

        self.shutdown_time_lab = QLabel()
        self.shutdown_time_lab.setText('設置關機時間:')

        self.shutdown_time_in = QDateTimeEdit(QDateTime.currentDateTime())
        self.shutdown_time_in.setDisplayFormat('yyyy-MM-dd HH:mm:ss')
        self.shutdown_time_in.setCalendarPopup(True)

        self.submit_btn = QPushButton()
        self.submit_btn.setText('提交關機')
        self.submit_btn.clicked.connect(self.submit_btn_click)

        self.clear_btn = QPushButton()
        self.clear_btn.setText('清除關機')
        self.clear_btn.clicked.connect(self.clear_btn_click)

        self.show_message_lab = QLabel()
        self.show_message_lab.setText('更多免費小工具源碼獲取請前往公眾號:Python 集中營!')
        self.show_message_lab.setFont(QFont('黑體', 8))

        fbox = QFormLayout()
        fbox.addRow(self.shutdown_time_lab, self.shutdown_time_in)
        fbox.setSpacing(15)
        fbox.addRow(self.clear_btn, self.submit_btn)
        fbox.addRow(self.show_message_lab)

        self.thread_ = CloseCompThread(self)
        self.thread_.message.connect(self.show_message_lab_click)

        self.setLayout(fbox)

上面的就是已經(jīng)設置好的界面布局及需要的組件信息,然后將組件信息以及信號量關聯(lián)到槽函數(shù)上實現(xiàn)相應的動態(tài)操作。

下面是所有相關的槽函數(shù),同樣這些槽函數(shù)是放在CloseCompUI的class中的。

def show_message_lab_click(self, message):
    self.show_message_lab.setText(message + ',公眾號:Python 集中營!')

def submit_btn_click(self):
    if self.shutdown_time_in.text():
        self.is_close = True
        self.thread_.start()
    else:
        self.show_message_lab_click('請先設置關機時間')

def clear_btn_click(self):
    self.is_close = False
    self.thread_.start()

創(chuàng)建CloseCompThread的class類,作為單獨的子線程獨立運行不影響主線程的執(zhí)行,將所有的業(yè)務模塊(具體的關機實現(xiàn))寫到該線程中。

# This class is a QThread that runs a function that takes a list of strings and returns a list of strings
class CloseCompThread(QThread):
    message = pyqtSignal(str)

    def __init__(self, parent=None):
        """
        A constructor that initializes the class.

        :param parent: The parent widget
        """
        super(CloseCompThread, self).__init__(parent)
        self.parent = parent
        self.working = True

    def __del__(self):
        """
        If the shutdown time is set, the shutdown thread is started, otherwise the message is displayed
        """
        self.working = False
        self.wait()

    def run(self):
        """
        *|CURSOR_MARCADOR|*
        """
        try:
            is_close = self.parent.is_close
            print(is_close)
            if is_close is True:
                shutdown_time_in = self.parent.shutdown_time_in.text()
                t = time.strptime(shutdown_time_in, "%Y-%m-%d %H:%M:%S")
                t1 = int(time.mktime(t))
                t0 = int(time.time())
                num = t1 - t0
                if num > 0:
                    os.system('shutdown -s -t %d' % num)
                    self.message.emit("此電腦將在%s關機" % shutdown_time_in)
                else:
                    self.message.emit("關機時間不能小于當前操作系統(tǒng)時間")
            else:
                os.system('shutdown -a')
                self.message.emit("已經(jīng)清除自動關機設置")
        except:
            self.message.emit("提交/清除自動關機出現(xiàn)錯誤")

開發(fā)子線程CloseCompThread的業(yè)務實現(xiàn)后基本上已經(jīng)大功告成了,接下來使用main函數(shù)直接整個桌面啟動就OK了。

# A common idiom in Python to use this to guard the main body of your code.
if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = CloseCompUI()
    main.show()
    sys.exit(app.exec_())

上述自動關機小工具應用中所有的代碼塊已經(jīng)過測試,可以直接啟動使用。應用中只使用了一個PyQt5的python非標準庫需要安裝,其他的不需要安裝。

到此這篇關于基于Python實現(xiàn)自動關機小工具的文章就介紹到這了,更多相關Python自動關機內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 基于PyQt5制作一個數(shù)據(jù)圖表生成器

    基于PyQt5制作一個數(shù)據(jù)圖表生成器

    這篇文章主要介紹了如何利用PyQT5制作一個數(shù)據(jù)圖表生成器,可以通過Pyecharts模塊生成可視化的html數(shù)據(jù)圖表,感興趣的小伙伴可以跟隨小編學習一下
    2022-02-02
  • python pandas中DataFrame類型數(shù)據(jù)操作函數(shù)的方法

    python pandas中DataFrame類型數(shù)據(jù)操作函數(shù)的方法

    下面小編就為大家分享一篇python pandas中DataFrame類型數(shù)據(jù)操作函數(shù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • 教你使用Python?的?Template?類生成文件報告

    教你使用Python?的?Template?類生成文件報告

    這篇文章主要介紹了用?Python?的?Template?類生成文件報告,在閱讀本文時,您不僅學習了Python字符串的基本知識,Template類以及使用它的原因,而且還實現(xiàn)了第一個文件報告腳本,需要的朋友可以參考下
    2022-08-08
  • python數(shù)據(jù)結(jié)構(gòu)之二叉樹的建立實例

    python數(shù)據(jù)結(jié)構(gòu)之二叉樹的建立實例

    這篇文章主要介紹了python數(shù)據(jù)結(jié)構(gòu)之二叉樹的建立實例,采用了類似遞歸方式建立,需要的朋友可以參考下
    2014-04-04
  • python 發(fā)送qq郵件的示例

    python 發(fā)送qq郵件的示例

    這篇文章主要介紹了python 發(fā)送qq郵件的示例,幫助大家更好的理解和學習使用python,感興趣的朋友可以了解下
    2021-03-03
  • 基于Python?+?PyQt搭建可視化頁面的詳細教程

    基于Python?+?PyQt搭建可視化頁面的詳細教程

    PyQt是一個Python綁定庫,它是基于Qt庫的,PyQt提供了一整套豐富的組件,包括窗口、按鈕、菜單、列表框等,以及事件處理、布局管理、網(wǎng)絡通信等多種功能,本文介紹了基于Python?+?PyQt搭建可視化頁面的詳細教程,需要的朋友可以參考下
    2024-07-07
  • Python MySQL 日期時間格式化作為參數(shù)的操作

    Python MySQL 日期時間格式化作為參數(shù)的操作

    這篇文章主要介紹了Python MySQL 日期時間格式化作為參數(shù)的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • PyTorch搭建CNN實現(xiàn)風速預測

    PyTorch搭建CNN實現(xiàn)風速預測

    PyTorch是一個開源的Python機器學習庫,基于Torch,用于自然語言處理等應用程序。它不僅能夠?qū)崿F(xiàn)強大的GPU加速,同時還支持動態(tài)神經(jīng)網(wǎng)絡。本文將介紹PyTorch搭建CNN如何實現(xiàn)風速預測,感興趣的可以學習一下
    2021-12-12
  • pytorch模型的保存加載與續(xù)訓練詳解

    pytorch模型的保存加載與續(xù)訓練詳解

    這篇文章主要為大家介紹了pytorch模型的保存加載與續(xù)訓練詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • python3實現(xiàn)指定目錄下文件sha256及文件大小統(tǒng)計

    python3實現(xiàn)指定目錄下文件sha256及文件大小統(tǒng)計

    這篇文章主要為大家詳細介紹了python3實現(xiàn)指定目錄下文件sha256及文件大小統(tǒng)計,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-02-02

最新評論

丹阳市| 葵青区| 张家港市| 青浦区| 新和县| 扎兰屯市| 五河县| 东明县| 静乐县| 临泉县| 望谟县| 衢州市| 霍州市| 东城区| 温宿县| 会宁县| 庐江县| 文水县| 延长县| 原阳县| 遵义市| 兴业县| 什邡市| 闸北区| 阿克陶县| 隆德县| 拜城县| 黄石市| 屏东市| 昭觉县| 团风县| 浦江县| 新兴县| 屯留县| 桐柏县| 闻喜县| 宜黄县| 博爱县| 九龙县| 金堂县| 邻水|