In the digital world, we navigate daily the ability to customize our user experience is not just a luxury it’s a necessity. Whether it’s for ease of reading, accessibility, or simply personal preference, adjusting how we view content can significantly enhance our interaction with software applications. This is where the power of Python’s tkinter library shines through.
Today, this tutorial covers the step-by-step guide to creating a screen rotation function using the tkinter library. So let’s get started!
Table of Contents
- Necessary Libraries
- Imports
- Screen Rotation Functions
- Creating the Main Window
- Creating Graphical User Interface Elements
- Run the Main Window
- Example
- Full Code
Necessary Libraries
$ pip install tk
$ pip install rotate-screen
For the code to function properly, make sure to install the tkinter and rotate-screen libraries via the terminal or your command prompt.
Imports
from tkinter import *
import rotatescreen as rs
We start by importing the tkinter
library that allows us to create a graphical user interface together with the rotatescreen
module, which allows us to control the screen orientation.
Screen Rotation Functions
def rotate_screen(orientation):
screen = rs.get_primary_display()
if orientation == "up":
screen.set_landscape()
elif orientation == "right":
screen.set_portrait_flipped()
elif orientation == "left":
screen.set_portrait()
elif orientation == "down":
screen.set_landscape_flipped()
def reset_screen():
screen = rs.get_primary_display()
screen.set_landscape()
This code creates two functions the first one rotate_screen
, as the name suggests, this function changes the screen position depending on the specified orientation, The second one reset_screen
resets the screen orientation to its initial position.
Creating The Main Window
root = Tk()
root.geometry("500x250")
root.title("Screen Rotation Controller - The Pycodes")
root.configure(bg="lightblue")
What we did here is simply create the main window, give it a name, define its geometry as well as give it a background color.
Creating Graphical User Interface Elements
frame = Frame(root, bg="lightblue")
frame.pack(pady=30)
Button(frame, text="UP", command=lambda: rotate_screen("up"), bg="white", font="arial 14", width=20).grid(row=0, column=0, padx=10)
Button(frame, text="Right", command=lambda: rotate_screen("right"), bg="white", font="arial 14", width=20).grid(row=0, column=1, padx=10)
Button(frame, text="Left", command=lambda: rotate_screen("left"), bg="white", font="arial 14", width=20).grid(row=1, column=0, padx=10)
Button(frame, text="Down", command=lambda: rotate_screen("down"), bg="white", font="arial 14", width=20).grid(row=1, column=1, padx=10)
Button(root, text="Reset the Screen", command=reset_screen, bg="white", font="arial 14", width=30).pack()
Here, we created a frame within the main window that will contain our buttons, four of which (up, right, left, down) will call the rotate_screen
function, and the “Reset the Screen” button which will call the reset_screen
function therefore resetting the orientation of the screen.
Run The Main Window
root.mainloop()
Lastly, this part is the one responsible for keeping the main window running and interacting with the user until he decides to exit.
Example
Full Code
from tkinter import *
import rotatescreen as rs
def rotate_screen(orientation):
screen = rs.get_primary_display()
if orientation == "up":
screen.set_landscape()
elif orientation == "right":
screen.set_portrait_flipped()
elif orientation == "left":
screen.set_portrait()
elif orientation == "down":
screen.set_landscape_flipped()
def reset_screen():
screen = rs.get_primary_display()
screen.set_landscape()
root = Tk()
root.geometry("500x250")
root.title("Screen Rotation Controller - The Pycodes")
root.configure(bg="lightblue")
frame = Frame(root, bg="lightblue")
frame.pack(pady=30)
Button(frame, text="UP", command=lambda: rotate_screen("up"), bg="white", font="arial 14", width=20).grid(row=0, column=0, padx=10)
Button(frame, text="Right", command=lambda: rotate_screen("right"), bg="white", font="arial 14", width=20).grid(row=0, column=1, padx=10)
Button(frame, text="Left", command=lambda: rotate_screen("left"), bg="white", font="arial 14", width=20).grid(row=1, column=0, padx=10)
Button(frame, text="Down", command=lambda: rotate_screen("down"), bg="white", font="arial 14", width=20).grid(row=1, column=1, padx=10)
Button(root, text="Reset the Screen", command=reset_screen, bg="white", font="arial 14", width=30).pack()
root.mainloop()
Happy Coding!