python定时开关机的代码_Python实现Windows定时关机

#ui制作:

照旧,笔者由Qt制作完成需要的ui,包括label,label_2,label_3,lable_4,lineEdit,lineEdit_2,pushButton组件.大致布局如下

两个lineEdit等待用户输入期望关机的时间。下面的Label用来显示操作后的返回信息。pushButton用于提交命令。ui制作完成。

#ui转为py文件:

这里笔者装的是PyQt5,并添加了环境变量。所以转化的cmd命令(cd到ui所在目录):

pyuic5 shut.ui -o shut.py

1

pyuic5 shut.ui-oshut.py

执行成功之后在ui所在目录生成shut.py文件。

#显示窗口:

直接生成的py文件运行是看不到窗口的,我们要加上一些必要的内容才能显示我们的窗口:

代码最上面加上

import sys

1

import sys

最后加上

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_())

1

2

3

4

5

6

7

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_())

之后再运行shut.py就能看到窗口了。

#功能实现:

思考一下程序的期望功能,使Windows自动关机。cmd命令是个不错的选择。于是笔者找了下,python执行cmd命令的方法:

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

1

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

调用cmd,执行命令。而其中的22和30是等待用户输入的数据。因此,应该用两个lineEdit中获取到的合法数字替换对应的h和m。用到获取lineEdit内容的方法:

h = self.lineEdit.text()

m = self.lineEdit_2.text()

1

2

h=self.lineEdit.text()

m=self.lineEdit_2.text()

然后以h,m替换执行命令中的时,分.

接着就是pushButton的部分了。为pushButton添加监听事件click。

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

1

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

其中,self.sd为触发该事件后,需要执行的操作。

#完整代码:

一些关键的部分,叙述完毕,至于返回信息部分,笔者在这里不再详述。下面贴出来Windows自动关机完整的代码:

# -*- 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_())

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

# -*- 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

classUi_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()

ifself.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_())

运行后,即出现如图操作窗口

#运行效果:

运行shut.py,输入12和53点击set,这时我们查看任务计划:

发现任务已经在计划中。点击Remove,刷新任务计划。

成功移除任务,功能实现

当然这只能在用户安装Python,并安装相关组件前提下才可运行。想要在任何windows使用,则需要下面的操作。

#打包:

笔者打包用的是Python的Pyinstaller组件。cd 到shut.py所在目录后,执行cmd命令:

pyinstaller -w shut.py

1

pyinstaller-wshut.py

这时,在shut.py所在目录生成dist文件夹。生成的exe路径。dist>>shut(Python源码文件名)>>shut.exe.前面顺利的话,双击shut.exe便会显示前面源码运行同样的窗口和操作。这样,你就可以把shut目录整个发给你的朋友。他们就可以通过双击shut.exe使用你的程序了。

至此,关于最近做的windows自动关机小软件的经验叙述完毕~

你可能感兴趣的:(python定时开关机的代码)