Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write an automatic shutdown program with Python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to write an automatic shutdown program with Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Realize the effect

2. Implementation steps

Module import

Import os,sys,timefrom PyQt5 import QtCore,QtWidgets,QtGui

Window Settin

Def pageShow (self,page): # set window location and size page.setGeometry (400400400200) # set window title page.setWindowTitle ('Window shutdown') # set window icon # page.setWindowIcon (QtGui.QIcon (' # ddffgg')) # set font style QtWidgets.QToolTip.setFont (QtGui.QFont ('SansSerif',10)) # create a prompt page.setToolTip (' this is the Window shutdown tool')

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", 10Med 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))

Sets the font style and size of the file.

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

Create a date-time text box, QDateEdit for add date text box and QTimeEdit for add time text box.

Self.time = QtWidgets.QDateTimeEdit (page)

Set the location of the date-time box as follows: left spacing, top spacing, width, and height.

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)

Get the current time of the system according to the PyQt method

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

Assign 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'))

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')

Show default size

# self.btn.resize (btn.sizeHint ()) self.btn.move (110110)

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')

Show default size

Self.btn1.move (210110)

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 ()

Set the widget text information displayed in the tool window

Def setUI (self,page): _ translate = QtCore.QCoreApplication.translate self.label.setText (_ translate ("page", "Please enter shutdown time") self.label2.setText (_ translate ("page", "date:")) self.btn.setText (_ translate ("page", "submit") self.btn1.setText (_ translate ("page", "clear") self.text.setText (_ translate ("page", "Please set shutdown time!"))

Add shutdown plan

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) self.text.setText ("the computer will shut down at% s!" % datetime) # self.time.setDateTime ('1') except: self.text.setText ("setup failed!") Else: self.text.setText ("date setting error!")

Clear the shutdown plan

Def cleart (self,page): try: os.system ('shutdown-a') self.text.setText ("shutdown task has been cleared!") Except: self.text.setText ("cleanup 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 code import os,sys,timefrom PyQt5 import QtCore,QtWidgets,QtGuiclass guanji (object): def pageShow (self) Page): # set the location and size of the window page.setGeometry (400400400200) # 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 QtWidgets.QToolTip.setFont (QtGui.QFont ('SansSerif') prompted in the tool (10)) # create a prompt page.setToolTip ('this is the Window shutdown tool') self.label = QtWidgets.QLabel (page) self.label.setGeometry (QtCore.QRect (60,20,120,45) self.label.setFont (QtGui.QFont ("Roman times", 10dQtGui.QFont.Bold)) self.label2 = QtWidgets.QLabel (page) self.label2.setGeometry (QtCore.QRect (100,55,40) ) self.label2.setFont (QtGui.QFont ("Roman times", 10MagneQtGui.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 (110110) self.btn1 = QtWidgets.QPushButton (page Clicked=self.cleart) self.btn1.setToolTip ('here is the cleanup task button') self.btn1.move (210110) self.text = QtWidgets.QLabel (page) self.text.setGeometry (QtCore.QRect (25,150,350,25)) self.text.setFont (QtGui.QFont ("Roman times", 14Med QtGui.QFont.Bold) self.setUI (page) page.show () def setUI (self) Page): _ translate = QtCore.QCoreApplication.translate self.label.setText (_ translate ("page", "Please enter shutdown time") self.label2.setText (_ translate ("page", "date:")) self.btn.setText (_ translate ("page", "submit") self.btn1.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 ("the computer will shut down at% s!" % 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 ("shutdown task has been cleared!") Except: self.text.setText ("cleanup task failed!") If _ _ name__=='__main__': app = QtWidgets.QApplication (sys.argv) page = QtWidgets.QWidget () ui = guanji () ui.pageShow (page) sys.exit (app.exec_ ()) "how to write an automatic shutdown program with Python" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report