【问题标题】:Python do action without pop up responsePython 执行操作而不弹出响应
【发布时间】:2017-09-10 05:21:30
【问题描述】:

我一直在尝试编写一个 python 程序来自动关闭我的计算机。通知消息框模块使用 tkinter 代码,如 TutorialPoint。弹出窗口将显示是/否选项。 使用当前代码,只有当我按下“No”按钮时,系统才会关闭。

因此,它应该自动启动关机过程,而无需我单击任何内容。同时,如果我单击“是”,则关机过程应该停止。

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

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

【问题讨论】:

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


    【解决方案1】:

    对于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")
    

    【讨论】:

    猜你喜欢
    • 2020-01-22
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    相关资源
    最近更新 更多