关于python:python做了个自动关机工具再也不会耽误我下班啦

0次阅读

共计 4087 个字符,预计需要花费 11 分钟才能阅读完成。

上班族常常会遇到这样状况,焦急上班后果将关机正点成重启,或者邻近上班又告诉散会,开完会曾经迟了还要去给电脑关机。

【浏览全文】

明天应用 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 在线开发工具,看看是否能满足你的开发需要?

正文完
 0