本文目录导读:

可以的!用脚本实现屏幕放大镜是个很实用的想法,我为你提供几种实现方案:
Python + PyQt5 方案(推荐,功能强大)
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QPixmap, QPainter, QScreen
class ScreenMagnifier(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("屏幕放大镜")
self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
self.setGeometry(100, 100, 400, 300)
self.label = QLabel(self)
self.label.setGeometry(0, 0, 400, 300)
self.timer = QTimer()
self.timer.timeout.connect(self.update_magnifier)
self.timer.start(50) # 每50ms更新一次画面
self.zoom_factor = 2.0 # 放大倍数
def update_magnifier(self):
screen = QApplication.primaryScreen()
if screen:
# 获取屏幕中心位置
center_x = self.x() + self.width() // 2
center_y = self.y() + self.height() // 2
# 捕获屏幕区域
capture_rect = QRect(
center_x - int(self.width() / (2 * self.zoom_factor)),
center_y - int(self.height() / (2 * self.zoom_factor)),
int(self.width() / self.zoom_factor),
int(self.height() / self.zoom_factor)
)
# 截图并放大显示
screenshot = screen.grabWindow(0, capture_rect.x(), capture_rect.y(),
capture_rect.width(), capture_rect.height())
scaled = screenshot.scaled(self.width(), self.height(), Qt.KeepAspectRatio)
self.label.setPixmap(scaled)
def mousePressEvent(self, event):
if event.button() == Qt.RightButton:
self.close()
# macOS 需要需要的兼容性设置
if __name__ == '__main__':
app = QApplication(sys.argv)
magnifier = ScreenMagnifier()
magnifier.show()
sys.exit(app.exec_())
Python + Tkinter 方案(轻量级)
import tkinter as tk
from PIL import ImageGrab
import threading
import time
class MagnifierApp:
def __init__(self):
self.root = tk.Tk()
self.root.title("屏幕放大镜")
self.root.geometry("300x300")
self.root.attributes('-topmost', True)
self.label = tk.Label(self.root)
self.label.pack(fill=tk.BOTH, expand=True)
self.zoom_factor = 2.0
self.running = True
# 启动更新线程
self.update_thread = threading.Thread(target=self.update_loop)
self.update_thread.daemon = True
self.update_thread.start()
# 绑定关闭事件
self.root.protocol("WM_DELETE_WINDOW", self.close)
def update_loop(self):
while self.running:
try:
# 获取当前鼠标位置
x, y = self.root.winfo_pointerx(), self.root.winfo_pointery()
# 获取屏幕尺寸
screen_width = self.root.winfo_screenwidth()
screen_height = self.root.winfo_screenheight()
# 计算截图区域(以鼠标为中心)
size = 150
left = max(0, x - size//2)
top = max(0, y - size//2)
right = min(screen_width, x + size//2)
bottom = min(screen_height, y + size//2)
# 截图并缩放
shot = ImageGrab.grab(bbox=(left, top, right, bottom))
shot = shot.resize((300, 300))
# 更新显示
self.label.config(image=tk.PhotoImage(shot))
self.label.image = shot
time.sleep(0.1)
except Exception as e:
print(f"更新失败: {e}")
def close(self):
self.running = False
self.root.destroy()
if __name__ == "__main__":
app = MagnifierApp()
app.root.mainloop()
PowerShell 方案(Windows 快速实现)
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = "屏幕放大镜"
$form.Size = New-Object System.Drawing.Size(400, 300)
$form.TopMost = $true
$pictureBox = New-Object System.Windows.Forms.PictureBox
$pictureBox.Size = New-Object System.Drawing.Size(380, 280)
$pictureBox.Location = New-Object System.Drawing.Point(10, 10)
$form.Controls.Add($pictureBox)
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 100 # 每100ms更新
$timer.Add_Tick({
$bounds = [System.Windows.Forms.Cursor]::Position
$bounds.X -= 75
$bounds.Y -= 75
$bmp = New-Object System.Drawing.Bitmap(150, 150)
$g = [System.Drawing.Graphics]::FromImage($bmp)
$g.CopyFromScreen($bounds, [System.Drawing.Point]::Empty, $bmp.Size)
$scaled = New-Object System.Drawing.Bitmap($bmp, 380, 280)
$pictureBox.Image = $scaled
$g.Dispose()
$bmp.Dispose()
$scaled.Dispose()
})
$timer.Start()
$form.ShowDialog()
安装依赖
Python方案需要的库:
# PyQt5方案 pip install PyQt5 # Tkinter方案 pip install Pillow pip install pillow-tk # Windows可能需要
使用说明
-
运行效果:窗口会显示鼠标附近的屏幕内容,并放大显示
-
操控方式:
- 移动鼠标即可改变放大区域
- 右键点击退出程序
-
调整参数:可以修改
zoom_factor变量调整放大倍数
推荐使用 PyQt5方案,功能最完善且跨平台支持好,需要我调整放大倍数、窗口大小或其他功能吗?