Use Python to implement Windows Timed Shutdown and python Timed Shutdown

Source: Internet
Author: User

Use Python to implement Windows Timed Shutdown and python Timed Shutdown

It was the first few crawlers that made me know Python, a new friend. Although I had just known me for a few days, I felt a strange sense of tacit understanding. You can always find a solution in Python whenever you cannot find a solution in other places. It can be used when downloading large files and running programs. I just wrote a windows auto-Shutdown program. The program is too simple and should be fun, of course there are still many improvements. The following body:

# Ui creation:

As usual, I made the ui required by Qt, including label, label_2, label_3, lable_4, lineEdit, lineEdit_2, and pushButton components. The general layout is as follows:

Two lineEdit users are waiting to enter the desired shutdown time. The Label below is used to display the returned information after the operation. PushButton is used to submit commands. Ui creation is complete.

Convert the ui to a py file:

Here I installed PyQt5 and added environment variables. So the converted cmd command (cd to the directory where the ui is located ):

pyuic5 shut.ui -o shut.py

After successful execution, the shut. py file is generated in the directory where the ui is located.

# Display window:

Directly generated py files run without a window. We need to add some necessary content to display our window:

Add

import sys

Add

If _ name _ = '_ main _': app = QtWidgets. QApplication (sys. argv) Form = QtWidgets. QWidget () ui = Ui_x () // Where Ui_x is the generated class name ui. setupUi (Form) Form. show () sys.exit(app.exe c _())

Then run shut. py to see the window.

# Function implementation:

Think about the expected functions of the program to enable Windows to automatically shut down. Cmd command is a good choice. So I found out how to execute the cmd command in python:

os.popen('at 22:30 shutdown -s')

Call cmd to execute the command. Among them, 22 and 30 are data waiting for user input. Therefore, replace the corresponding h and m with the valid numbers obtained in two lineEdit statements. The method for obtaining lineEdit content is as follows:

h = self.lineEdit.text()m = self.lineEdit_2.text()

Then replace the hour in the execution command with h and m.

Then, the pushButton part. Add a listener event click for pushButton.

self.pushButton = QtWidgets.QPushButton(shut,clicked=self.sd)

Self. sd indicates the operation to be performed after the event is triggered.

# Complete code:

Some key parts are described. As for the returned information, I will not detail them here. The following is the complete code for Windows automatic shutdown:

# -*- 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 sysimport osfrom PyQt5 import QtCore, QtGui, QtWidgetsclass 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 appears.

# Running effect:

Run shut. py, input 12 and 53, and click set. Then we can view the task plan:

The task is already being scheduled. Click Remove to refresh the task plan.

The task is removed successfully.

Of course, this can only be run when you install Python and related components. To use it in any windows environment, perform the following operations.

# Packaging:

I package Python Pyinstaller components. Run the cmd command:

pyinstaller -w shut.py

In this case, the dist folder is generated in the directory where shut. py is located. The generated exe path. Dist> shut (Python source code file name)> shut.exe.otherwise, double shut.exe will show that the previous source code runs the same windows and operations. In this way, you can send the shut directory to your friends. They can use your program through double shut.exe.

The above section describes how to use Python to implement Windows Timed Shutdown. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.