使用Python从 PC 关闭、重新启动和注销的 GUI
在本文中,我们将编写一个Python脚本来关闭或重新启动或注销您的系统并将其与 GUI 应用程序绑定。
Python中的OS 模块提供了与操作系统交互的功能。 OS 是一个内置库Python。
句法 :
For shutdown your system : os.system(“shutdown /s /t 1”)
For restart your system : os.system(“shutdown /r /t 1”)
For Logout your system : os.system(“shutdown -l”)
使用 Tkinter 实现 GUI 应用程序:
Python3
# import modules
from tkinter import *
import os
# user define funtion
def shutdown():
return os.system("shutdown /s /t 1")
def restart():
return os.system("shutdown /r /t 1")
def logout():
return os.system("shutdown -l")
# tkinter object
master = Tk()
# background set to grey
master.configure(bg='light grey')
# creating a button using the widget
# Buttons that will call the submit function
Button(master, text="Shutdown", command=shutdown).grid(row=0)
Button(master, text="Restart", command=restart).grid(row=1)
Button(master, text="Log out", command=logout).grid(row=2)
mainloop()
输出:
注意:请确保在 IDLE 上运行此代码之前保存并关闭所有程序,因为此程序将立即关闭并重新启动您的计算机。