[Python] Notification after shut down on windows using python

Discussão em 'Python' iniciado por Stack, Janeiro 27, 2025.

  1. Stack

    Stack Membro Participativo

    I want to create a function that displays a message for the user when windows start again after a shutdown.

    It is used together with the shutdown command shutdown \s \hybrid

    Heres my attempt with the help of AI

    import os


    def leave_message_on_startup(message):
    script_content = f"""
    import pymsgbox

    message = "{message}"
    pymsgbox.alert(message, "Startup Message")
    """

    dirname = os.path.dirname
    script_path = f"{dirname(dirname(dirname(__file__)))}\\temp\\DisplayMessageOnStartup.pyw"
    os.makedirs("temp", exist_ok=True)
    with open(script_path, "w+") as script_file:
    script_file.write(script_content)

    create_task_command = f'schtasks /create /tn "{task_name}" /tr "py {script_path}" /sc onstart /f'
    os.system(create_task_command)

    def trigger_task(task_name):
    trigger_task_command = f'schtasks /run /tn "{task_name}"'
    os.system(trigger_task_command)


    task_name = "DisplayMessageOnStartup"
    message = "Sample message"
    leave_message_on_startup(message)
    trigger_task(task_name)


    After running the code, I double-clicked on the created script in File Explorer and it worked.

    Even though the script creation was successful, the task creation seems to have failed.

    I can't find the problem.

    Please enlighten me on ways to achieve notifications after windows shutdown.

    Continue reading...

Compartilhe esta Página