2017-09-10 28 views
0

我一直在試圖編寫一個python程序來自動關閉我的電腦。通知消息箱模塊使用類似於TutorialPoint中的tkinter代碼。彈出窗口將顯示是/否選項。 與當前的代碼,只有當我按「」按鈕,系統將關閉。Python做的動作沒有彈出響應

所以相反,它應該自動啓動關機過程,而不需要點擊任何東西。同時,如果我點擊「是」,那麼關機過程應該停止。

這是代碼。我如何做到這一點?

import MessageBox 
def PopUp(Title, Msg, Type='Info'): 
    Title = str(Title) 
    Msg = str(Msg) 

    root = tk.Tk() 
    root.withdraw() 
    if Type == "Question": 
     response = MessageBox.askquestion(Title, Msg) 
     print("question", response) 
     return response 
    elif Type == "TryAgain": 
     response = MessageBox.askretrycancel(Title, Msg) 
     print("try again", response) 
     return response 
    else: 
     print("Incorrect Type selected.") 
     response = MessageBox.showinfo(Title, Msg) 
     print("info", response) 
     return response 

def main(): 
    CurrentTime = int(time.strftime('%H')) 
    if CurrentTime > 22 or CurrentTime < 5: 
     msg = ("The Time is %s hours. Abort Automatic Shutdown?" % CurrentTime) 
     resp1 = Notification.PopUp("Auto Shut Down", msg, Type="Question") 
     print('Response from Notification is %s' % resp1) 

     if resp1 == 'no': 
      closeApps() 
      shutDown() 
     else: 
      print('ShutDown abortered by user.') 

if __name__ == '__main__': 
    main() 

回答

0

對於closeApps(),你可以使用psutil模塊,

import psutil 
for process in psutil.process_iter(): 
    if process.name()=="Application.exe": 
     process.kill() 

psutil.process_iter()給所有正在運行的進程的列表。

注意:用要關閉的應用程序的名稱替換"Application.exe"

至於shutDown(),你只需要使用os給終端命令:

import os 
os.system("shutdown /s") 
+0

感謝。我已經有一個CloseApps()和ShutDown()函數,但我的問題是關機應該自動啓動,但如果我想中止,我應該仍然可以... – Naveen

+0

終端命令'shutdown/a'可以被用來中止關機。請參閱https://stackoverflow.com/questions/2358929/prevent-windows-7-shutdown – RottenCandy