简体   繁体   中英

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:
关机图标

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

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/

All you need in python to restart is exec('shutdown -r') idk how you want to make a button...

you can create a dialog with python's builtin input feature:

prompt = input("do Something")
if prompt == "yes":
  print("done")
else:
  print("not done")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM