1, achieve the effect

2. Implementation steps

Module import

import os,sys,time
from PyQt5 import QtCore,QtWidgets,QtGui
Copy the code

Window Settings

def pageShow(self,page): # setWindowTitle page.setWindowTitle('Window shutdown') # setWindow icon # page. SetWindowIcon (QtGui QIcon (' # DDFFGG ') # set tool tip in font style QtWidgets. QToolTip. SetFont (QtGui. QFont (' SansSerif, 10)) # create a prompt page.settooltip (' This is the Window shutdown tool ')Copy the code

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))
Copy the code

Create a text label and time bar

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

Set the font style and size of the file.

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

Create a date and time textbox. QDateEdit means to add a date textbox, and QTimeEdit means to add a time textbox.

self.time = QtWidgets.QDateTimeEdit(page)
Copy the code

Set the date and time boxes in order of size

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

You can set the date using the calendar plug-in

self.time.setCalendarPopup(True)
Copy the code

Get the current system time according to the PyQt method

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

Assigns the current system time to 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'))
Copy the code

A button and set the 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 ')Copy the code

Display default size

# self. The BTN. Resize (BTN sizeHint ()) the self. The BTN. Move (110110).Copy the code

Create a button and set the 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 ')Copy the code

Display default size

Self. Btn1. Move (210110).Copy the code

Set up 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()
Copy the code

Sets the part text information displayed in the tool window

def setUI(self,page): _translate = QtCore. QCoreApplication. Translate the self. Label the setText (_translate (" page ", "please enter the shutdown time")) Self. Label2. SetText (_translate (" page ", "date: "), the self. The BTN. SetText (_translate (" page ", "submit")) self. Btn1. The setText (_translate (" page ", "clear")) Self.text. SetText (_translate("page"," Please set shutdown time! ") ))Copy the code

Adding a 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 (" computer will shutdown at %s! % dateTime) #self.time.setDateTime('1') except: self.text. SetText (" set failed! ") #self.time.setDateTime('1') except: self.text. ) else: self.text.settext (" Wrong date setting! ") )Copy the code

Clear shutdown schedule

Def cleart(self,page): try: os.system('shutdown -a') self.text.settext (" self.text.settext ") def cleart(self,page): try: os.system('shutdown -a') self.text. Except: self.text.settext (" Clear task failed!" ) if __name__=='__main__':Copy the code

Create applications and objects

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

3. All codes

import os,sys,time from PyQt5 import QtCore,QtWidgets,QtGui class guanji(object): def pageShow(self,page): # setWindowTitle page.setWindowTitle('Window shutdown') # setWindow icon # page. SetWindowIcon (QtGui QIcon (' # DDFFGG ') # set tool tip in font style QtWidgets. QToolTip. SetFont (QtGui. QFont (' SansSerif, 10)) Self.label = qtwidgets. QLabel(Page) self.label. SetGeometry (qtCore-qrect (60, 20,) self.label. In 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 commit 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 (qtCor.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 the self. Label the setText (_translate (" page ", "please enter the shutdown time")) Self. Label2. SetText (_translate (" page ", "date: "), the self. The BTN. SetText (_translate (" page ", "submit")) self. Btn1. The setText (_translate (" page ", "clear")) Self.text. SetText (_translate("page"," Please set 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 (" computer will shutdown at %s! ") % datetime) except: self.text.settext (" setup failed! ") ) else: self.text.settext (" Wrong date setting! ") Def cleart(self,page): try: os.system('shutdown -a') self.text.settext (" shutdown -a') def cleart(self,page): try: os.system('shutdown -a') self.text. Except: self.text.settext (" Clear task failed!" ) if __name__=='__main__': app = QtWidgets.QApplication(sys.argv) page = QtWidgets.QWidget() ui = guanji() ui.pageShow(page) sys.exit(app.exec_())Copy the code