-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathar.py
More file actions
121 lines (99 loc) · 4 KB
/
Copy pathar.py
File metadata and controls
121 lines (99 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import json
import pygetwindow as gw
import pyautogui
import time
import subprocess
pyautogui.FAILSAFE = False
# 从配置文件中加载配置
def load_config(config_file):
with open(config_file, "r", encoding="utf-8") as file:
return json.load(file)
def openW(edge_path,browsers):
# 打开网页并调整窗口
for browser in browsers:
url = browser["url"]
title = browser["title"]
screen_x, screen_y = browser["screen_position"]
screen_width, screen_height = browser["screen_size"]
# 打开网页,强制新窗口
subprocess.Popen([edge_path, "--new-window", url])
time.sleep(5) # 等待网页加载完成
# 匹配窗口标题
window = None
for win in gw.getWindowsWithTitle(title):
if win.title:
window = win
break
if window:
# 移动窗口到对应屏幕
move_window_to_position(window, screen_x, screen_y, max_attempts=20, tolerance=5, delay=0.1)
pyautogui.click(window.left + 10, window.top + 10) # 点击窗口激活
# pyautogui.click(window.left + 10, window.top + 10) # 点击窗口激活
time.sleep(3)
pyautogui.press('f11')
else:
print(f"未找到与 {url} 对应的 Edge 窗口,请确认标题匹配。")
def checkW(edge_path,browsers):
# 打开网页并调整窗口
for browser in browsers:
url = browser["url"]
title = browser["title"]
screen_x, screen_y = browser["screen_position"]
screen_width, screen_height = browser["screen_size"]
# # 打开网页,强制新窗口
# subprocess.Popen([edge_path, "--new-window", url])
# time.sleep(5) # 等待网页加载完成
# 匹配窗口标题
window = None
for win in gw.getWindowsWithTitle(title):
if win.title:
window = win
break
if window:
# 移动窗口到对应屏幕
move_window_to_position(window, screen_x, screen_y, max_attempts=20, tolerance=5, delay=0.1)
pyautogui.click(window.left + 10, window.top + 10) # 点击窗口激活
# pyautogui.click(window.left + 10, window.top + 10) # 点击窗口激活
time.sleep(1)
# pyautogui.press('f11')
else:
print(f"未找到与 {url} 对应的 Edge 窗口,请确认标题匹配。")
def move_window_to_position(window, target_x, target_y, max_attempts=10, tolerance=5, delay=0.1):
"""
移动窗口到指定位置,并使用 while 循环检查是否移动到位。
参数:
- window: 窗口对象 (pygetwindow.Window)
- target_x, target_y: 目标位置 (int)
- max_attempts: 最大检查次数 (int)
- tolerance: 容错范围 (int)
- delay: 每次检查之间的延迟时间 (秒)
返回:
- True: 如果窗口成功移动到目标位置
- False: 如果达到最大尝试次数仍未移动到位
"""
# 尝试移动窗口
window.moveTo(target_x, target_y)
# 开始循环检查
attempt = 0
while attempt < max_attempts:
# 检查当前位置是否在目标范围内
if abs(window.left - target_x) <= tolerance and abs(window.top - target_y) <= tolerance:
print(f"窗口成功移动到 ({target_x}, {target_y}),当前位置为 ({window.left}, {window.top})")
return True
# 等待并重试
time.sleep(delay)
attempt += 1
# 如果超时,返回失败
print(f"窗口未成功移动到目标位置 ({target_x}, {target_y}),最终位置为 ({window.left}, {window.top})")
return False
# 主函数
def main():
# 加载配置
config = load_config("config.json")
browsers = config["browsers"]
edge_path = config["edge_path"]
openW(edge_path,browsers)
checkW(edge_path,browsers)
print("所有网页已打开并分配到不同屏幕。")
if __name__ == "__main__":
main()