99热99这里只有精品6国产,亚洲中文字幕在线天天更新,在线观看亚洲精品国产福利片 ,久久久久综合网

歡迎加入QQ討論群258996829
麥子學院 頭像
蘋果6袋
6
麥子學院

Python實現(xiàn)Windows定時關機

發(fā)布時間:2017-03-23 17:29  回復:0  查看:2296   最后回復:2017-03-23 17:29  

是最初的幾個爬蟲,讓我認識了Python這個新朋友,雖然才剛認識了幾天,但感覺有種莫名的默契感。每當在別的地方找不到思路,總能在Python語言找到解決的辦法。自動關機,在平時下載大文件,以及跑程序的時候能用到的,剛才寫了個windows自動關機的小程序,程序過于簡單,就當是玩玩吧,當然還有很多可改進的地方。下面正文:

  #ui制作:

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

Python實現(xiàn)Windows定時關機


兩個lineEdit等待用戶輸入期望關機的時間。下面的Label用來顯示操作后的返回信息。pushButton用于提交命令。ui制作完成。

  ui轉為py文件:

  這里筆者裝的是PyQt5,并添加了環(huán)境變量。所以轉化的cmd命令(cdui所在目錄)

  pyuic5 shut.ui -o shut.py

  執(zhí)行成功之后在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就能看到窗口了。

  #功能實現(xiàn):

  思考一下程序的期望功能,使Windows自動關機。cmd命令是個不錯的選擇。于是筆者找了下,python執(zhí)行cmd命令的方法:

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

  調用cmd,執(zhí)行命令。而其中的2230是等待用戶輸入的數據。因此,應該用兩個lineEdit中獲取到的合法數字替換對應的hm。用到獲取lineEdit內容的方法:

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

  然后以hm替換執(zhí)行命令中的時,分.

  接著就是pushButton的部分了。為pushButton添加監(jiān)聽事件click。

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

  其中,self.sd為觸發(fā)該事件后,需要執(zhí)行的操作。

  #完整代碼:

  一些關鍵的部分,敘述完畢,至于返回信息部分,筆者在這里不再詳述。下面貼出來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_())

  運行后,即出現(xiàn)如圖操作窗口

Python實現(xiàn)Windows定時關機


#運行效果:

  運行shut.py,輸入1253點擊set,這時我們查看任務計劃:

Python實現(xiàn)Windows定時關機

Python實現(xiàn)Windows定時關機


  發(fā)現(xiàn)任務已經在計劃中。點擊Remove,刷新任務計劃。

Python實現(xiàn)Windows定時關機

成功移除任務,功能實現(xiàn)

  當然這只能在用戶安裝Python,并安裝相關組件前提下才可運行。想要在任何windows使用,則需要下面的操作。

  #打包:

  筆者打包用的是PythonPyinstaller組件。cd shut.py所在目錄后,執(zhí)行cmd命令:

  pyinstaller -w shut.py

  這時,在shut.py所在目錄生成dist文件夾。生成的exe路徑。dist>>shut(Python源碼文件名)>>shut.exe.前面順利的話,雙擊shut.exe便會顯示前面源碼運行同樣的窗口和操作。這樣,你就可以把shut 目錄 整個發(fā)給你的朋友。他們就可以通過雙擊shut.exe使用你的程序了。

 

來源:博客園

您還未登錄,請先登錄

熱門帖子

最新帖子

?