基于Python實(shí)現(xiàn)自動關(guān)機(jī)小工具
上班族經(jīng)常會遇到這樣情況,著急下班結(jié)果將關(guān)機(jī)誤點(diǎn)成重啟,或者臨近下班又通知開會,開完會已經(jīng)遲了還要去給電腦關(guān)機(jī)。
今天使用PyQt5做了個自動關(guān)機(jī)的小工具,設(shè)置好關(guān)機(jī)時(shí)間然后直接提交即可,下班就可以直接走人了。
有直接需要.exe可執(zhí)行應(yīng)用的話,直接到文末處獲取下載鏈接!
自動關(guān)機(jī)小工具也支持了清除已經(jīng)設(shè)置好的關(guān)機(jī)時(shí)間,防止已經(jīng)設(shè)置好了關(guān)機(jī)時(shí)間重新調(diào)整時(shí)不知道怎么調(diào)整。

本應(yīng)用除了使用os的python標(biāo)準(zhǔn)庫來設(shè)置關(guān)機(jī),還引入了PyQt5的桌面應(yīng)用框架,通過實(shí)現(xiàn)自動設(shè)置關(guān)機(jī)命令以及清除操作來完成。
# 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類,用來實(shí)現(xiàn)自動關(guān)機(jī)應(yīng)用的頁面布局,將UI相關(guān)以及對應(yīng)的槽函數(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('自動關(guān)機(jī)小工具 公眾號: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('設(shè)置關(guān)機(jī)時(shí)間:')
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('提交關(guān)機(jī)')
self.submit_btn.clicked.connect(self.submit_btn_click)
self.clear_btn = QPushButton()
self.clear_btn.setText('清除關(guān)機(jī)')
self.clear_btn.clicked.connect(self.clear_btn_click)
self.show_message_lab = QLabel()
self.show_message_lab.setText('更多免費(fèi)小工具源碼獲取請前往公眾號: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)設(shè)置好的界面布局及需要的組件信息,然后將組件信息以及信號量關(guān)聯(lián)到槽函數(shù)上實(shí)現(xiàn)相應(yīng)的動態(tài)操作。
下面是所有相關(guān)的槽函數(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('請先設(shè)置關(guān)機(jī)時(shí)間')
def clear_btn_click(self):
self.is_close = False
self.thread_.start()
創(chuàng)建CloseCompThread的class類,作為單獨(dú)的子線程獨(dú)立運(yùn)行不影響主線程的執(zhí)行,將所有的業(yè)務(wù)模塊(具體的關(guān)機(jī)實(shí)現(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關(guān)機(jī)" % shutdown_time_in)
else:
self.message.emit("關(guān)機(jī)時(shí)間不能小于當(dāng)前操作系統(tǒng)時(shí)間")
else:
os.system('shutdown -a')
self.message.emit("已經(jīng)清除自動關(guān)機(jī)設(shè)置")
except:
self.message.emit("提交/清除自動關(guān)機(jī)出現(xiàn)錯誤")
開發(fā)子線程CloseCompThread的業(yè)務(wù)實(shí)現(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_())
上述自動關(guān)機(jī)小工具應(yīng)用中所有的代碼塊已經(jīng)過測試,可以直接啟動使用。應(yīng)用中只使用了一個PyQt5的python非標(biāo)準(zhǔn)庫需要安裝,其他的不需要安裝。
到此這篇關(guān)于基于Python實(shí)現(xiàn)自動關(guān)機(jī)小工具的文章就介紹到這了,更多相關(guān)Python自動關(guān)機(jī)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python pandas中DataFrame類型數(shù)據(jù)操作函數(shù)的方法
下面小編就為大家分享一篇python pandas中DataFrame類型數(shù)據(jù)操作函數(shù)的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
教你使用Python?的?Template?類生成文件報(bào)告
這篇文章主要介紹了用?Python?的?Template?類生成文件報(bào)告,在閱讀本文時(shí),您不僅學(xué)習(xí)了Python字符串的基本知識,Template類以及使用它的原因,而且還實(shí)現(xiàn)了第一個文件報(bào)告腳本,需要的朋友可以參考下2022-08-08
python數(shù)據(jù)結(jié)構(gòu)之二叉樹的建立實(shí)例
這篇文章主要介紹了python數(shù)據(jù)結(jié)構(gòu)之二叉樹的建立實(shí)例,采用了類似遞歸方式建立,需要的朋友可以參考下2014-04-04
基于Python?+?PyQt搭建可視化頁面的詳細(xì)教程
PyQt是一個Python綁定庫,它是基于Qt庫的,PyQt提供了一整套豐富的組件,包括窗口、按鈕、菜單、列表框等,以及事件處理、布局管理、網(wǎng)絡(luò)通信等多種功能,本文介紹了基于Python?+?PyQt搭建可視化頁面的詳細(xì)教程,需要的朋友可以參考下2024-07-07
Python MySQL 日期時(shí)間格式化作為參數(shù)的操作
這篇文章主要介紹了Python MySQL 日期時(shí)間格式化作為參數(shù)的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
PyTorch搭建CNN實(shí)現(xiàn)風(fēng)速預(yù)測
PyTorch是一個開源的Python機(jī)器學(xué)習(xí)庫,基于Torch,用于自然語言處理等應(yīng)用程序。它不僅能夠?qū)崿F(xiàn)強(qiáng)大的GPU加速,同時(shí)還支持動態(tài)神經(jīng)網(wǎng)絡(luò)。本文將介紹PyTorch搭建CNN如何實(shí)現(xiàn)風(fēng)速預(yù)測,感興趣的可以學(xué)習(xí)一下2021-12-12
python3實(shí)現(xiàn)指定目錄下文件sha256及文件大小統(tǒng)計(jì)
這篇文章主要為大家詳細(xì)介紹了python3實(shí)現(xiàn)指定目錄下文件sha256及文件大小統(tǒng)計(jì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02

