Introduction
In this tutorial, we will be building a basic application using which can shut down and restart our computer using Kivy GUI framework. To begin we will first need to install kivy on our computer.
Install modules
Open your terminal and run the command below to install kivy.
pip3 install Kivy
Create app
After installing the module above, create a file called app.py and import a few modules as shown below.
import subprocess
import os
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
After importing the modules above let’s now create a class that will handle the restart and shutdown functionality.
class ShutdownApp(App):
def build(self):
layout = BoxLayout(padding=10)
shutdown_button = Button(text="Shutdown", on_press=self.shutdown)
restart_button = Button(text="Restart", on_press=self.restart)
layout.add_widget(shutdown_button)
layout.add_widget(restart_button)
return layout
def shutdown(self, instance):
subprocess.call(['shutdown', 'now'])
def restart(self, instance):
subprocess.call(['sudo', 'reboot'])
if __name__ == '__main__':
ShutdownApp().run()
The above code first creates the shutdown and restart button, then a shutdown function is created that uses Linux shutdown commands since we are using a Linux machine. Finally, a restart function is created that uses the reboot command on Linux.
Run
Now open your terminal and run the command below to run the application.
python3 app.py
Results
After running the command above, our application should show up as shown below.
Click the shutdown button to shut down and the restart button to restart your computer. There you have it. Thanks for reading. Happy Coding