是最初的幾個爬蟲,讓我認識了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
執行成功之後在ui所在目錄產生shut.py檔案。
#顯示視窗:
直接產生的py檔案運行是看不到視窗的,我們要加上一些必要的內容才能顯示我們的視窗:
代碼最上面加上
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_())
之後再運行shut.py就能看到視窗了。
#功能實現:
思考一下程式的期望功能,使Windows自動關機。cmd命令是個不錯的選擇。於是筆者找了下,python執行cmd命令的方法:
os.popen('at 22:30 shutdown -s')
調用cmd,執行命令。而其中的22和30是等待使用者輸入的資料。因此,應該用兩個lineEdit中擷取到的合法數字替換對應的h和m。用到擷取lineEdit內容的方法:
h = self.lineEdit.text()m = self.lineEdit_2.text()
然後以h,m替換執行命令中的時,分.
接著就是pushButton的部分了。為pushButton添加監聽事件click。
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 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_())
運行後,即出現操作視窗
#運行效果:
運行shut.py,輸入12和53點擊set,這時我們查看任務計劃:
發現任務已經在計劃中。點擊Remove,重新整理任務計劃。
成功移除任務,功能實現
當然這隻能在使用者安裝Python,並安裝相關組件前提下才可運行。想要在任何windows使用,則需要下面的操作。
#打包:
筆者打包用的是Python的Pyinstaller組件。cd 到shut.py所在目錄後,執行cmd命令:
pyinstaller -w shut.py
這時,在shut.py所在目錄產生dist檔案夾。產生的exe路徑。dist>>shut(Python源碼檔案名稱)>>shut.exe.前面順利的話,雙擊shut.exe便會顯示前面源碼運行同樣的視窗和操作。這樣,你就可以把shut目錄整個發給你的朋友。他們就可以通過雙擊shut.exe使用你的程式了。