


Example code for implementing Windows scheduled shutdown function in Python
It was the first few crawlers that introduced me to this new friend Python. Although I had only known him for a few days, I felt an inexplicable sense of tacit understanding. Whenever you can't find an idea elsewhere, you can always find a solution in Python. Automatic shutdown can be used when downloading large files or running programs. I just wrote a small program for automatic shutdown of Windows. The program is too simple, so just treat it as a play. Of course, there are many areas for improvement. Text below:
#ui production:
As usual, the author used Qt to produce the required ui, including label, label_2, label_3, lable_4, lineEdit, lineEdit_2, pushButton components. The general layout is as follows
Two lineEdits wait for the user to enter the desired shutdown time. The following Label is used to display the return information after the operation. pushButton is used to submit commands. UI production is completed.
ui to py file:
Here the author installed PyQt5 and added environment variables. So the converted cmd command (cd to the directory where ui is located):
pyuic5 shut.ui -o shut.py
After successful execution, the shut.py file is generated in the directory where ui is located.
#Display window:
You cannot see the window when running the directly generated py file. We need to add some necessary content to display our window:
Code Add
import sys
to the top and finally add
if name == 'main': app = QtWidgets.QApplication(sys.argv) Form = QtWidgets.QWidget() ui = Ui_x()//其中Ui_x为生成的class名 ui.setupUi(Form) Form.show() sys.exit(app.exec_())
and then run shutdown.py to see the window.
#Function implementation:
Think about the desired function of the program to make Windows shut down automatically. The cmd command is a good choice. So the author looked for a way to execute the cmd command in python:
os.popen('at 22:30 shutdown -s')
Call cmd and execute the command. Among them, 22 and 30 are data waiting for user input. Therefore, the corresponding h and m should be replaced with the legal numbers obtained in the two lineEdits. Use the method to obtain the content of lineEdit:
h = self.lineEdit.text() m = self.lineEdit_2.text()
Then replace the hours and minutes in the execution command with h and m.
Then comes the pushButton part. Add a listener for the event click for pushButton.
self.pushButton = QtWidgets.QPushButton(shut,clicked=self.sd)
Among them, self.sd is the operation that needs to be performed after the event is triggered.
#Complete code:
Some key parts have been described. As for the return information part, the author will not elaborate here. The complete code for Windows automatic shutdown is posted below:
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'shut.ui' # # Created: Mon Mar 20 18:10:31 2017 # by: PyQt5 UI code generator 5.2.1 # # WARNING! All changes made in this file will be lost! import sys import os from PyQt5 import QtCore, QtGui, QtWidgets class Ui_shut(object): flag = True def setupUi(self, shut): shut.setObjectName("shut") shut.resize(411, 170) shut.setFixedSize(411,170) self.label = QtWidgets.QLabel(shut) self.label.setGeometry(QtCore.QRect(40, 50, 41, 51)) self.label.setFont(QtGui.QFont("Roman times",10,QtGui.QFont.Bold)) self.label.setObjectName("label") self.lineEdit = QtWidgets.QLineEdit(shut) self.lineEdit.setGeometry(QtCore.QRect(70, 50, 71, 41)) self.lineEdit.setFont(QtGui.QFont("Roman times",10,QtGui.QFont.Bold)) self.lineEdit.setObjectName("lineEdit") self.label_2 = QtWidgets.QLabel(shut) self.label_2.setGeometry(QtCore.QRect(150, 60, 31, 31)) self.label_2.setFont(QtGui.QFont("Roman times",10,QtGui.QFont.Bold)) self.label_2.setObjectName("label_2") self.lineEdit_2 = QtWidgets.QLineEdit(shut) self.lineEdit_2.setGeometry(QtCore.QRect(180, 50, 71, 41)) self.lineEdit_2.setFont(QtGui.QFont("Roman times",10,QtGui.QFont.Bold)) self.lineEdit_2.setObjectName("lineEdit_2") self.label_3 = QtWidgets.QLabel(shut) self.label_3.setGeometry(QtCore.QRect(260, 60, 31, 31)) self.label_3.setFont(QtGui.QFont("Roman times",10,QtGui.QFont.Bold)) self.label_3.setObjectName("label_3") self.pushButton = QtWidgets.QPushButton(shut,clicked=self.sd) self.pushButton.setGeometry(QtCore.QRect(290, 50, 101, 41)) self.pushButton.setFont(QtGui.QFont("Roman times",10,QtGui.QFont.Bold)) self.pushButton.setObjectName("pushButton") self.label_4 = QtWidgets.QLabel(shut) self.label_4.setGeometry(QtCore.QRect(0, 120, 411, 31)) self.label_4.setFont(QtGui.QFont("Roman times",10,QtGui.QFont.Bold)) self.label_4.setObjectName("label_4") self.retranslateUi(shut) QtCore.QMetaObject.connectSlotsByName(shut) def retranslateUi(self, shut): _translate = QtCore.QCoreApplication.translate shut.setWindowTitle(_translate("shut", "Auto Shutdown by dearvee")) self.label.setText(_translate("shut", "At:")) self.label_2.setText(_translate("shut", "H")) self.label_3.setText(_translate("shut", "M")) self.label_4.setText(_translate("shut", "Please input time of shutdown~")) self.pushButton.setText(_translate("shut", "Set")) def sd(self,shut): h = self.lineEdit.text() m = self.lineEdit_2.text() if self.flag: self.flag = False try: os.popen('at '+h+':'+m+' shutdown -s') self.label_4.setText('Success! the system will shutdown at today '+h+':'+m+'.') self.pushButton.setText('Remove all') self.lineEdit.clear() self.lineEdit_2.clear() except: self.label_4.setText('Something is wrong~') else: self.flag = True try: os.popen('at /delete /yes') self.label_4.setText('Success! already removed~') self.pushButton.setText('Set') self.lineEdit.clear() self.lineEdit_2.clear() except: self.label_4.setText('Something is wrong~') if name == 'main': app = QtWidgets.QApplication(sys.argv) Form = QtWidgets.QWidget() ui = Ui_shut() ui.setupUi(Form) Form.show() sys.exit(app.exec_())
After running, the operation window as shown in the figure will appear
# Running effect:
Run shut.py, enter 12 and 53 and click set. Then we check the task plan:
and find that the task is already in the plan . Click Remove to refresh the task plan.
The task is successfully removed and the function is implemented
Of course, this can only be run if the user installs Python and installs related components. If you want to use it on any windows, you need the following operations.
#Packaging:
The author uses Python's Pyinstaller component for packaging. After cd to the directory where shutdown.py is located, execute the cmd command:
pyinstaller -w shut.py
At this time, a dist folder is generated in the directory where shutdown.py is located. Generated exe path. dist>>shut (Python source code file name)>>shut.exe. If everything goes well, double-clicking shut.exe will display the same window and operation as the previous source code. In this way, you can send the entire shutdown directory to your friends. They can then use your program by double-clicking shutdown.exe.
The above is the detailed content of Example code for implementing Windows scheduled shutdown function in Python. For more information, please follow other related articles on the PHP Chinese website!

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version
SublimeText3 Linux latest version