程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Write an automatic shutdown program in Python and package it into an EXE executable program!

編輯:Python

1、 Realization effect

2、 Implementation steps

Module import

import os,sys,time
from PyQt5 import QtCore,QtWidgets,QtGui

Window settings

def pageShow(self,page):
# Set the position and size of the window
page.setGeometry(400,400,400,200)
# Set the title of the window
page.setWindowTitle('Window shutdown')
# Set the icon of the window
#page.setWindowIcon(QtGui.QIcon('#ddffgg'))
# Set the font style of the tooltip
QtWidgets.QToolTip.setFont(QtGui.QFont('SansSerif',10))
# Create a prompt
page.setToolTip(' This is a Window Shut down tools ')

Create a text label `

`

self.label = QtWidgets.QLabel(page)
self.label.setGeometry(QtCore.QRect(60, 20, 120, 45))
self.label.setFont(QtGui.QFont("Roman times",10,QtGui.QFont.Bold))

`

` Create a text label and time bar box

self.label2 = QtWidgets.QLabel(page)
self.label2.setGeometry(QtCore.QRect(100, 55, 40, 51))

Set the font style of the file , size .

self.label2.setFont(QtGui.QFont("Roman times",10,QtGui.QFont.Bold))

Create a date time text box ,QDateEdit Indicates the add date text box ,QTimeEdit Indicates the addition time text box .

self.time = QtWidgets.QDateTimeEdit(page)

Set the position and size of the date time box as follows:

self.time.setGeometry(QtCore.QRect(140, 70, 180, 25))
self.time.setDisplayFormat("yyyy-MM-dd HH:mm:ss")

You can use the calendar plug-in to set the date

self.time.setCalendarPopup(True)

according to PyQt Method to obtain the current time of the system

now = QtCore.QDateTime.currentDateTime()
now_time = now.toString(QtCore.Qt.ISODate)

Assign the current system time to... In the time box

now_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
self.time.setDateTime(QtCore.QDateTime.fromString(now_time, 'yyyy-MM-dd hh:mm:ss'))

A button and set add click event

self.btn = QtWidgets.QPushButton(page,clicked=self.shut)
#self.btn.clicked.connect(self.shut(page))
self.btn.setToolTip(' Here is the submit button ')

Show default size

#self.btn.resize(btn.sizeHint())
self.btn.move(110,110)

Create a button and set clear shutdown task click event

self.btn1 = QtWidgets.QPushButton(page,clicked=self.cleart)
#self.btn.clicked.connect(self.shut())
self.btn1.setToolTip(' Here is the clear task button ')

Show default size

self.btn1.move(210,110)

Set a text prompt box

self.text = QtWidgets.QLabel(page)
self.text.setGeometry(QtCore.QRect(25, 150, 350, 25))
self.text.setFont(QtGui.QFont("Roman times",14,QtGui.QFont.Bold))
self.setUI(page)
page.show()

Set the part text information displayed in the tool window

def setUI(self,page):
_translate = QtCore.QCoreApplication.translate
self.label.setText(_translate("page"," Please enter the shutdown time "))
self.label2.setText(_translate("page"," date :"))
self.btn.setText(_translate("page"," Submit "))
self.btn1.setText(_translate("page"," eliminate "))
self.text.setText(_translate("page"," Please set the shutdown time !"))

Add shutdown schedule

def shut(self,page):
datetime = self.time.text()
t1 = time.strptime(datetime,"%Y-%m-%d %H:%M:%S")
t = int(time.mktime(t1))
nq = int(time.time())
d = t-nq
#print(d)
#exit()
if d>0:
try:
os.system('shutdown -s -t %d' % d)
self.text.setText(" The computer will be in %s To turn it off !" % datetime)
#self.time.setDateTime('1')
except:
self.text.setText(" Setup failed !")
else:
self.text.setText(" Date setting error !")

Clear shutdown schedule

def cleart(self,page):
try:
os.system('shutdown -a')
self.text.setText(" The shutdown task has been cleared !")
except:
self.text.setText(" Clearing task failed !")
if __name__=='__main__':

Create applications and objects

app = QtWidgets.QApplication(sys.argv)
page = QtWidgets.QWidget()
ui = guanji()
ui.pageShow(page)
sys.exit(app.exec_())

3、 All the code

import os,sys,time
from PyQt5 import QtCore,QtWidgets,QtGui
class guanji(object):
def pageShow(self,page):
# Set the position and size of the window
page.setGeometry(400,400,400,200)
# Set the title of the window
page.setWindowTitle('Window shutdown')
# Set the icon of the window
#page.setWindowIcon(QtGui.QIcon('#ddffgg'))
# Set the font style of the tooltip
QtWidgets.QToolTip.setFont(QtGui.QFont('SansSerif',10))
# Create a prompt
page.setToolTip(' This is a Window Shut down tools ')
self.label = QtWidgets.QLabel(page)
self.label.setGeometry(QtCore.QRect(60, 20, 120, 45))
self.label.setFont(QtGui.QFont("Roman times",10,QtGui.QFont.Bold))
self.label2 = QtWidgets.QLabel(page)
self.label2.setGeometry(QtCore.QRect(100, 55, 40, 51))
self.label2.setFont(QtGui.QFont("Roman times",10,QtGui.QFont.Bold))
self.time = QtWidgets.QDateTimeEdit(page)
self.time.setGeometry(QtCore.QRect(140, 70, 180, 25))
self.time.setDisplayFormat("yyyy-MM-dd HH:mm:ss")
self.time.setCalendarPopup(True)
now_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
self.time.setDateTime(QtCore.QDateTime.fromString(now_time, 'yyyy-MM-dd hh:mm:ss'))
self.btn = QtWidgets.QPushButton(page,clicked=self.shut)
self.btn.setToolTip(' Here is the submit button ')
self.btn.move(110,110)
self.btn1 = QtWidgets.QPushButton(page,clicked=self.cleart)
self.btn1.setToolTip(' Here is the clear task button ')
self.btn1.move(210,110)
self.text = QtWidgets.QLabel(page)
self.text.setGeometry(QtCore.QRect(25, 150, 350, 25))
self.text.setFont(QtGui.QFont("Roman times",14,QtGui.QFont.Bold))
self.setUI(page)
page.show()
def setUI(self,page):
_translate = QtCore.QCoreApplication.translate
self.label.setText(_translate("page"," Please enter the shutdown time "))
self.label2.setText(_translate("page"," date :"))
self.btn.setText(_translate("page"," Submit "))
self.btn1.setText(_translate("page"," eliminate "))
self.text.setText(_translate("page"," Please set the shutdown time !"))
def shut(self,page):
datetime = self.time.text()
t1 = time.strptime(datetime,"%Y-%m-%d %H:%M:%S")
t = int(time.mktime(t1))
nq = int(time.time())
d = t-nq
if d>0:
try:
os.system('shutdown -s -t %d' % d)
self.text.setText(" The computer will be in %s To turn it off !" % datetime)
except:
self.text.setText(" Setup failed !")
else:
self.text.setText(" Date setting error !")
def cleart(self,page):
try:
os.system('shutdown -a')
self.text.setText(" The shutdown task has been cleared !")
except:
self.text.setText(" Clearing task failed !")
if __name__=='__main__':
app = QtWidgets.QApplication(sys.argv)
page = QtWidgets.QWidget()
ui = guanji()
ui.pageShow(page)
sys.exit(app.exec_())

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved