Build an Application for Screen Rotation Using Python
In this article, we are going to write a python script for screen rotation and implement it with GUI.
The display can be modified to four orientations using some methods from the rotatescreen module, it is a small Python package for rotating the screen in a system.
Installation:
pip install rotate-screen
Approach:
Step 1) Import the required module in the python script.
Python3
# Import required moduleimport rotatescreen |
Step 2) Create an object of rotatescreen.get_primary_display() to access the main screen of the system.
Python3
# Accessing the main screenrotate_screen = rotatescreen.get_primary_display() |
Step 3) Now use various methods to rotate the screen.
- set_landscape(), Rotate Up
- set_portrait_flipped(), Rotate Left
- set_landscape_flipped(), Rotate Down
- set_portrait(), Rotate Right
Python3
# Methods to change orientation# for landscaperotate_screen.set_landscape()# portrait at leftrotate_screen.set_portrait_flipped()# landscape at downrotate_screen.set_landscape_flipped()# portrait at rightrotate_screen.set_portrait() |
Below is the complete program of the above approach along with GUI implementation.
Python3
# Import required modulesfrom tkinter import *import rotatescreen# User defined function# for rotating screendef Screen_rotation(temp): screen = rotatescreen.get_primary_display() if temp == "up": screen.set_landscape() elif temp == "right": screen.set_portrait_flipped() elif temp == "down": screen.set_landscape_flipped() elif temp == "left": screen.set_portrait()# Creating tkinter objectmaster = Tk()master.geometry("100x100")master.title("Screen Rotation")master.configure(bg='light grey')# Variable classes in tkinterresult = StringVar()# Creating buttons to change orientationButton(master, text="Up", command=lambda: Screen_rotation( "up"), bg="white").grid(row=0, column=3)Button(master, text="Right", command=lambda: Screen_rotation( "right"), bg="white").grid(row=1, column=6)Button(master, text="Left", command=lambda: Screen_rotation( "left"), bg="white").grid(row=1, column=2)Button(master, text="Down", command=lambda: Screen_rotation( "down"), bg="white").grid(row=3, column=3)mainloop()# this code belongs to Satyam kumar (ksatyam858) |
Output:



