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

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import required module
import rotatescreen

chevron_right


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



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Accessing the main screen
rotate_screen = rotatescreen.get_primary_display()

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# Methods to change orientation
  
# for landscape
rotate_screen.set_landscape()
  
# portrait at left
rotate_screen.set_portrait_flipped()
  
# landscape at down
rotate_screen.set_landscape_flipped()
  
# portrait at right
rotate_screen.set_portrait()

chevron_right


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

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import required modules
from tkinter import *
import rotatescreen
  
  
# User defined funtion
# for rotating screen
def screenRotation(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 object
master = Tk()
master.geometry("100x100")
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)
  
  
mainloop()
  
# this code belongs to Satyam kumar (ksatyam858)

chevron_right


Output:


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.