当前位置: 首页 > news >正文

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

上班族经常会遇到这样情况,着急下班结果将关机误点成重启,或者临近下班又通知开会,开完会已经迟了还要去给电脑关机。

【阅读全文】

今天使用PyQt5做了个自动关机的小工具,设置好关机时间然后直接提交即可,下班就可以直接走人了。

有直接需要.exe可执行应用的话,直接到文末处获取下载链接!

自动关机小工具也支持了清除已经设置好的关机时间,防止已经设置好了关机时间重新调整时不知道怎么调整。

file

本应用除了使用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在线开发工具,看看是否能满足你的开发需求?

相关文章:

  • BUUCTF NewStarCTF 公开赛赛道Week5 Writeup
  • @Conditional注解详解
  • 动态路由协议解析(rip)
  • 38、Java 中的正则表达式(单字符匹配和预定义字符)
  • 电气论文实现:基于优化算法和python-pandapower的配电网重构(IEEE33节点算例)
  • 刚来的00后真的卷,听说工作还没两年,跳到我们公司直接起薪20k...
  • 【云原生 · Docker】Docker 镜像操作、容器操作常用指令
  • 基于粒子群优化算法的无人机路径规划与轨迹算法的实现(Matlab代码实现)
  • Spring Cloud基本介绍
  • 【目标检测】使用TensorRT加速YOLOv5
  • python数据分析及可视化(九)pandas数据规整(分组聚合、数据透视表、时间序列、数据分析流程)
  • 到了30岁,我才有了深刻的感悟:千万不要一辈子靠技术生存
  • 过滤器和拦截器的区别
  • i.MX 6ULL 驱动开发 十九:RGBLCD
  • 前端谷歌浏览器基本介绍及前后端分离原理分析
  • 《Javascript数据结构和算法》笔记-「字典和散列表」
  • 2017届校招提前批面试回顾
  • C++回声服务器_9-epoll边缘触发模式版本服务器
  • es6--symbol
  • happypack两次报错的问题
  • HTML5新特性总结
  • Less 日常用法
  • mysql_config not found
  • vue-router的history模式发布配置
  • vue的全局变量和全局拦截请求器
  • XML已死 ?
  • 反思总结然后整装待发
  • 解析 Webpack中import、require、按需加载的执行过程
  • 利用阿里云 OSS 搭建私有 Docker 仓库
  • - 转 Ext2.0 form使用实例
  • 走向全栈之MongoDB的使用
  • ​sqlite3 --- SQLite 数据库 DB-API 2.0 接口模块​
  • #{} 和 ${}区别
  • #gStore-weekly | gStore最新版本1.0之三角形计数函数的使用
  • #在 README.md 中生成项目目录结构
  • ( )的作用是将计算机中的信息传送给用户,计算机应用基础 吉大15春学期《计算机应用基础》在线作业二及答案...
  • (03)光刻——半导体电路的绘制
  • (14)目标检测_SSD训练代码基于pytorch搭建代码
  • (16)Reactor的测试——响应式Spring的道法术器
  • (arch)linux 转换文件编码格式
  • (第9篇)大数据的的超级应用——数据挖掘-推荐系统
  • (含react-draggable库以及相关BUG如何解决)固定在左上方某盒子内(如按钮)添加可拖动功能,使用react hook语法实现
  • .NET HttpWebRequest、WebClient、HttpClient
  • .NET/C# 编译期间能确定的相同字符串,在运行期间是相同的实例
  • .sdf和.msp文件读取
  • ;号自动换行
  • @PreAuthorize注解
  • @value 静态变量_Python彻底搞懂:变量、对象、赋值、引用、拷贝
  • [ 数据结构 - C++] AVL树原理及实现
  • [23] 4K4D: Real-Time 4D View Synthesis at 4K Resolution
  • [Android Pro] Notification的使用
  • [C#]winform部署PaddleOCRV3推理模型
  • [CareerCup][Google Interview] 实现一个具有get_min的Queue
  • [GN] Vue3.2 快速上手 ---- 核心语法2
  • [HTML API]HTMLCollection