基于Python編寫(xiě)一個(gè)自動(dòng)關(guān)機(jī)程序
1、實(shí)現(xiàn)效果
2、實(shí)現(xiàn)步驟
模塊導(dǎo)入
import os,sys,time from PyQt5 import QtCore,QtWidgets,QtGui
窗口設(shè)置
def pageShow(self,page): #設(shè)置窗口的位置和大小 page.setGeometry(400,400,400,200) #設(shè)置窗口的標(biāo)題 page.setWindowTitle('Window shutdown') #設(shè)置窗口的圖標(biāo) #page.setWindowIcon(QtGui.QIcon('#ddffgg')) #設(shè)置工具中提示的字體樣式 QtWidgets.QToolTip.setFont(QtGui.QFont('SansSerif',10)) #創(chuàng)建一個(gè)提示 page.setToolTip('這是Window關(guān)機(jī)工具')
創(chuàng)建一個(gè)文本標(biāo)簽
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))
創(chuàng)建一個(gè)文本標(biāo)簽和時(shí)間欄框
self.label2 = QtWidgets.QLabel(page) self.label2.setGeometry(QtCore.QRect(100, 55, 40, 51))
設(shè)置該文件的字體樣式,大小。
self.label2.setFont(QtGui.QFont("Roman times",10,QtGui.QFont.Bold))
創(chuàng)建一個(gè)日期時(shí)間文本框,QDateEdit表示添加日期文本框,QTimeEdit表示添加時(shí)間文本框。
self.time = QtWidgets.QDateTimeEdit(page)
設(shè)置日期時(shí)間框的位置大小依次是左間距,上間距,寬,高。
self.time.setGeometry(QtCore.QRect(140, 70, 180, 25)) self.time.setDisplayFormat("yyyy-MM-dd HH:mm:ss")
設(shè)置日期可以使用日歷插件
self.time.setCalendarPopup(True)
根據(jù)PyQt方法獲取系統(tǒng)的當(dāng)前時(shí)間
now = QtCore.QDateTime.currentDateTime() now_time = now.toString(QtCore.Qt.ISODate)
將當(dāng)前系統(tǒng)時(shí)間賦值給時(shí)間框中
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'))
一個(gè)按鈕并設(shè)置添加單擊事件
self.btn = QtWidgets.QPushButton(page,clicked=self.shut) #self.btn.clicked.connect(self.shut(page)) self.btn.setToolTip('這里是提交按鈕')
顯示默認(rèn)尺寸
#self.btn.resize(btn.sizeHint()) self.btn.move(110,110)
創(chuàng)建一個(gè)按鈕并設(shè)置清除關(guān)機(jī)任務(wù)單擊事件
self.btn1 = QtWidgets.QPushButton(page,clicked=self.cleart) #self.btn.clicked.connect(self.shut()) self.btn1.setToolTip('這里是清除任務(wù)按鈕')
顯示默認(rèn)尺寸
self.btn1.move(210,110)
設(shè)置一個(gè)文本提示框
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()
設(shè)置工具窗口中顯示的部件文本信息
def setUI(self,page): _translate = QtCore.QCoreApplication.translate self.label.setText(_translate("page","請(qǐng)輸入關(guān)機(jī)時(shí)間")) self.label2.setText(_translate("page","日期:")) self.btn.setText(_translate("page","提交")) self.btn1.setText(_translate("page","清除")) self.text.setText(_translate("page","請(qǐng)?jiān)O(shè)置關(guān)機(jī)時(shí)間!"))
添加關(guān)機(jī)計(jì)劃
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("電腦將在%s關(guān)機(jī)!" % datetime) #self.time.setDateTime('1') except: self.text.setText("設(shè)置失??!") else: self.text.setText("日期設(shè)置錯(cuò)誤!")
清除關(guān)機(jī)計(jì)劃
def cleart(self,page): try: os.system('shutdown -a') self.text.setText("已經(jīng)清除關(guān)機(jī)任務(wù)!") except: self.text.setText("清除任務(wù)失??!") if __name__=='__main__':
創(chuàng)建應(yīng)用程序和對(duì)象
app = QtWidgets.QApplication(sys.argv) page = QtWidgets.QWidget() ui = guanji() ui.pageShow(page) sys.exit(app.exec_())
3、全部代碼
import os,sys,time from PyQt5 import QtCore,QtWidgets,QtGui class guanji(object): def pageShow(self,page): #設(shè)置窗口的位置和大小 page.setGeometry(400,400,400,200) #設(shè)置窗口的標(biāo)題 page.setWindowTitle('Window shutdown') #設(shè)置窗口的圖標(biāo) #page.setWindowIcon(QtGui.QIcon('#ddffgg')) #設(shè)置工具中提示的字體樣式 QtWidgets.QToolTip.setFont(QtGui.QFont('SansSerif',10)) #創(chuàng)建一個(gè)提示 page.setToolTip('這是Window關(guān)機(jī)工具') 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('這里是提交按鈕') self.btn.move(110,110) self.btn1 = QtWidgets.QPushButton(page,clicked=self.cleart) self.btn1.setToolTip('這里是清除任務(wù)按鈕') 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","請(qǐng)輸入關(guān)機(jī)時(shí)間")) self.label2.setText(_translate("page","日期:")) self.btn.setText(_translate("page","提交")) self.btn1.setText(_translate("page","清除")) self.text.setText(_translate("page","請(qǐng)?jiān)O(shè)置關(guān)機(jī)時(shí)間!")) 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("電腦將在%s關(guān)機(jī)!" % datetime) except: self.text.setText("設(shè)置失?。?) else: self.text.setText("日期設(shè)置錯(cuò)誤!") def cleart(self,page): try: os.system('shutdown -a') self.text.setText("已經(jīng)清除關(guān)機(jī)任務(wù)!") except: self.text.setText("清除任務(wù)失敗!") if __name__=='__main__': app = QtWidgets.QApplication(sys.argv) page = QtWidgets.QWidget() ui = guanji() ui.pageShow(page) sys.exit(app.exec_())
以上就是基于Python編寫(xiě)一個(gè)自動(dòng)關(guān)機(jī)程序的詳細(xì)內(nèi)容,更多關(guān)于Python自動(dòng)關(guān)機(jī)程序的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Pythony運(yùn)維入門(mén)之Socket網(wǎng)絡(luò)編程詳解
這篇文章主要介紹了Pythony運(yùn)維入門(mén)之Socket網(wǎng)絡(luò)編程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04windows下Python安裝、使用教程和Notepad++的使用教程
這篇文章主要介紹了windows下Python安裝、使用教程和Notepad++的使用教程,需要的朋友可以參考下2019-10-10python3+PyQt5+Qt Designer實(shí)現(xiàn)界面可視化
本文主要介紹了python3+PyQt5+Qt Designer實(shí)現(xiàn)界面可視化,Qt Designer,用鼠標(biāo)拖拖就能完成窗體設(shè)計(jì),感興趣的可以了解一下2021-06-06使用 Python 的 pprint庫(kù)格式化和輸出列表和字典的方法
pprint是"pretty-print"的縮寫(xiě),使用 Python 的標(biāo)準(zhǔn)庫(kù) pprint 模塊,以干凈的格式輸出和顯示列表和字典等對(duì)象,這篇文章主要介紹了如何使用 Python 的 pprint庫(kù)格式化和輸出列表和字典,需要的朋友可以參考下2023-05-05Python實(shí)現(xiàn)時(shí)間序列可視化的方法
matplotlib庫(kù)是一個(gè)用于創(chuàng)建出版質(zhì)量圖表的桌面繪圖包(2D繪圖庫(kù)),是Python中最基本的可視化工具。這篇文章主要介紹了Python時(shí)間序列可視化實(shí)現(xiàn),需要的朋友可以參考下2019-08-08