基于Python實現(xiàn)自動關機小工具
上班族經(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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python pandas中DataFrame類型數(shù)據(jù)操作函數(shù)的方法
下面小編就為大家分享一篇python pandas中DataFrame類型數(shù)據(jù)操作函數(shù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
python數(shù)據(jù)結(jié)構(gòu)之二叉樹的建立實例
這篇文章主要介紹了python數(shù)據(jù)結(jié)構(gòu)之二叉樹的建立實例,采用了類似遞歸方式建立,需要的朋友可以參考下2014-04-04
Python MySQL 日期時間格式化作為參數(shù)的操作
這篇文章主要介紹了Python MySQL 日期時間格式化作為參數(shù)的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
python3實現(xiàn)指定目錄下文件sha256及文件大小統(tǒng)計
這篇文章主要為大家詳細介紹了python3實現(xiàn)指定目錄下文件sha256及文件大小統(tǒng)計,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-02-02

