基于Python實(shí)現(xiàn)自動(dòng)關(guān)機(jī)小工具
上班族經(jīng)常會遇到這樣情況,著急下班結(jié)果將關(guān)機(jī)誤點(diǎn)成重啟,或者臨近下班又通知開會,開完會已經(jīng)遲了還要去給電腦關(guān)機(jī)。
今天使用PyQt5做了個(gè)自動(dòng)關(guān)機(jī)的小工具,設(shè)置好關(guān)機(jī)時(shí)間然后直接提交即可,下班就可以直接走人了。
有直接需要.exe可執(zhí)行應(yīng)用的話,直接到文末處獲取下載鏈接!
自動(dò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)自動(dòng)設(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)自動(dòng)關(guān)機(jī)應(yīng)用的頁面布局,將UI相關(guān)以及對應(yīng)的槽函數(shù)寫到這個(gè)類中。
# 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('自動(dòng)關(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)的動(dò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)清除自動(dòng)關(guān)機(jī)設(shè)置") except: self.message.emit("提交/清除自動(dòng)關(guān)機(jī)出現(xiàn)錯(cuò)誤")
開發(fā)子線程CloseCompThread的業(yè)務(wù)實(shí)現(xiàn)后基本上已經(jīng)大功告成了,接下來使用main函數(shù)直接整個(gè)桌面啟動(dòng)就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_())
上述自動(dòng)關(guān)機(jī)小工具應(yīng)用中所有的代碼塊已經(jīng)過測試,可以直接啟動(dòng)使用。應(yīng)用中只使用了一個(gè)PyQt5的python非標(biāo)準(zhǔn)庫需要安裝,其他的不需要安裝。
到此這篇關(guān)于基于Python實(shí)現(xiàn)自動(dòng)關(guān)機(jī)小工具的文章就介紹到這了,更多相關(guān)Python自動(dòng)關(guān)機(jī)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python對接ihuyi實(shí)現(xiàn)短信驗(yàn)證碼發(fā)送
在本篇文章里小編給大家分享的是關(guān)于python對接ihuyi實(shí)現(xiàn)短信驗(yàn)證碼發(fā)送功能,需要的朋友們可以參考下。2020-05-05python數(shù)據(jù)分析:關(guān)鍵字提取方式
今天小編就為大家分享一篇python數(shù)據(jù)分析:關(guān)鍵字提取方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02python使用BeautifulSoup與正則表達(dá)式爬取時(shí)光網(wǎng)不同地區(qū)top100電影并對比
這篇文章主要給大家介紹了關(guān)于python使用BeautifulSoup與正則表達(dá)式爬取時(shí)光網(wǎng)不同地區(qū)top100電影并對比的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04Python樹莓派學(xué)習(xí)筆記之UDP傳輸視頻幀操作詳解
這篇文章主要介紹了Python樹莓派學(xué)習(xí)筆記之UDP傳輸視頻幀操作,結(jié)合實(shí)例形式詳細(xì)分析了Python樹莓派編程中使用UDP協(xié)議進(jìn)行視頻幀傳輸?shù)南嚓P(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-11-11ansible作為python模塊庫使用的方法實(shí)例
ansible是一個(gè)python package,是個(gè)完全的unpack and play軟件,對客戶端唯一的要求是有ssh有python,并且裝了python-simplejson包,部署上簡單到發(fā)指。下面這篇文章就給大家主要介紹了ansible作為python模塊庫使用的方法實(shí)例,需要的朋友可以參考借鑒。2017-01-01pycharm創(chuàng)建一個(gè)python包方法圖解
在本篇文章中小編給大家分享了關(guān)于pycharm怎么創(chuàng)建一個(gè)python包的相關(guān)知識點(diǎn),需要的朋友們學(xué)習(xí)下。2019-04-04python實(shí)現(xiàn)多線程網(wǎng)頁下載器
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)一個(gè)多線程網(wǎng)頁下載器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04