Shutdown, Restart and Logout Computer using Python with GUI

FREE Online Courses: Click, Learn, Succeed, Start Now!

In this python project, we are going to create a GUI Window that will have options of Shutdown, Logout, and Restart the computer. Just by clicking on the button on the window, we can make these things happen. Let’s start building this project.

About Shutdown, Restart, and Logout using Python

Using the OS Module (Operating System Module) we can make a system do some tasks without manually doing them ourselves. Shutting down, restarting, and logging out of a system is a part of these tasks. The OS Module gives a portable way of using the operating system functionalities.

Project Details

The aim of this project is to create a GUI Window that will have the options to Shutdown, Restart, and Logout from the system. This is a beginner level project and does not require any prior knowledge. We will be using the following two modules –

  • Tkinter Module
  • Os Module

Project Prerequisites

We need to install the following libraries using the commands given;

1. Tkinter Module – This module will help us create our GUI window for the project. The command to install it is as follows-

pip install tk

2. Os Module – This module provides a portable way of using operating system functionalities. The command to install it is as follows-

pip install os-sys

Download Project Source Code

Before starting with the project, click here to download the source code: Shutdown, Restart PC using Python Code

Steps to Create the Project

Let’s look at the steps to create the project-

  1. Import the Required Modules
  2. Create the GUI Window
  3. Shutdown Function
  4. Logout Function
  5. Restart Function

Now let us have a look at each step and code in detail.

1. Importing the Required Modules;

import os
from tkinter import *
import tkinter as tk
  • Importing Os Module and Tkinter Module. We will be requiring these modules to use them further in the project.

2. Creating the GUI Window:

# Create an instance of tkinter frame or window
window=Tk()
# Set the size of the tkinter window
window.geometry("700x350")
window.title("PythonGeeks")#give title to the window
head=Label(window, text="Shutdown, Restart and Logout Using Pc", font=('Calibri 15'))# a label
head.pack(pady=20)
  • Tk() – Using this method we are creating a GUI screen named window.
  • geometry() – This method gives the size specification of the window.
  • title() – This method gives title to the window.
  • Label() – Label is a widget on our window. Using the Label() method, we create this label and here we have created this label to display a text on our window. We can change the size, font of the text, background color, foreground color etc inside the Label() method.
#creating buttons
Button(window,text='Shutdown',command=Shutdown, font=('normal',11), bg='yellow').pack()
Button(window,text='Restart',command=Restart,font=('normal',11), bg='yellow').pack()
Button(window,text='Logout',command=logout,font=('normal',11), bg='yellow').pack()
  • Button() – Button() function helps us in creating buttons on our GUI Window. You can specify the text on the Button, size of the button, color, font, size of the text etc inside a Button() method. Command specifies the function to which the button is associated. Here in this project, we have created three buttons-

1. Shutdown Button

2. Logout Button

3. Restart Button

3. Shutdown Functionality:

def Shutdown():#function to shutdown
    os.system("shutdown /s /t 0")
  • /s – This is for shutting down the system. Time is 0 seconds means that immediately after the button is clicked.

4. Restart Functionality:

def Restart():#function to restart
    os.system("shutdown /r /t 0")
  • /r – This is for restarting the system immediately after the button is clicked.

5. Logout Functionality:

def logout():#function to logout
    os.system("shutdown /l /t 0")
  • /l – This is for logging out of the system immediately after the button is clicked.

Project Output

python shutdown restart computer

Summary

Congratulations! We have completed the Python Project to shutdown, restart and logout from the computer. Now using the GUI Window we can Logout, Shutdown and Restart without manually doing these things. You can try adding more functions to the project and make it fun.

If you are Happy with PythonGeeks, do not forget to make us happy with your positive feedback on Google | Facebook


1 Response

  1. Sunny says:

    This Code does not work on Python 3.10.5 . Tkinter is not getting installed . please advise

Leave a Reply

Your email address will not be published. Required fields are marked *