要点:
1. 该 解决方案 采样模拟 鼠标的方式, 通过 调用已 存在的界面或者 对话框。实施点击动作。
2. 该 方案未进行全面检测, 或许有部分bug 请 看清楚再使用。
3. 该方案 可以学习到 如何使用鼠标,移动,点击功能
可以学习到 如何让一个进程 页面 位于顶层,也就是可见。
如何调用 输入事件等等。
好了,不多说,上代码:
from tkinter import *
import tkinter.filedialog as fd
import threading
import time
import random
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
from watchdog.events import FileSystemEventHandler
from autoRunexe import *
class SelectExeUpdate():
def __init__(self) -> None:
self.myWindowMgr = WindowMgr()#窗口实例
self.mypymouse = PyMouse()#鼠标对象
self.left = None
self.top = None
self.right = None
self.bottom = None
self.is_send_ok_flag = False
pass
#查找发送的qq或者微信进程(前台)
def find(self):
self.myWindowMgr.find_window_wildcard(None,".*?手机.*?")
self.left,self.top,self.right,self.bottom = self.myWindowMgr.show_hwnd(True)
self.find_fouc()
pass
#查找发送按钮,确定可以发送
def find_fouc(self):
#确定最小化按钮位置,用于隐藏手机助手页面,便于下次拉起
self.min_x_pos = None
self.min_y_pos = None
self.min_posX_offset = 90
self.min_posY_offset = 30
#end
if self.left is not None:
if self.top is not None:
if self.right is not None:
if self.bottom is not None:
#print('find_fouc...')
self.is_send_ok_flag = True
self.min_x_pos = self.right - self.min_posX_offset
self.min_y_pos = self.top + self.min_posY_offset
pass
pass
#隐藏助手页面
def hide_assistant_windows(self):
if self.min_x_pos is not None:
self.mypymouse.move(self.min_x_pos,self.min_y_pos)
self.mypymouse.click(self.min_x_pos,self.min_y_pos)
pass
#选择文件助手的发送文件按钮
def select_dicts(self):
x_pos = random.randint(60,65) #偏移量x
y_pos = random.randint(698,708)
if self.is_send_ok_flag:
self.mypymouse.move(self.left + x_pos,self.top + y_pos)
self.mypymouse.click(self.left + x_pos,self.top + y_pos)
pass
#鼠标模拟点击发送
def send_handle(self):
if self.is_send_ok_flag:
self.mypymouse.click(self.right-35,self.bottom-35)
self.is_send_ok_flag = False
pass
#获取当前鼠标位置
def get_cur_position(self):
print(self.mypymouse.position())
pass
def handle_path(self, path):
self.m_path_name = None
self.m_dir_path = None
if len(path) == 0:
return False
n_path_lists = path.split('\\')
r_dir_path = n_path_lists[0]
self.m_path_name = n_path_lists[1]
self.m_dir_path = r_dir_path.replace('\\','/')
pass
#拷贝文件夹路径
def CopyDirPath(self,path):
self.handle_path(path)
self.opened_open_dir_offset_x = 400
self.opened_open_dir_offset_y = 50
self.mypymouse.move(self.open_right - self.opened_open_dir_offset_x, self.open_top + self.opened_open_dir_offset_y)
self.mypymouse.click(self.open_right - self.opened_open_dir_offset_x, self.open_top + self.opened_open_dir_offset_y,1,1)
self.myWindowMgr.other_keys_input(self.m_dir_path)
pass
#拷贝文件名路径
def CopyFileName(self):
#print(self.r_mpath_name)
n_name_lists = self.m_path_name.split('.')
n_name_prefix = n_name_lists[0]
n_name_suffix = n_name_lists[1]
self.opened_open_file_offset_x = 240
self.opened_open_file_offset_y = 60
self.mypymouse.move(self.open_left + self.opened_open_file_offset_x, self.open_bottom - self.opened_open_file_offset_y)
self.mypymouse.click(self.open_left + self.opened_open_file_offset_x, self.open_bottom - self.opened_open_file_offset_y,1,1)
#print(n_name_prefix)
self.myWindowMgr.other_keys_input(n_name_prefix)
pass
#点击打开(发送),打开页面的
def click_open_button(self):
self.opened_open_btn_offset_x = 200 #打开页面的打开按钮偏移量x
self.opened_open_btn_offset_y = 30
self.mypymouse.move(self.open_right - self.opened_open_btn_offset_x, self.open_bottom - self.opened_open_btn_offset_y)
self.mypymouse.click(self.open_right - self.opened_open_btn_offset_x, self.open_bottom - self.opened_open_btn_offset_y)
self.is_send_ok_flag = False
pass
#列举所有窗口,找到打开窗口句柄
def top_windows(self):
open_windows_name = '打开'
self.open_hWnd = self.myWindowMgr.find_window(None,open_windows_name)
if self.open_hWnd is not None:
self.open_left,self.open_top,self.open_right,self.open_bottom = self.myWindowMgr.show_hwnd()
return True
else :
return False
class MyHandler(FileSystemEventHandler):
def __init__(self):
self.selectExe = SelectExeUpdate()
pass
def on_modified(self, event):#回调函数
#print("文件被修改了 %s" % event.src_path)
pass
def on_created(self, event):#回调函数
print("on_created: %s" % event.src_path)
self.paths = event.src_path
time.sleep(1)
self.selectExe.find()
time.sleep(1)
self.selectExe.select_dicts()
time.sleep(1)
self.selectExe.top_windows()
time.sleep(1)
self.selectExe.CopyDirPath(self.paths)
time.sleep(1)
self.selectExe.CopyFileName()
time.sleep(1)
self.selectExe.click_open_button()
time.sleep(1)
self.selectExe.hide_assistant_windows()
pass
def get_files_path(self):
return self.paths
class watchDirectory():
def __init__(self,paths):
self.path = paths
self.paths_len = len(paths)
pass
#配置检测文件夹变化线程
def config(self):
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
self.paths = self.path if self.paths_len > 1 else '.'
# 生成事件处理器对象
#event_handler = LoggingEventHandler()
event_handler = MyHandler()
# 生成监控器对象
observer = Observer()
# 注册事件处理器,配置监控目录
observer.schedule(event_handler, self.paths, recursive=True)
# 监控器启动——创建线程
observer.start()
pass
#设置检测文件夹变化时间
def run(self):
# 以下代码是为了保持主线程运行
try:
while True:
time.sleep(3)
except KeyboardInterrupt:
self.observer.stop()
# 主线程任务结束之后,进入阻塞状态,一直等待其他的子线程执行结束之后,主线程再终止
#self.observer.join()
class SelectDirectory():
def __init__(self) -> None:
self.folder_path = None
self.selectTestExe = SelectExeUpdate()
pass
#打开要监控的文件夹,将文件夹路径写入到输入控件
def openFloder(self):
self.folder_path = fd.askdirectory() # 打开文件
self.show_folderPath.delete(0,END) # 清空
self.show_folderPath.insert(0,self.folder_path) #写入路径
#开启线程
def startThread(self):
if self.folder_path is None:
return
self.n_waiting = watchDirectory(self.folder_path)
self.n_waiting.config()
self.thread1 = threading.Thread(target=self.n_waiting.run)
self.thread1.setDaemon(True)
self.thread1.start()
pass
def stopThread(self):
pass
#开启新线程测试之
def testThreadrun(self):
self.selectTestExe.find()
time.sleep(2)
self.selectTestExe.select_dicts()
time.sleep(1)
self.selectTestExe.top_windows()
time.sleep(3)
self.selectTestExe.CopyDirPath('test')
time.sleep(1)
self.selectTestExe.click_open_button()
pass
#测试按钮功能
def testcmdThread(self):
self.thread2 = threading.Thread(target=self.testThreadrun)
self.thread2.setDaemon(True)
self.thread2.start()
pass
#主循环
def loop(self):
root = Tk()
root.title('文件检测,自动上传_v1.0') # 窗口标题
root.geometry('480x80') # 窗口大小
root.minsize(490, 80)
self.show_folderPath = Entry(root,width=40)
self.show_folderPath.grid(row = 1,column = 2)
btn = Button(root,text ="选择文件夹",command = self.openFloder) # 设置一个按钮
btn.grid(row = 1,column = 1)
start_btn = Button(root,text ="启动",command = self.startThread) # 设置一个按钮
start_btn.grid(row = 1,column = 3)
stop_btn = Button(root,text ="停止",command = self.stopThread) # 设置一个按钮
stop_btn.grid(row = 1,column = 4)
test_ext_btn = Button(root,text ="测试弹窗",command = self.testcmdThread) # 设置一个按钮
test_ext_btn.grid(row = 1,column = 5)
root.mainloop()
pass
if __name__ == "__main__":
select = SelectDirectory()
select.loop()
pass