Open In App
Related Articles

Build an Application for Screen Rotation Using Python

Improve Article
Save Article
Like Article

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.


pip install rotate-screen


Step 1) Import the required module in the python script.


# Import required module
import rotatescreen

Step 2) Create an object of rotatescreen.get_primary_display() to access the main screen of the system. 


# Accessing the main screen
rotate_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


# Methods to change orientation
# for landscape
# portrait at left
# landscape at down
# portrait at right

Below is the complete program of the above approach along with GUI implementation.


# Import required modules
from tkinter import *
import rotatescreen
# User defined function
# for rotating screen
def Screen_rotation(temp):
    screen = rotatescreen.get_primary_display()
    if temp == "up":
    elif temp == "right":
    elif temp == "down":
    elif temp == "left":
# Creating tkinter object
master = Tk()
master.title("Screen Rotation")
master.configure(bg='light grey')
# Variable classes in tkinter
result = StringVar()
# Creating buttons to change orientation
Button(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)
# this code belongs to Satyam kumar (ksatyam858)


Code Explanation:

  1. The code starts by importing the required modules.
  2. The rotatescreen module is used to rotate the screen.
  3. Then, a user defined function called Screen_rotation() is created and assigned to it.
  4. This function will be executed when the button “Up” is pressed on the master window of tkinter object.
  5. Next, variables are declared for storing text and result variable which will hold string values respectively.
  6. Button objects are then created with different texts and commands assigned to them so that they can change orientation of screen in response to pressing their respective buttons on master window of tkinter object.
  7. Finally, mainloop() method is called which starts executing all these functions one after another until it reaches its end point where it stops running any more code from this program
  8. So, the above code is a tkinter program that rotates the screen in various ways.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 11 Dec, 2022
Like Article
Save Article
Similar Reads
Complete Tutorials