Python在没有弹出响应的情况下执行操作

时间:2017-09-10 05:21:30

标签: python windows python-3.x tkinter popup

我一直在尝试编写一个python程序来自动关闭我的电脑。 Notification Messagebox模块使用类似于TutorialPoint的tkinter代码。弹出窗口将显示是/否选项。 使用当前代码,只有按“”按钮,系统才会关闭。

相反,它应该自动启动关机过程而不点击任何内容。同时,如果我单击“是”,则应停止关闭过程。

这是代码。我该如何做到这一点?

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

1 个答案:

答案 0 :(得分:0)

对于enter fatal: unable to access 'https://github.com/sudhsudhi/coursera_test.git/': Failed to connect to github.com port 443: Connection refused ,您可以使用closeApps()模块

psutil

import psutil for process in psutil.process_iter(): if process.name()=="Application.exe": process.kill() 列出了所有正在运行的进程。

注意:将psutil.process_iter()替换为要关闭的应用程序的名称。

对于"Application.exe",您只需使用shutDown()来提供终端命令:

os