基于Python实现自动关机小工具
作者:Python集中营 发布时间:2023-05-07 23:48:47
标签:Python,自动,关机
上班族经常会遇到这样情况,着急下班结果将关机误点成重启,或者临近下班又通知开会,开完会已经迟了还要去给电脑关机。
今天使用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非标准库需要安装,其他的不需要安装。
来源:https://www.cnblogs.com/lwsbc/p/16822637.html


猜你喜欢
- 问题你想解析某个XML文档,文档中使用了XML命名空间。解决方案考虑下面这个使用了命名空间的文档:<?xml version=&quo
- 一、前言昨夜刷b站的时候,看到了一条评论,形式如下图,于是心血来潮写了个python脚本,可以根据文字来生成这种由emoji拼接成的“文字”
- k-means算法思想较简单,说的通俗易懂点就是物以类聚,花了一点时间在python中实现k-means算法,k-means算法有本身的缺点
- 类的定义# class是定义类的关键字,ClassName为类的名称class ClassName:# 在这里写其他内容passclass
- 这篇文章主要介绍了Python namedtuple命名元组实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考
- 本文实例讲述了php基于协程实现异步的方法。分享给大家供大家参考,具体如下:github上php的协程大部分是根据这篇文章实现的:http:
- 从这篇开始,用一个新的系列文章去介绍和学习MySQL。为什么要学习MySQL,从产品角度来说,大部分软件的数据库服务还是采取关系型数据库,主
- 通过网上查找资料了解关于性能优化方面的内容,现简单整理,仅供大家在优化的过程中参考使用,如有什么问题请及时提出,再做出相应的补充修改。 一、
- 一.设置客户端网络实用工具点击“开始”-“程序”,在“Microsoft SQL Server”菜单中选择“客户端网络实用工具”。在“别名”
- 本文实例讲述了python获取当前计算机cpu数量的方法。分享给大家供大家参考。具体分析如下:这里实际上返回的是计算机的cpu核心数,比如c
- import os,pandas as pd,re#1.获取文件夹下要合并的文件名dirpath = '文件夹地址'#工作表
- Pandas处理CSV文件,分为以下几步:读取Pandas文件统计列值出现的次数筛选特定列值遍历数据行绘制直方图(柱状图)读取Pandas文
- 目录wsgi 相关概念CGIWSGIASGIcgi 示例cgi脚本cgi服务实现wsgirefwsgi 小结小技巧python web开发中
- 在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数。 2. 使用Function对象来进行返
- 最近有一个需求:将日志以json格式输出, 并且有些字段是logging模块没有的.看了很多源码和资料, 终于搞定, 抽取精华分享出来, 一
- 每个函数创建时默认带有一个prototype属性,其中包含一个constructor属性,和一个指向Object对象的隐藏属性__proto
- 代码如下:'===================================== '获得文件后缀 '=====
- 发现问题图片地址如下http://qximg.lightplan.cc/2016/09/11/1473574486942944.jpeg需要
- 1、Pinia是什么Pinia是一个vue的状态管理方案,是vuex团队成员开发,实现了很多vuex5的提案,更加地轻量化且有devtool
- append()函数描述:在列表ls最后(末尾)添加一个元素object语法:ls.append(object) -> None 无返