Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Build an Application to Search Installed Application using Python
Next article icon

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 therotatescreen 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 moduleimportrotatescreen

 
Step 2)Create an object ofrotatescreen.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 modulesfromtkinterimport*importrotatescreen# User defined function# for rotating screendefScreen_rotation(temp):screen=rotatescreen.get_primary_display()iftemp=="up":screen.set_landscape()eliftemp=="right":screen.set_portrait_flipped()eliftemp=="down":screen.set_landscape_flipped()eliftemp=="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:

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.

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp