📜  使用Python构建屏幕旋转应用程序

📅  最后修改于: 2022-05-13 01:55:10.716000             🧑  作者: Mango

使用Python构建屏幕旋转应用程序

在本文中,我们将编写一个用于屏幕旋转的Python脚本并使用 GUI 来实现它。

可以使用rotatescreen模块中的一些方法将显示修改为四个方向,它是一个用于在系统中旋转屏幕的小型Python包。

安装:

pip install rotate-screen

方法:

步骤1)在Python脚本中导入所需的模块。

Python3
# Import required module
import rotatescreen


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


Python3
# 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()


Python3
# 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":
        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)



Step 2)创建一个rotatescreen.get_primary_display()对象来访问系统的主屏幕。

蟒蛇3

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


步骤 3)现在使用各种方法来旋转屏幕。

  • set_landscape() ,向上旋转
  • set_portrait_flipped() ,向左旋转
  • set_landscape_flipped(),向下旋转
  • set_portrait() ,向右旋转

蟒蛇3

# 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()


下面是上述方法的完整程序以及 GUI 实现。

蟒蛇3

# 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":
        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)

输出: