上班族經常會遇到這樣情況,著急下班結果將關機誤點成重啟,或者臨近下班又通知開會,開完會已經遲了還要去給電腦關機,
【閱讀全文】
今天使用PyQt5做了個自動關機的小工具,設定好關機時間然后直接提交即可,下班就可以直接走人了,
有直接需要.exe可執行應用的話,直接到文末處獲取下載鏈接!
自動關機小工具也支持了清除已經設定好的關機時間,防止已經設定好了關機時間重新調整時不知道怎么調整,

本應用除了使用os的python標準庫來設定關機,還引入了PyQt5的桌面應用框架,通過實作自動設定關機命令以及清除操作來完成,
# 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
創建CloseCompUI的class類,用來實作自動關機應用的頁面布局,將UI相關以及對應的槽函式寫到這個類中,
# 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)
上面的就是已經設定好的界面布局及需要的組件資訊,然后將組件資訊以及信號量關聯到槽函式上實作相應的動態操作,
下面是所有相關的槽函式,同樣這些槽函式是放在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()
創建CloseCompThread的class類,作為單獨的子執行緒獨立運行不影響主執行緒的執行,將所有的業務模塊(具體的關機實作)寫到該執行緒中,
# 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("關機時間不能小于當前作業系統時間")
else:
os.system('shutdown -a')
self.message.emit("已經清除自動關機設定")
except:
self.message.emit("提交/清除自動關機出現錯誤")
開發子執行緒CloseCompThread的業務實作后基本上已經大功告成了,接下來使用main函式直接整個桌面啟動就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_())
上述自動關機小工具應用中所有的代碼塊已經過測驗,可以直接啟動使用,應用中只使用了一個PyQt5的python非標準庫需要安裝,其他的不需要安裝,
在公眾號內回復'關機小助手'即可獲取exe可執行桌面應用的百度網盤的下載鏈接,請大家按需下載后直接在電腦上雙擊運行即可,歡迎留言交流!
【往期精彩】
Python 集中營【資料處理圖書推薦】
吐血整理python資料分析利器pandas的八個生命周期!
五個最佳的python在線開發工具,看看是否能滿足你的開發需求?
歡迎關注作者公眾號【Python 集中營】,專注于后端編程,每天更新技術干貨,不定時分享各類資料!轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/519123.html
標籤:其他
上一篇:驅動開發:內核無痕隱藏自身分析
