简体   繁体   English

如何使用Python在Win10中创建重启对话框

[英]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.我正在尝试为任务栏上的重新启动按钮运行 Python 脚本。 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.我想在 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:首先通过运行以下命令安装 PySimpleGUI:

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).如果希望它在 Python 之外运行,则需要创建一个可执行文件 (.exe)。 In order to do that, you need to install PyInstaller.为此,您需要安装 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/ https://pypi.org/project/pyinstaller/

All you need in python to restart is exec('shutdown -r') idk how you want to make a button...在 python 中重新启动所需的只是exec('shutdown -r') idk 你想如何制作一个按钮......

you can create a dialog with python's builtin input feature:您可以使用 python 的内置输入功能创建一个对话框:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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