Skip to content
Advertisement

How to create Dialog box for Restarting in Win10 using Python

I am trying to run a Python script for the restart button on my taskbar. Here is the code I have:

import os
  
restart = input("Do you wish to restart your computer ? (yes / no): ")
  
if restart == 'no':
    exit()
else:
    os.system("shutdown /r /t 1")

I want to execute this outside of Python. Meaning when I click this button:
shutdown icon

I want it to say “Do you wish to restart your computer?” to confirm the restart instead of just doing it automatically.

Advertisement

Answer

Maybe this can help. Install PySimpleGUI first by running the following command:

pip install --upgrade PySimpleGUI

If it doesn’t work, try:

pip3 install --upgrade PySimpleGUI

Run this code. It will simply open a window with two buttons: Shut down and Cancel. Just be sure you’re ready to run it because it will shut down your machine. Save your work!

import PySimpleGUI as sg
import os

layout = [[sg.Button("Shut down", font='Lucida 14'), sg.Button('Cancel', font='Lucida 14', button_color=('white', 'firebrick3'))]]
window = sg.Window("Shutdown",layout)

while True:
    event, values = window.read()
    if event == 'Cancel' or event == sg.WIN_CLOSED:
        break
    else:
        if event == 'Shut down':
            os.system('shutdown -s')
window.close()

You won’t be able to use the system icons and button, though. This is your standalone app to shut down your machine. If you want it to run outside Python, you need to create an executable file (.exe). In order to do that, you need to install PyInstaller.

pip install pyinstaller

or

pip3 install pyinstaller

Check out their website. It explains in detail how to do it. https://pypi.org/project/pyinstaller/

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement