Python实现Windows定时关机
<p>是最初的几个爬虫,让我认识了Python这个新朋友,虽然才刚认识了几天,但感觉有种莫名的默契感。每当在别的地方找不到思路,总能在Python找到解决的办法。自动关机,在平时下载大文件,以及跑程序的时候能用到的,刚才写了个windows自动关机的小程序,程序过于简单,就当是玩玩吧,当然还有很多可改进的地方。下面正文:</p> <h2>UI制作:</h2> <p>照旧,笔者由Qt制作完成需要的ui,包括label,label_2,label_3,lable_4,lineEdit,lineEdit_2,pushButton组件.大致布局如下</p> <p><img src="https://simg.open-open.com/show/c90e21a540d8b56825cccc42dd83c5f3.png"></p> <p>两个lineEdit等待用户输入期望关机的时间。下面的Label用来显示操作后的返回信息。pushButton用于提交命令。ui制作完成。</p> <h2>UI转为py文件:</h2> <p>这里笔者装的是PyQt5,并添加了环境变量。所以转化的cmd命令(cd到ui所在目录):</p> <pre> <code class="language-python">pyuic5shut.ui -o shut.py </code></pre> <p>执行成功之后在ui所在目录生成shut.py文件。</p> <h2>显示窗口:</h2> <p>直接生成的py文件运行是看不到窗口的,我们要加上一些必要的内容才能显示我们的窗口:</p> <p>代码最上面加上</p> <pre> <code class="language-python">importsys </code></pre> <p>最后加上</p> <pre> <code class="language-python">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_()) </code></pre> <p>之后再运行shut.py就能看到窗口了。</p> <h2>功能实现:</h2> <p>思考一下程序的期望功能,使Windows自动关机。cmd命令是个不错的选择。于是笔者找了下,python执行cmd命令的方法:</p> <pre> <code class="language-python">os.popen('at 22:30 shutdown -s') </code></pre> <p>调用cmd,执行命令。而其中的22和30是等待用户输入的数据。因此,应该用两个lineEdit中获取到的合法数字替换对应的h和m。用到获取lineEdit内容的方法:</p> <pre> <code class="language-python">h = self.lineEdit.text() m = self.lineEdit_2.text() </code></pre> <p>然后以h,m替换执行命令中的时,分.</p> <p>接着就是pushButton的部分了。为pushButton添加监听事件click。</p> <pre> <code class="language-python">self.pushButton = QtWidgets.QPushButton(shut,clicked=self.sd) </code></pre> <p>其中,self.sd为触发该事件后,需要执行的操作。</p> <h2>完整代码:</h2> <p>一些关键的部分,叙述完毕,至于返回信息部分,笔者在这里不再详述。下面贴出来Windows自动关机完整的代码:</p> <pre> <code class="language-python"># -*- 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! importsys importos fromPyQt5importQtCore, QtGui, QtWidgets class Ui_shut(object): flag = True defsetupUi(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) defretranslateUi(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")) defsd(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_()) </code></pre> <p>运行后,即出现如图操作窗口</p> <p><img src="https://simg.open-open.com/show/2a3ffa771ed85cfd2a49fb80278e592f.png"></p> <h2>运行效果:</h2> <p>运行shut.py,输入12和53点击set,这时我们查看任务计划:</p> <p><img src="https://simg.open-open.com/show/8367e20d7094eef0052368372552dcf4.png"></p> <p><img src="https://simg.open-open.com/show/8e580d28d36b48409db7aad6666866d2.png"></p> <p>发现任务已经在计划中。点击Remove,刷新任务计划。</p> <p><img src="https://simg.open-open.com/show/c556b0fa78fb5db03146f23312262f83.png"></p> <p>成功移除任务,功能实现</p> <p>当然这只能在用户安装Python,并安装相关组件前提下才可运行。想要在任何windows使用,则需要下面的操作。</p> <h2>打包:</h2> <p>笔者打包用的是Python的Pyinstaller组件。cd 到shut.py所在目录后,执行cmd命令:</p> <pre> <code class="language-python">pyinstaller -w shut.py </code></pre> <p>这时,在shut.py所在目录生成dist文件夹。生成的exe路径。dist>>shut(Python源码文件名)>>shut.exe.前面顺利的话,双击shut.exe便会显示前面源码运行同样的窗口和操作。这样,你就可以把shut <strong>目录</strong> 整个发给你的朋友。他们就可以通过双击shut.exe使用你的程序了。</p> <p>至此,关于最近做的windows自动关机小软件的经验叙述完毕~</p> <p> </p> <p>来自:http://python.jobbole.com/87556/</p> <p> </p>
本文由用户 215852477 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!