您好,登錄后才能下訂單哦!
這篇“怎么利用Python實(shí)現(xiàn)自定義連點(diǎn)器”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“怎么利用Python實(shí)現(xiàn)自定義連點(diǎn)器”文章吧。
前些天留意到我媽一直在預(yù)約四價(jià)疫苗都約不上,就想著寫個(gè)程序來模擬人的操作去點(diǎn)擊,判斷疫苗是否被搶完,無限循環(huán)去刷新這個(gè)頁面,一旦疫苗可預(yù)約就立馬搶下來選擇時(shí)間接種人。當(dāng)預(yù)約成功后就語音循環(huán)播報(bào):已經(jīng)搶到,趕緊過來看一下。
基于以上的想法和需求,我花了半小時(shí)編輯了以下代碼,并在一小時(shí)內(nèi)成功預(yù)約。
import pyautogui from ctypes import * # 獲取屏幕上某個(gè)坐標(biāo)的顏色 from time import sleep import time start = time.time() def get_color(x, y): gdi32 = windll.gdi32 user32 = windll.user32 hdc = user32.GetDC(None) # 獲取顏色值 pixel = gdi32.GetPixel(hdc, x, y) # 提取RGB值 r = pixel & 0x0000ff g = (pixel & 0x00ff00) >> 8 b = pixel >> 16 return [r, g, b] print(get_color(297,454)) while True: if get_color(240 , 255) == [60,211,180] or get_color(247,255) == [60,211,180] or get_color(253,255) == [60,211,180] or get_color(260,255) == [60,211,180] or get_color(270,255) == [60,211,180] or get_color(280,255) == [60,211,180] or get_color(290 ,255) == [60,211,180] or get_color(300 ,255) == [60,211,180] or get_color(310,255) == [60,211,180] or get_color(320, 255) == [60,211,180]: pyautogui.click(310,255)#點(diǎn)進(jìn)去搶 sleep(0.5) pyautogui.click(467,262)#選擇預(yù)約時(shí)間 while True: if get_color(297,454) == [0,142,255]: break else: sleep(0.3) sleep(0.5) pyautogui.click(498,454)#點(diǎn)擊下午 sleep(0.5) pyautogui.click(467,520)#選擇時(shí)間 sleep(0.5) pyautogui.click(470,899)#點(diǎn)選好了 sleep(0.5) pyautogui.click(470, 899)#點(diǎn)立即預(yù)約 #sleep() break else: pyautogui.click(123,60) sleep(0.8)#刷新 print('總耗時(shí):'.format(time.time()-start)) print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())) while 1: import pyttsx3 engine = pyttsx3.init() engine.say('我搶到了!快來看一下') engine.runAndWait() sleep(1)
實(shí)現(xiàn)思路大致流程圖:
可以看到邏輯非常簡單即可實(shí)現(xiàn)我想要的功能,不過即使是這樣,我也花了差不多半小時(shí)的時(shí)間來編寫代碼。于是我就在想,要是以后我要開發(fā)搶票、技能連招啊、信息轟炸朋友啊等等的功能,是不是也要這么多時(shí)間呢,那我能不能自己造輪子來快速幫助我開發(fā)我想要的腳本呢。
一般需要的功能有:點(diǎn)擊、延時(shí)、連點(diǎn)、拖拽。一般這四個(gè)功能就能完成絕大多數(shù)的簡單的輔助腳本開發(fā)了,但我想做得稍微高級一點(diǎn),功能多一點(diǎn),就想另外開發(fā):循環(huán)、判斷、模擬按鍵、文本輸入、存儲(chǔ)操作、讀取操作的功能。
那么我們就要朝著我們想要實(shí)現(xiàn)的九大功能來開發(fā):循環(huán)、點(diǎn)擊、延時(shí)、連點(diǎn)、拖拽、判斷、模擬按鍵、文本輸入、存儲(chǔ)操作、讀取操作。
首先就是希望我的每一步操作都會(huì)被程序記錄下來并執(zhí)行,我們可以定義一個(gè)列表來存儲(chǔ)每一個(gè)操作,列表中的每一個(gè)元素就是每一步的操作,然后遍歷這個(gè)列表來讀取并執(zhí)行每一個(gè)操作就可以將一整個(gè)操作全部執(zhí)行。
當(dāng)我的每一步操作都輸入完畢后,我都希望程序能自動(dòng)幫我把程序存儲(chǔ)下來方便我下一次使用,這樣下次使用就不用再編譯多一次了。
每一個(gè)列表的第0項(xiàng)就是需要操作的功能,第0項(xiàng)之后都是各種參數(shù)。
要想電腦幫我們點(diǎn)擊,首先要告訴電腦我要點(diǎn)擊的位置在哪里。想要獲取鼠標(biāo)位置就需要用到pyautogui庫,這個(gè)庫下有個(gè)position()方法可以返回鼠標(biāo)位置的X坐標(biāo),Y坐標(biāo)。
定義獲取位置函數(shù)
import pyautogui def get_xy(): x, y = pyautogui.position() return [x,y]
用面向?qū)ο笏枷雭砗喕绦?,提高代碼復(fù)用率,使程序可讀性更高,是python開發(fā)的重要思想之一哦
pyautogui庫還有非常多常見功能,感興趣的可以翻看我之前寫的博客:Python速成篇之像selenium一樣操作電腦詳解
點(diǎn)擊功能代碼如下
step=[] while True: choose = input('請輸入你需要使用的功能:') if choose == '點(diǎn)擊': click = [] while 1: click_dongzuo = input('請移動(dòng)鼠標(biāo)到目標(biāo)位置上輸入“1”完成動(dòng)作,輸入“0”取消動(dòng)作') if click_dongzuo == '1': click_weizhi = get_xy() click.append('點(diǎn)擊') click.append(click_weizhi) step.append(click) break elif click_dongzuo == '0': print('操作已取消') break else: print('請輸入正確的操作(輸入“0”或“1”')
執(zhí)行時(shí)的邏輯
for i in step1: if i[0] == '點(diǎn)擊': x = int(i[1][0]) y = int(i[1][1]) print(f'{x},{y}') pyautogui.click(x,y)
記錄點(diǎn)擊需要記錄點(diǎn)擊功能、位置參數(shù)生成一個(gè)列表,然后將列表append到step總列表中去
使用到了python內(nèi)置庫中的time模塊,可以使程序強(qiáng)制停止相應(yīng)時(shí)間。將參數(shù)生成列表append到step總列表中去
if choose =='延時(shí)': while 1: timerr = [] try: timex = int(input('請輸入延時(shí)時(shí)間:')) timerr.append('延時(shí)') timerr.append(timex) step.append(timerr) break except: print('延時(shí)失敗/n請輸入正確的延時(shí)時(shí)間') continue
執(zhí)行時(shí)的邏輯
def timer(timex): time.sleep(timex) if i[0] == '延時(shí)': t = int(i[1]) timer(t)
有些簡單的頁面可以通過連點(diǎn)來實(shí)現(xiàn)搶票等功能,這個(gè)功能必不可少
記錄這個(gè)動(dòng)作的必要參數(shù)有連點(diǎn)功能記錄、點(diǎn)擊頻率參數(shù)、通過連點(diǎn)次數(shù)完成動(dòng)作還是通過連點(diǎn)時(shí)長完成動(dòng)作。
同樣是調(diào)用了獲取鼠標(biāo)位置的函數(shù),
if choose == '連點(diǎn)': click_liandian = [] while 1: click_dongzuo = input('請移動(dòng)鼠標(biāo)到目標(biāo)位置上輸入“1”完成動(dòng)作,輸入“0”取消動(dòng)作') if click_dongzuo == '1': click_weizhi = get_xy() click_liandian.append('連點(diǎn)') click_liandian.append(click_weizhi) break elif click_dongzuo == '0': print('操作已取消') break else: print('請輸入正確的操作(輸入“0”或“1”') click_pinlv = float(input('請輸入連點(diǎn)頻率:')) while 1: click_stop_choose = input('“連點(diǎn)次數(shù)”or“連點(diǎn)時(shí)長”') if click_stop_choose =='連點(diǎn)次數(shù)': click_cishu = int(input('請輸入連點(diǎn)次數(shù):')) click_liandian.append('連點(diǎn)次數(shù)') click_liandian.append(click_cishu) click_liandian.append(click_pinlv) step.append(click_liandian) print(click_liandian) print(step) break if click_stop_choose == '連點(diǎn)時(shí)長': click_shichang = int(input('請輸入連點(diǎn)時(shí)長(秒):')) click_liandian.append('連點(diǎn)時(shí)長') click_liandian.append(click_shichang) step.append(click_liandian) click_liandian.append(click_pinlv) print(click_liandian) print(step) break else: continue
執(zhí)行時(shí)的邏輯
if choose == '連點(diǎn)': click_liandian = [] while 1: click_dongzuo = input('請移動(dòng)鼠標(biāo)到目標(biāo)位置上輸入“1”完成動(dòng)作,輸入“0”取消動(dòng)作') if click_dongzuo == '1': click_weizhi = get_xy() click_liandian.append('連點(diǎn)') click_liandian.append(click_weizhi) break elif click_dongzuo == '0': print('操作已取消') break else: print('請輸入正確的操作(輸入“0”或“1”') click_pinlv = float(input('請輸入連點(diǎn)頻率:')) while 1: click_stop_choose = input('“連點(diǎn)次數(shù)”or“連點(diǎn)時(shí)長”') if click_stop_choose =='連點(diǎn)次數(shù)': click_cishu = int(input('請輸入連點(diǎn)次數(shù):')) click_liandian.append('連點(diǎn)次數(shù)') click_liandian.append(click_cishu) click_liandian.append(click_pinlv) step.append(click_liandian) print(click_liandian) print(step) break if click_stop_choose == '連點(diǎn)時(shí)長': click_shichang = int(input('請輸入連點(diǎn)時(shí)長(秒):')) click_liandian.append('連點(diǎn)時(shí)長') click_liandian.append(click_shichang) step.append(click_liandian) click_liandian.append(click_pinlv) print(click_liandian) print(step) break else: continue
當(dāng)我們記錄完所有操作后我們希望將操作保存下來方便下次使用,不需要從頭錄入。
這將生成一個(gè)與py腳本同級的txt文件,txt文件中保存了所有的步驟,可直接讀取使用
if choose =='存儲(chǔ)': if len(step) == 0: print('還未記錄你任何操作,請?zhí)砑硬僮髟偈褂么鎯?chǔ)功能') else: do_name = input('請為以上操作命名吧:') path = r"{}.txt".format(do_name) with open(path, "w",encoding = 'utf8') as f: f.write(str(step))
這一步呢就稍微麻煩一點(diǎn),因?yàn)閠xt沒辦法記錄list類型的數(shù)據(jù),只能以str類型寫出去并且只能以str類型讀取進(jìn)來。我自己定義了一些函數(shù)來轉(zhuǎn)化為list類型,比較復(fù)雜,就不再說明函數(shù)是怎么實(shí)現(xiàn)的,日后有機(jī)會(huì)再跟各位分享。
def writeList2txt(file,data): ''' 將list寫入txt :param data: :return: ''' file.write(str(data)) def readListFromStr(str): ''' str -> List 除去冗余的方法調(diào)用 :param str: :return: ''' res,pos = help(str,1) res1=[] a ='1' for ii in res: iii=[] for i in ii: if type(i)==type(a): i = i.replace("'", "") iii.append(i) else: iii.append(i) res1.append(iii) return res1 def help(str,startIndex): ''' 單行字符串的讀取,形成list :param str: :return: ''' str = str.replace(" ","") # 將所有空格刪去 res = [] i = startIndex pre = startIndex while i <len(str): if str[i] == '[': # 將pre-i-2的字符都切片,切split if i-2>=pre: slice = str[pre:i-1].split(',') for element in slice: res.append(element) # 遞歸調(diào)用 加入子list child,pos = help(str,i+1) res.append(child) i = pos # i移動(dòng)到pos位置,也就是遞歸的最后一個(gè)右括號 pre = pos + 2 # 右括號之后是, [ 有三個(gè)字符,所以要+2至少 elif str[i] == ']': # 將前面的全部放入列表 if i-1>=pre: slice = str[pre:i].split(',') for element in slice: res.append(element) return res,i i = i + 1 return res,i def get_caozuo(caozuopath): with open(caozuopath , 'r' , encoding='utf8') as f: data = f.read() return data def get_caozuo_name(): files1 = [] file_dir = r"C:\Users\ge\Desktop\test1\我的作品\自定義連點(diǎn)器" for root, dirs, files in os.walk(file_dir, topdown=False): files = files[:-1] for i in files: files1.append(i[:-4]) return files1 print(get_caozuo_name()) if choose == '循環(huán)執(zhí)行': caozuojiyi = get_caozuo_name() while True: xunhuan_choose = input('已存儲(chǔ)的操作有:{}\n請輸入循環(huán)操作的操作名:'.format(caozuojiyi)) if xunhuan_choose in caozuojiyi: break else: print('存儲(chǔ)庫中并無你想要的操作,請重新輸入:')
上面的功能只能把二維的列表導(dǎo)入成list類型的數(shù)據(jù)類型,不利于后面的導(dǎo)入。一旦存儲(chǔ)或?qū)氲牧斜磉_(dá)到三維或以上就不適用了。后來我在網(wǎng)上搜了半天,終于發(fā)現(xiàn)json庫里面有方法支持以list的形式導(dǎo)出txt并且可以以list的方式讀取。以下是我實(shí)現(xiàn)存儲(chǔ)導(dǎo)入的邏輯代碼:
導(dǎo)入(功能實(shí)現(xiàn))
def txttolist(path): import json b = open(path, "r", encoding='UTF-8') out = b.read() out = json.loads(out) return out
導(dǎo)入(邏輯代碼)
如果程序內(nèi)存里已有操作了將提示保存
if choose == '導(dǎo)入': if len(step) == 0: step = daoru()[0] else: baocun_choose = input('此次操作若未保存請先,導(dǎo)入別的操作會(huì)覆蓋原來的操作,你確定要導(dǎo)入嗎?\n請輸入“yes”or“no”:\n') while 1: if baocun_choose == 'no': break if baocun_choose == 'yes': print('你已取消保存') step = daoru()[0] break else: yorn = input("請輸入'yes'or'no':\n")
存儲(chǔ)(功能實(shí)現(xiàn))
def cunchu(): yorn = input("執(zhí)行完畢,是否保存?\n輸入'yes'or'no'\n") while 1: if yorn == 'yes': if len(step) == 0: print('還未記錄你任何操作,請?zhí)砑硬僮髟偈褂么鎯?chǔ)功能') else: do_name = input('請為以上操作命名吧:') path = r"{}.txt".format(do_name) listtotxt(list=step, path=path) break if yorn == 'no': print('你已取消存儲(chǔ)') break else: yorn = input("請輸入'yes'or'no':\n") def listtotxt(list, path): import json c_list = list c_list = json.dumps(c_list) '''將c_list存入文件 ''' a = open(path, "w", encoding='UTF-8') a.write(c_list) a.close() print('已存入txt')
存儲(chǔ)(邏輯代碼)
要是程序內(nèi)存里還沒有操作將提醒
if choose == '存儲(chǔ)': if len(step) == 0: print('還未記錄你任何操作,請?zhí)砑硬僮髟偈褂么鎯?chǔ)功能') else: do_name = input('請為以上操作命名吧:') path = r"{}.txt".format(do_name) listtotxt(list=step, path=path)
這個(gè)功能也是基于pyautogui庫來使用的,主要用到了pyautogui.dragTo()方法
功能實(shí)現(xiàn)
pyautogui.moveTo(int(i[1][0]), int(i[1][1])) pyautogui.dragTo(int(i[2][0]), int(i[2][1]), 1, button='left') print(f'已執(zhí)行拖拽動(dòng)作,拖拽起始位置是X:{int(i[1][0])},Y:{int(i[1][1])},拖拽后的位置是X:{int(i[2][0])},Y:{int(i[2][1])}')
邏輯代碼:
先創(chuàng)建列表tuozhuai,向列表添加三個(gè)參數(shù):“拖拽”、第一個(gè)位置參數(shù)、第二個(gè)位置參數(shù)
if choose == '拖拽': tuozhuai = [] while 1: click_dongzuo = input('請移動(dòng)鼠標(biāo)到目標(biāo)位置上輸入“1”完成移動(dòng)前的位置輸入,輸入“0”取消動(dòng)作') if click_dongzuo == '1': click_weizhi = get_xy() tuozhuai.append('拖拽') tuozhuai.append(click_weizhi) while 1: click_dongzuo = input('請移動(dòng)鼠標(biāo)到目標(biāo)位置上輸入“1”完成移動(dòng)后的位置輸入,輸入“0”取消動(dòng)作') if click_dongzuo == '1': click_weizhi = get_xy() tuozhuai.append(click_weizhi) break elif click_dongzuo == '0': print('操作已取消') break else: print('請輸入正確的操作(輸入“0”或“1”') break elif click_dongzuo == '0': print('操作已取消') break else: print('請輸入正確的操作(輸入“0”或“1”') step.append(tuozhuai)
也是用到了pyauogui庫,主要使用pyautogui庫的pytewrite函數(shù),但是這個(gè)函數(shù)對中文不友好,于是我另辟蹊徑使用pyperclip庫的copy函數(shù)將要輸入的文本內(nèi)容拷貝打粘貼板,通過控制按鍵control+v來輸入至目標(biāo)位置。
功能實(shí)現(xiàn)
if choose == '輸入': shuru = [] while 1: click_dongzuo = input('請移動(dòng)鼠標(biāo)到你要輸入的位置上輸入“1”完成動(dòng)作,輸入“0”取消動(dòng)作') if click_dongzuo == '1': click_weizhi = get_xy() txt_in = input('請輸入你要在該位置輸入的文字:\n') shuru.append('輸入') shuru.append(click_weizhi) shuru.append(txt_in) step.append(shuru) break elif click_dongzuo == '0': print('操作已取消') break else: print('請輸入正確的操作(輸入“0”或“1”')
邏輯代碼
if i[0] == '輸入': pyautogui.click(int(i[1][0]), int(i[1][1])) pyperclip.copy(i[2]) time.sleep(0.1) pyautogui.hotkey('ctrl', 'v')
原理相同,將不再贅述
功能實(shí)現(xiàn)
if i[0] == '雙擊': pyautogui.click(int(i[1][0]), int(i[1][1])) pyautogui.click(int(i[1][0]), int(i[1][1])) print(f'已執(zhí)行完點(diǎn)擊動(dòng)作,點(diǎn)擊坐標(biāo)位置:X:{int(i[1][0])},Y:{int(i[1][1])} ') if i[0] == '右擊': x = int(i[1][0]) y = int(i[1][1]) pyautogui.rightClick(x, y) print(f'已執(zhí)行完右擊動(dòng)作,點(diǎn)擊坐標(biāo)位置:X:{x},Y:{y} ') if i[0] == '中擊': x = int(i[1][0]) y = int(i[1][1]) pyautogui.middleClick(x, y) print(f'已執(zhí)行完中擊動(dòng)作,點(diǎn)擊坐標(biāo)位置:X:{x},Y:{y} ')
邏輯代碼
if choose == '右擊': click_r = [] while 1: click_dongzuo = input('請移動(dòng)鼠標(biāo)到目標(biāo)位置上輸入“1”完成動(dòng)作,輸入“0”取消動(dòng)作') if click_dongzuo == '1': click_weizhi = get_xy() click_r.append('右擊') click_r.append(click_weizhi) step.append(click_r) break elif click_dongzuo == '0': print('操作已取消') break else: print('請輸入正確的操作(輸入“0”或“1”') if choose == '中擊': click_m = [] while 1: click_dongzuo = input('請移動(dòng)鼠標(biāo)到目標(biāo)位置上輸入“1”完成動(dòng)作,輸入“0”取消動(dòng)作') if click_dongzuo == '1': click_weizhi = get_xy() click_m.append('中擊') click_m.append(click_weizhi) step.append(click_m) break elif click_dongzuo == '0': print('操作已取消') break else: print('請輸入正確的操作(輸入“0”或“1”') click_double = [] while 1: click_dongzuo = input('請移動(dòng)鼠標(biāo)到目標(biāo)位置上輸入“1”完成動(dòng)作,輸入“0”取消動(dòng)作') if click_dongzuo == '1': click_weizhi = get_xy() click_double.append('雙擊') click_double.append(click_weizhi) step.append(click_double) break elif click_dongzuo == '0': print('操作已取消') break else: print('請輸入正確的操作(輸入“0”或“1”')
功能實(shí)現(xiàn)
if i[0] == '按鍵': pyautogui.hotkey(*i[1])
邏輯代碼
if choose == '按鍵': while 1: anjian = input('這是模擬操作鍵盤的操作(例如復(fù)制,輸入'ctrl‘ + 'c‘):\n') if anjian != 'q': anjian = anjian.split('+') anjians = [] a = [] for an in anjian: an = an.replace("‘", "").replace("'", "").strip() if an in pyautogui.KEYBOARD_KEYS: anjians.append(an) nihaofan = 0 else: print('你的輸入不合法') nihaofan = 1 break if nihaofan == 0: a.append('按鍵') a.append(anjians) step.append(a) print('錄入成功') break if anjian == 'q': break
功能實(shí)現(xiàn)
if i[0] == '滾動(dòng)': import pywinauto.mouse x, y = pyautogui.position() pywinauto.mouse.scroll((x, y), i[1]) # (1100,300)是初始坐標(biāo),1000是滑動(dòng)距離(可負(fù))
邏輯代碼
if choose == '滾動(dòng)': while 1: gundong = [] try: gundong1 = int(input('這里是模擬鼠標(biāo)滾動(dòng),請輸入你要滾動(dòng)距離(正數(shù)為向上移動(dòng),負(fù)數(shù)為向下移動(dòng)):\n')) gundong.append('滾動(dòng)') gundong.append(gundong1) step.append(gundong) break except: print('你的輸入有誤,請重新輸入')
def chakan(): if len(step) == 0: print('暫未錄入操作,請先錄入操作再查看') zizeng = 1 for i in step: if i[0] == '點(diǎn)擊': x = int(i[1][0]) y = int(i[1][1]) print(f'第{zizeng}步:\n執(zhí)行點(diǎn)擊動(dòng)作,點(diǎn)擊坐標(biāo)位置:X:{x},Y:{y} ') if i[0] == '延時(shí)': t = int(i[1]) print(f'第{zizeng}步:\n執(zhí)行延時(shí)動(dòng)作,延時(shí)時(shí)長:{t}秒') if i[0] == '連點(diǎn)': if i[2] == '連點(diǎn)次數(shù)': print(f'第{zizeng}步:\n執(zhí)行連點(diǎn)操作,你選擇的是{i[2]},連點(diǎn)次數(shù)是{i[4]}') if i[2] == '連點(diǎn)時(shí)長': print(f'第{zizeng}步:\n執(zhí)行連點(diǎn)操作,你選擇的是{i[2]},連點(diǎn)時(shí)長是{i[4]}秒') if i[0] == '拖拽': print( f'第{zizeng}步:\n執(zhí)行拖拽動(dòng)作,拖拽起始位置是X:{int(i[1][0])},Y:{int(i[1][1])},拖拽后的位置是X:{int(i[2][0])},Y:{int(i[2][1])}') if i[0] == '雙擊': print(f'第{zizeng}步:\n執(zhí)行點(diǎn)擊動(dòng)作,點(diǎn)擊坐標(biāo)位置:X:{int(i[1][0])},Y:{int(i[1][1])} ') if i[0] == '按鍵': print(f'第{zizeng}步:\n執(zhí)行按鍵動(dòng)作,將同時(shí)按下”{i[1]}“鍵') zizeng += 1
執(zhí)行后將詢問是否保存
def zhixing(step): for i in step: if i[0] == '點(diǎn)擊': x = int(i[1][0]) y = int(i[1][1]) pyautogui.click(x, y) print(f'已執(zhí)行完點(diǎn)擊動(dòng)作,點(diǎn)擊坐標(biāo)位置:X:{x},Y:{y} ') if i[0] == '延時(shí)': t = int(i[1]) timer(t) print(f'已執(zhí)行完延時(shí)動(dòng)作,延時(shí)時(shí)長:{t}秒') if i[0] == '連點(diǎn)': if i[2] == '連點(diǎn)次數(shù)': clicker_cishu(int(i[3]), int(i[1][0]), int(i[1][1]), int(i[4])) print(f'已執(zhí)行完連點(diǎn)操作,你選擇的是{i[2]},連點(diǎn)次數(shù)是{i[4]}') if i[2] == '連點(diǎn)時(shí)長': clicker_time(int(i[3]), int(i[1][0]), int(i[1][1]), int(i[4])) print(f'已執(zhí)行完連點(diǎn)操作,你選擇的是{i[2]},連點(diǎn)時(shí)長是{i[4]}秒') if i[0] == '拖拽': pyautogui.moveTo(int(i[1][0]), int(i[1][1])) pyautogui.dragTo(int(i[2][0]), int(i[2][1]), 1, button='left') print(f'已執(zhí)行拖拽動(dòng)作,拖拽起始位置是X:{int(i[1][0])},Y:{int(i[1][1])},拖拽后的位置是X:{int(i[2][0])},Y:{int(i[2][1])}') if i[0] == '雙擊': pyautogui.click(int(i[1][0]), int(i[1][1])) pyautogui.click(int(i[1][0]), int(i[1][1])) print(f'已執(zhí)行完點(diǎn)擊動(dòng)作,點(diǎn)擊坐標(biāo)位置:X:{int(i[1][0])},Y:{int(i[1][1])} ') if i[0] == '輸入': pyautogui.click(int(i[1][0]), int(i[1][1])) pyperclip.copy(i[2]) time.sleep(0.1) pyautogui.hotkey('ctrl', 'v') if i[0] == '按鍵': pyautogui.hotkey(*i[1]) if i[0] == '右擊': x = int(i[1][0]) y = int(i[1][1]) pyautogui.rightClick(x, y) print(f'已執(zhí)行完右擊動(dòng)作,點(diǎn)擊坐標(biāo)位置:X:{x},Y:{y} ') if i[0] == '中擊': x = int(i[1][0]) y = int(i[1][1]) pyautogui.middleClick(x, y) print(f'已執(zhí)行完中擊動(dòng)作,點(diǎn)擊坐標(biāo)位置:X:{x},Y:{y} ') if i[0] == '滾動(dòng)': import pywinauto.mouse x, y = pyautogui.position() pywinauto.mouse.scroll((x, y), i[1]) # (1100,300)是初始坐標(biāo),1000是滑動(dòng)距離(可負(fù))
if choose == '執(zhí)行': if len(step) == 0: print('你還未記錄任何操作,請至少記錄了一個(gè)操作再執(zhí)行') else: zhixing(step) cunchu()
到了最難最虐腦的邏輯判斷功能了,邏輯判斷板塊這幾個(gè)功能困擾了我一整天,敲到我腦殼疼
實(shí)現(xiàn)這一功能主要是基于顏色的RBG值來判斷程序所要要執(zhí)行的步驟塊。
選擇目標(biāo)點(diǎn),開啟線程去時(shí)刻監(jiān)管這個(gè)目標(biāo)點(diǎn)的顏色變化,一旦目標(biāo)顏色變?yōu)槠诖?,立即?zhí)行之前存儲(chǔ)的步驟塊,可以選擇是否循環(huán)這個(gè)步驟塊的操作。選擇完畢后開啟第二個(gè)線程去執(zhí)行這個(gè)步驟塊,此時(shí)主程序?qū)⒗^續(xù)遍歷panduans的操作。設(shè)置一個(gè)while循環(huán)來阻塞主程序的運(yùn)行及監(jiān)控state變量值的變化,state初始值為“未觸發(fā)”,一旦監(jiān)管線程發(fā)現(xiàn)目標(biāo)值變化為期待值,立即修改state值為“觸發(fā)”,同時(shí)關(guān)閉執(zhí)行步驟塊的線程,同時(shí)關(guān)閉自身的監(jiān)管線程,此時(shí)主程序檢測到state值為“觸發(fā)”后立刻將新的步驟塊的線程開啟并將state值修改為“未觸發(fā)”。就此開啟新一輪的循環(huán)。
之間呢,遇到了多個(gè)線程修改同一個(gè)值的情況導(dǎo)致報(bào)錯(cuò);遇到了多種停止線程的方法都不適用的情況;遇到了沒設(shè)置守護(hù)進(jìn)程又要找到這個(gè)進(jìn)程去關(guān)閉的情況;嘗試了老版的_thread進(jìn)程庫、嘗試了主流的threading進(jìn)程庫、嘗試了線程池的方法,終于找到一條適合我的方法。不容易呀
if choose == '判斷': if len(panduans) == 0: tuichu = 0 panduan = input('此功能的實(shí)現(xiàn)是基于顏色的RBG值來判斷程序所要要執(zhí)行的步驟塊。\n現(xiàn)在,請選擇你的‘先執(zhí)行步驟塊等待條件觸發(fā)'或是‘直接等待條件觸發(fā)'的操作:(輸入"步驟塊"或"等待")\n') if panduan == '如果': panduan_if = [] while 1: click_dongzuo = input('請移動(dòng)鼠標(biāo)到目標(biāo)位置上吸取顏色,輸入“1”完成動(dòng)作,輸入“0”取消動(dòng)作') if click_dongzuo == '1': xy = get_xy() click_color = GetColor(xy) panduan_yn = input(f'這個(gè)位置的RGB為:{click_color},是否確定為下一步驟塊的判斷根據(jù)?(輸入"yes"or"no")\n') while 1: if panduan_yn == 'yes': get_caozuo_name() print(f'請選擇滿足當(dāng)顏色為{click_color}時(shí)要執(zhí)行的步驟包:') steps, steps_name = daoru() xunhuan_yn = input('這個(gè)步驟塊是否循環(huán)執(zhí)行至下一條件觸發(fā)?(輸入"yes"or"no")\n') while 1: if xunhuan_yn == 'yes': panduan_if.append('如果') panduan_if.append(xy) panduan_if.append(click_color) panduan_if.append(steps_name) panduan_if.append('循環(huán)') panduan_if.append(steps) panduans.append(panduan_if) print('添加成功,該步驟包將會(huì)循環(huán)') break elif xunhuan_yn == 'no': panduan_if.append('如果') panduan_if.append(xy) panduan_if.append(click_color) panduan_if.append(steps_name) panduan_if.append('不循環(huán)') panduan_if.append(steps) panduans.append(panduan_if) print('添加成功,該步驟包將只執(zhí)行一次') break else: xunhuan_yn = input('你的輸入有誤,請輸入"yes"or"no":') tuichu = 1 break if panduan_yn == 'no': print('請重新選擇') break else: panduan_yn = input('你的輸入有誤,請輸入"yes"or"no"') if tuichu == 1: break elif click_dongzuo == '0': print('操作已取消') break else: print('請輸入正確的操作(輸入“0”或“1”)') if panduan == '步驟塊': panduan_step = [] steps, steps_name = daoru() xunhuan_yn = input('這個(gè)步驟塊是否循環(huán)執(zhí)行直至條件觸發(fā)?(輸入"yes"or"no")\n') while 1: if xunhuan_yn == 'yes': panduan_step.append('步驟塊') panduan_step.append('循環(huán)') panduan_step.append(steps_name) panduan_step.append(steps) panduans.append(panduan_step) break elif xunhuan_yn == 'no': panduan_step.append('步驟塊') panduan_step.append('不循環(huán)') panduan_step.append(steps_name) panduan_step.append(steps) panduans.append(panduan_step) break else: xunhuan_yn = input('你的輸入有誤,請輸入"yes"or"no":') if panduan == '等待': panduan_if = [] print('你選擇了等待,程序?qū)r(shí)刻檢測目標(biāo)位置的顏色以執(zhí)行接下來的步驟塊') panduan_if.append('等待') panduans.append(panduan_if) if panduan != '步驟塊' and panduan != '如果' and panduan != '等待': print('你的輸入有誤') if len(panduans) > 0: print('你一錄入了至少一個(gè)邏輯判斷,請選擇繼續(xù)選擇目標(biāo)位置的顏色來觸發(fā)接下來你選擇的步驟塊') panduan_if = [] while 1: click_dongzuo = input('請移動(dòng)鼠標(biāo)到目標(biāo)位置上吸取顏色,輸入“1”完成動(dòng)作,輸入“0”取消動(dòng)作') if click_dongzuo == '1': xy = get_xy() click_color = GetColor(xy) panduan_yn = input(f'這個(gè)位置的RGB為:{click_color},是否確定為下一步驟塊的判斷根據(jù)?(輸入"yes"or"no")\n') while 1: if panduan_yn == 'yes': get_caozuo_name() print(f'請選擇滿足當(dāng)顏色為{click_color}時(shí)要執(zhí)行的步驟包:') steps, steps_name = daoru() xunhuan_yn = input('這個(gè)步驟塊是否循環(huán)執(zhí)行直至條件觸發(fā)?(輸入"yes"or"no")\n') while 1: if xunhuan_yn == 'yes': panduan_if.append('如果') panduan_if.append(xy) panduan_if.append(click_color) panduan_if.append(steps_name) panduan_if.append('循環(huán)') panduan_if.append(steps) panduans.append(panduan_if) print('添加成功,該步驟包將會(huì)循環(huán)') break elif xunhuan_yn == 'no': panduan_if.append('如果') panduan_if.append(xy) panduan_if.append(click_color) panduan_if.append(steps_name) panduan_if.append('不循環(huán)') panduan_if.append(steps) panduans.append(panduan_if) print('添加成功,該步驟包將只執(zhí)行一次') break else: xunhuan_yn = input('你的輸入有誤,請輸入"yes"or"no":') tuichu = 1 break if panduan_yn == 'no': print('請重新選擇') break else: panduan_yn = input('你的輸入有誤,請輸入"yes"or"no"') if tuichu == 1: break elif click_dongzuo == '0': print('操作已取消') break else: print('請輸入正確的操作(輸入“0”或“1”)')
if choose == '邏輯執(zhí)行': print('這里是邏輯執(zhí)行庫,所有的邏輯判斷都會(huì)存儲(chǔ)到這里') print(panduans) xiancheng = threading.Thread(target=zhixingbuzhoukuai, args=('等待', '1', '循環(huán)')) xiancheng.setDaemon(True) xiancheng.start() for pan in panduans: state = '未觸發(fā)' if pan[0] == '如果': print(pan[5]) print(len(pan[5])) bu = str(pan[5]) print(bu) bu = readListFromStr(bu) zhixing(bu) print(bu) if state == '未觸發(fā)': if pan[4] == '循環(huán)': rgb = pan[2] rgb_xy = pan[1] _thread.start_new_thread(jianshi, ()) while 1: if state == '觸發(fā)': xiancheng = threading.Thread(target=zhixingbuzhoukuai, args=(pan[3], pan[5], '循環(huán)')) xiancheng.start() state = '未觸發(fā)' break if pan[4] == '不循環(huán)': rgb = pan[2] rgb_xy = pan[1] _thread.start_new_thread(jianshi, ()) while 1: if state == '觸發(fā)': xiancheng = threading.Thread(target=zhixingbuzhoukuai, args=(pan[3], pan[5], '不循環(huán)')) xiancheng.start() state = '未觸發(fā)' break if pan[0] == '步驟塊': stop_thread(xiancheng) if pan[1] == '循環(huán)': xiancheng = threading.Thread(target=zhixingbuzhoukuai, args=(pan[2], pan[3], '循環(huán)')) xiancheng.start() if pan[1] == '不循環(huán)': xiancheng = threading.Thread(target=zhixingbuzhoukuai, args=(pan[2], pan[3], '不循環(huán)')) xiancheng.start() if pan[0] == '等待': print('程序正在監(jiān)測目標(biāo)位置RGB值') print('邏輯執(zhí)行已全部執(zhí)行完畢') break
邏輯塊存儲(chǔ)功能的實(shí)現(xiàn)
def listtotxt(list, path): import json c_list = list c_list = json.dumps(c_list) '''將c_list存入文件 ''' a = open(path, "w", encoding='UTF-8') a.write(c_list) a.close() print('已存入txt')
邏輯塊存儲(chǔ)邏輯代碼
if choose == '邏輯塊存儲(chǔ)': yorn = input("確定保存?\n輸入'yes'or'no'\n") while 1: if yorn == 'yes': if len(panduans) == 0: print('還未記錄你任何操作,請?zhí)砑硬僮髟偈褂眠壿媺K存儲(chǔ)功能') else: do_name = input('請為以上操作命名吧:') if '邏輯塊存儲(chǔ)' in do_name: do_name = input('抱歉,你的命名里不允許包含”邏輯塊存儲(chǔ)“,請重新命名') else: path = r"{}邏輯塊存儲(chǔ).txt".format(do_name) listtotxt(list=panduans, path=path) break if yorn == 'no': print('你已取消存儲(chǔ)') break else: yorn = input("請輸入'yes'or'no':\n")
邏輯塊導(dǎo)入功能的實(shí)現(xiàn)
def txttolist(path): import json b = open(path, "r", encoding='UTF-8') out = b.read() out = json.loads(out) return out
邏輯塊導(dǎo)入邏輯代碼
if choose == '邏輯塊導(dǎo)入': caozuojiyi = get_caozuokuai_name() while True: xunhuan_choose = input('已存儲(chǔ)的操作有:{}\n請輸入導(dǎo)入操作的操作名:'.format(caozuojiyi)) if xunhuan_choose in caozuojiyi: break else: print('邏輯塊存儲(chǔ)庫中并無你想要的操作,請重新輸入:') caozuopath = r"{}邏輯塊存儲(chǔ).txt".format(xunhuan_choose) panduans = txttolist(path=caozuopath)
import threading import pyautogui from ctypes import * import time import os, sys import pyperclip import inspect import ctypes import _thread def _async_raise(tid, exctype): """raises the exception, performs cleanup if needed""" tid = ctypes.c_long(tid) if not inspect.isclass(exctype): exctype = type(exctype) res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype)) if res == 0: raise ValueError("invalid thread id") elif res != 1: # """if it returns a number greater than one, you're in trouble, # and you should call it again with exc=NULL to revert the effect""" ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None) raise SystemError("PyThreadState_SetAsyncExc failed") def stop_thread(threa): _async_raise(threa.ident, SystemExit) def get_caozuo_name(): dirname, filename = os.path.split(os.path.abspath(sys.argv[0])) files1 = [] file_dir = r"{}".format(os.path.realpath(sys.argv[0])[:-13]) for root, dirs, files in os.walk(file_dir, topdown=False): files = files[:-1] for i in files: if '.txt' in i: files1.append(i[:-4]) return files1 def get_caozuokuai_name(): dirname, filename = os.path.split(os.path.abspath(sys.argv[0])) files1 = [] file_dir = r"{}".format(os.path.realpath(sys.argv[0])[:-13]) for root, dirs, files in os.walk(file_dir, topdown=False): files = files[:-1] for i in files: if '邏輯塊存儲(chǔ).txt' in i: files1.append(i[:-9]) return files1 def writeList2txt(file, data): ''' 將list寫入txt :param data: :return: ''' file.write(str(data), encoding='uft8') def readListFromStr(str): ''' str -> List 除去冗余的方法調(diào)用 :param str: :return: ''' res, pos = help(str, 1) res1 = [] a = '1' for ii in res: iii = [] for i in ii: if type(i) == type(a): i = i.replace("'", "") iii.append(i) else: iii.append(i) res1.append(iii) return res1 def help(str, startIndex): ''' 單行字符串的讀取,形成list :param str: :return: ''' str = str.replace(" ", "") # 將所有空格刪去 res = [] i = startIndex pre = startIndex while i < len(str): if str[i] == '[': # 將pre-i-2的字符都切片,切split if i - 2 >= pre: slice = str[pre:i - 1].split(',') for element in slice: res.append(element) # 遞歸調(diào)用 加入子list child, pos = help(str, i + 1) res.append(child) i = pos # i移動(dòng)到pos位置,也就是遞歸的最后一個(gè)右括號 pre = pos + 2 # 右括號之后是, [ 有三個(gè)字符,所以要+2至少 elif str[i] == ']': # 將前面的全部放入列表 if i - 1 >= pre: slice = str[pre:i].split(',') for element in slice: res.append(element) return res, i i = i + 1 return res, i def get_caozuo(caozuopath): with open(caozuopath, 'r', encoding='utf8') as f: data = f.read() return data def get_xy(): x, y = pyautogui.position() return [x, y] def GetColor(xy): x = xy[0] y = xy[1] r = 0 g = 0 b = 0 try: gdi32 = windll.gdi32 user32 = windll.user32 hdc = user32.GetDC(None) # 獲取顏色值 pixel = gdi32.GetPixel(hdc, x, y) # 提取RGB值 r = pixel & 0x0000ff g = (pixel & 0x00ff00) >> 8 b = pixel >> 16 except KeyboardInterrupt: print('\n') return [r, g, b] def timer(timex): time.sleep(timex) def clicker_cishu(cishu, x, y, pinlv): for a in range(cishu): pyautogui.click(x, y) time.sleep(pinlv) def clicker_time(shijian, x, y, pinlv): start = time.time() while True: pyautogui.click(x, y) time.sleep(pinlv) end = time.time() shijian1 = end - start if shijian1 >= shijian: break def zhixing(step): for i in step: if i[0] == '點(diǎn)擊': x = int(i[1][0]) y = int(i[1][1]) pyautogui.click(x, y) print(f'已執(zhí)行完點(diǎn)擊動(dòng)作,點(diǎn)擊坐標(biāo)位置:X:{x},Y:{y} ') if i[0] == '延時(shí)': t = int(i[1]) timer(t) print(f'已執(zhí)行完延時(shí)動(dòng)作,延時(shí)時(shí)長:{t}秒') if i[0] == '連點(diǎn)': if i[2] == '連點(diǎn)次數(shù)': clicker_cishu(int(i[3]), int(i[1][0]), int(i[1][1]), int(i[4])) print(f'已執(zhí)行完連點(diǎn)操作,你選擇的是{i[2]},連點(diǎn)次數(shù)是{i[4]}') if i[2] == '連點(diǎn)時(shí)長': clicker_time(int(i[3]), int(i[1][0]), int(i[1][1]), int(i[4])) print(f'已執(zhí)行完連點(diǎn)操作,你選擇的是{i[2]},連點(diǎn)時(shí)長是{i[4]}秒') if i[0] == '拖拽': pyautogui.moveTo(int(i[1][0]), int(i[1][1])) pyautogui.dragTo(int(i[2][0]), int(i[2][1]), 1, button='left') print(f'已執(zhí)行拖拽動(dòng)作,拖拽起始位置是X:{int(i[1][0])},Y:{int(i[1][1])},拖拽后的位置是X:{int(i[2][0])},Y:{int(i[2][1])}') if i[0] == '雙擊': pyautogui.click(int(i[1][0]), int(i[1][1])) pyautogui.click(int(i[1][0]), int(i[1][1])) print(f'已執(zhí)行完點(diǎn)擊動(dòng)作,點(diǎn)擊坐標(biāo)位置:X:{int(i[1][0])},Y:{int(i[1][1])} ') if i[0] == '輸入': pyautogui.click(int(i[1][0]), int(i[1][1])) pyperclip.copy(i[2]) time.sleep(0.1) pyautogui.hotkey('ctrl', 'v') if i[0] == '按鍵': pyautogui.hotkey(*i[1]) if i[0] == '右擊': x = int(i[1][0]) y = int(i[1][1]) pyautogui.rightClick(x, y) print(f'已執(zhí)行完右擊動(dòng)作,點(diǎn)擊坐標(biāo)位置:X:{x},Y:{y} ') if i[0] == '中擊': x = int(i[1][0]) y = int(i[1][1]) pyautogui.middleClick(x, y) print(f'已執(zhí)行完中擊動(dòng)作,點(diǎn)擊坐標(biāo)位置:X:{x},Y:{y} ') if i[0] == '滾動(dòng)': import pywinauto.mouse x, y = pyautogui.position() pywinauto.mouse.scroll((x, y), i[1]) # (1100,300)是初始坐標(biāo),1000是滑動(dòng)距離(可負(fù)) def cunchu(): yorn = input("執(zhí)行完畢,是否保存?\n輸入'yes'or'no'\n") while 1: if yorn == 'yes': if len(step) == 0: print('還未記錄你任何操作,請?zhí)砑硬僮髟偈褂么鎯?chǔ)功能') else: do_name = input('請為以上操作命名吧:') path = r"{}.txt".format(do_name) listtotxt(list=step, path=path) break if yorn == 'no': print('你已取消存儲(chǔ)') break else: yorn = input("請輸入'yes'or'no':\n") def chakan(): if len(step) == 0: print('暫未錄入操作,請先錄入操作再查看') zizeng = 1 for i in step: if i[0] == '點(diǎn)擊': x = int(i[1][0]) y = int(i[1][1]) print(f'第{zizeng}步:\n執(zhí)行點(diǎn)擊動(dòng)作,點(diǎn)擊坐標(biāo)位置:X:{x},Y:{y} ') if i[0] == '延時(shí)': t = int(i[1]) print(f'第{zizeng}步:\n執(zhí)行延時(shí)動(dòng)作,延時(shí)時(shí)長:{t}秒') if i[0] == '連點(diǎn)': if i[2] == '連點(diǎn)次數(shù)': print(f'第{zizeng}步:\n執(zhí)行連點(diǎn)操作,你選擇的是{i[2]},連點(diǎn)次數(shù)是{i[4]}') if i[2] == '連點(diǎn)時(shí)長': print(f'第{zizeng}步:\n執(zhí)行連點(diǎn)操作,你選擇的是{i[2]},連點(diǎn)時(shí)長是{i[4]}秒') if i[0] == '拖拽': print( f'第{zizeng}步:\n執(zhí)行拖拽動(dòng)作,拖拽起始位置是X:{int(i[1][0])},Y:{int(i[1][1])},拖拽后的位置是X:{int(i[2][0])},Y:{int(i[2][1])}') if i[0] == '雙擊': print(f'第{zizeng}步:\n執(zhí)行點(diǎn)擊動(dòng)作,點(diǎn)擊坐標(biāo)位置:X:{int(i[1][0])},Y:{int(i[1][1])} ') if i[0] == '按鍵': print(f'第{zizeng}步:\n執(zhí)行按鍵動(dòng)作,將同時(shí)按下”{i[1]}“鍵') zizeng += 1 def daoru(): caozuojiyi = get_caozuo_name() while True: xunhuan_choose = input('已存儲(chǔ)的操作有:{}\n請輸入導(dǎo)入操作的操作名:'.format(caozuojiyi)) if xunhuan_choose in caozuojiyi: break else: print('存儲(chǔ)庫中并無你想要的操作,請重新輸入:') caozuopath = r'{}.txt'.format(xunhuan_choose) step1 = txttolist(caozuopath) print(step1) return [step1, xunhuan_choose] def jianshi(): global state, rgb, rgb_xy, xiancheng while 1: aa = GetColor(rgb_xy) if aa == rgb: try: stop_thread(xiancheng) finally: state = '觸發(fā)' print(f'檢測到{rgb_xy}位置的RGB值變?yōu)閧aa}') break def zhixingbuzhoukuai(buzhou, bu, xunhuanyn): global state print(f'正在執(zhí)行"{buzhou}"代碼塊的操作') state = '未觸發(fā)' if bu == '1': while 1: if state == '觸發(fā)': break if state == '未觸發(fā)': timer(0.1) elif xunhuanyn == '循環(huán)': while 1: if state == '觸發(fā)': break if state == '未觸發(fā)': zhixing(bu) elif xunhuanyn == '不循環(huán)': zhixing(bu) def listtotxt(list, path): import json c_list = list c_list = json.dumps(c_list) '''將c_list存入文件 ''' a = open(path, "w", encoding='UTF-8') a.write(c_list) a.close() print('已存入txt') def txttolist(path): import json b = open(path, "r", encoding='UTF-8') out = b.read() out = json.loads(out) return out rgb_xy = [] rgb = [] state = '未觸發(fā)' panduans = [] step = [] while True: choose = input('請輸入你需要使用的功能:') if choose == '點(diǎn)擊': click = [] while 1: click_dongzuo = input('請移動(dòng)鼠標(biāo)到目標(biāo)位置上輸入“1”完成動(dòng)作,輸入“0”取消動(dòng)作') if click_dongzuo == '1': click_weizhi = get_xy() click.append('點(diǎn)擊') click.append(click_weizhi) step.append(click) break elif click_dongzuo == '0': print('操作已取消') break else: print('請輸入正確的操作(輸入“0”或“1”') if choose == '延時(shí)': while 1: timerr = [] try: timex = int(input('請輸入延時(shí)時(shí)間:')) timerr.append('延時(shí)') timerr.append(timex) step.append(timerr) break except: print('延時(shí)失敗/n請輸入正確的延時(shí)時(shí)間') continue if choose == '連點(diǎn)': click_liandian = [] while 1: click_dongzuo = input('請移動(dòng)鼠標(biāo)到目標(biāo)位置上輸入“1”完成動(dòng)作,輸入“0”取消動(dòng)作') if click_dongzuo == '1': click_weizhi = get_xy() click_liandian.append('連點(diǎn)') click_liandian.append(click_weizhi) break elif click_dongzuo == '0': print('操作已取消') break else: print('請輸入正確的操作(輸入“0”或“1”') click_pinlv = float(input('請輸入連點(diǎn)頻率:')) while 1: click_stop_choose = input('“連點(diǎn)次數(shù)”or“連點(diǎn)時(shí)長”') if click_stop_choose == '連點(diǎn)次數(shù)': click_cishu = int(input('請輸入連點(diǎn)次數(shù):')) click_liandian.append('連點(diǎn)次數(shù)') click_liandian.append(click_cishu) click_liandian.append(click_pinlv) step.append(click_liandian) print(click_liandian) print(step) break if click_stop_choose == '連點(diǎn)時(shí)長': click_shichang = int(input('請輸入連點(diǎn)時(shí)長(秒):')) click_liandian.append('連點(diǎn)時(shí)長') click_liandian.append(click_shichang) step.append(click_liandian) click_liandian.append(click_pinlv) print(click_liandian) print(step) break else: continue if choose == '存儲(chǔ)': if len(step) == 0: print('還未記錄你任何操作,請?zhí)砑硬僮髟偈褂么鎯?chǔ)功能') else: do_name = input('請為以上操作命名吧:') path = r"{}.txt".format(do_name) listtotxt(list=step, path=path) if choose == '拖拽': tuozhuai = [] while 1: click_dongzuo = input('請移動(dòng)鼠標(biāo)到目標(biāo)位置上輸入“1”完成移動(dòng)前的位置輸入,輸入“0”取消動(dòng)作') if click_dongzuo == '1': click_weizhi = get_xy() tuozhuai.append('拖拽') tuozhuai.append(click_weizhi) while 1: click_dongzuo = input('請移動(dòng)鼠標(biāo)到目標(biāo)位置上輸入“1”完成移動(dòng)后的位置輸入,輸入“0”取消動(dòng)作') if click_dongzuo == '1': click_weizhi = get_xy() tuozhuai.append(click_weizhi) break elif click_dongzuo == '0': print('操作已取消') break else: print('請輸入正確的操作(輸入“0”或“1”') break elif click_dongzuo == '0': print('操作已取消') break else: print('請輸入正確的操作(輸入“0”或“1”') step.append(tuozhuai) if choose == '循環(huán)執(zhí)行': while 1: xunhuan_cishu_zhixing = 0 xunhuan_cishu = input('請輸入循環(huán)次數(shù)(如要無限循環(huán)請輸入"無限"):') if xunhuan_cishu == '無限': while True: zhixing(step) if xunhuan_cishu.isdigit(): for i in range(int(xunhuan_cishu)): xunhuan_cishu_zhixing += 1 zhixing(step) print(f'已完成{xunhuan_cishu_zhixing}次循環(huán)') break else: print('你的輸入有誤,請重新輸入:') if choose == '導(dǎo)入': if len(step) == 0: step = daoru()[0] else: baocun_choose = input('此次操作若未保存請先,導(dǎo)入別的操作會(huì)覆蓋原來的操作,你確定要導(dǎo)入嗎?\n請輸入“yes”or“no”:\n') while 1: if baocun_choose == 'no': break if baocun_choose == 'yes': print('你已取消保存') step = daoru()[0] break else: yorn = input("請輸入'yes'or'no':\n") if choose == '輸入': shuru = [] while 1: click_dongzuo = input('請移動(dòng)鼠標(biāo)到你要輸入的位置上輸入“1”完成動(dòng)作,輸入“0”取消動(dòng)作') if click_dongzuo == '1': click_weizhi = get_xy() txt_in = input('請輸入你要在該位置輸入的文字:\n') shuru.append('輸入') shuru.append(click_weizhi) shuru.append(txt_in) step.append(shuru) break elif click_dongzuo == '0': print('操作已取消') break else: print('請輸入正確的操作(輸入“0”或“1”') if choose == '按鍵': while 1: anjian = input('這是模擬操作鍵盤的操作(例如復(fù)制,輸入'ctrl‘ + 'c‘):\n') if anjian != 'q': anjian = anjian.split('+') anjians = [] a = [] for an in anjian: an = an.replace("‘", "").replace("'", "").strip() if an in pyautogui.KEYBOARD_KEYS: anjians.append(an) nihaofan = 0 else: print('你的輸入不合法') nihaofan = 1 break if nihaofan == 0: a.append('按鍵') a.append(anjians) step.append(a) print('錄入成功') break if anjian == 'q': break if choose == '雙擊': click_double = [] while 1: click_dongzuo = input('請移動(dòng)鼠標(biāo)到目標(biāo)位置上輸入“1”完成動(dòng)作,輸入“0”取消動(dòng)作') if click_dongzuo == '1': click_weizhi = get_xy() click_double.append('雙擊') click_double.append(click_weizhi) step.append(click_double) break elif click_dongzuo == '0': print('操作已取消') break else: print('請輸入正確的操作(輸入“0”或“1”') if choose == '滾動(dòng)': while 1: gundong = [] try: gundong1 = int(input('這里是模擬鼠標(biāo)滾動(dòng),請輸入你要滾動(dòng)距離(正數(shù)為向上移動(dòng),負(fù)數(shù)為向下移動(dòng)):\n')) gundong.append('滾動(dòng)') gundong.append(gundong1) step.append(gundong) break except: print('你的輸入有誤,請重新輸入') if choose == '查看': chakan() if choose == '右擊': click_r = [] while 1: click_dongzuo = input('請移動(dòng)鼠標(biāo)到目標(biāo)位置上輸入“1”完成動(dòng)作,輸入“0”取消動(dòng)作') if click_dongzuo == '1': click_weizhi = get_xy() click_r.append('右擊') click_r.append(click_weizhi) step.append(click_r) break elif click_dongzuo == '0': print('操作已取消') break else: print('請輸入正確的操作(輸入“0”或“1”') if choose == '中擊': click_m = [] while 1: click_dongzuo = input('請移動(dòng)鼠標(biāo)到目標(biāo)位置上輸入“1”完成動(dòng)作,輸入“0”取消動(dòng)作') if click_dongzuo == '1': click_weizhi = get_xy() click_m.append('中擊') click_m.append(click_weizhi) step.append(click_m) break elif click_dongzuo == '0': print('操作已取消') break else: print('請輸入正確的操作(輸入“0”或“1”') if choose == '執(zhí)行': if len(step) == 0: print('你還未記錄任何操作,請至少記錄了一個(gè)操作再執(zhí)行') else: zhixing(step) cunchu() if choose == '判斷': if len(panduans) == 0: tuichu = 0 panduan = input('此功能的實(shí)現(xiàn)是基于顏色的RBG值來判斷程序所要要執(zhí)行的步驟塊。\n現(xiàn)在,請選擇你的‘先執(zhí)行步驟塊等待條件觸發(fā)'或是‘直接等待條件觸發(fā)'的操作:(輸入"步驟塊"或"等待")\n') if panduan == '如果': panduan_if = [] while 1: click_dongzuo = input('請移動(dòng)鼠標(biāo)到目標(biāo)位置上吸取顏色,輸入“1”完成動(dòng)作,輸入“0”取消動(dòng)作') if click_dongzuo == '1': xy = get_xy() click_color = GetColor(xy) panduan_yn = input(f'這個(gè)位置的RGB為:{click_color},是否確定為下一步驟塊的判斷根據(jù)?(輸入"yes"or"no")\n') while 1: if panduan_yn == 'yes': get_caozuo_name() print(f'請選擇滿足當(dāng)顏色為{click_color}時(shí)要執(zhí)行的步驟包:') steps, steps_name = daoru() xunhuan_yn = input('這個(gè)步驟塊是否循環(huán)執(zhí)行至下一條件觸發(fā)?(輸入"yes"or"no")\n') while 1: if xunhuan_yn == 'yes': panduan_if.append('如果') panduan_if.append(xy) panduan_if.append(click_color) panduan_if.append(steps_name) panduan_if.append('循環(huán)') panduan_if.append(steps) panduans.append(panduan_if) print('添加成功,該步驟包將會(huì)循環(huán)') break elif xunhuan_yn == 'no': panduan_if.append('如果') panduan_if.append(xy) panduan_if.append(click_color) panduan_if.append(steps_name) panduan_if.append('不循環(huán)') panduan_if.append(steps) panduans.append(panduan_if) print('添加成功,該步驟包將只執(zhí)行一次') break else: xunhuan_yn = input('你的輸入有誤,請輸入"yes"or"no":') tuichu = 1 break if panduan_yn == 'no': print('請重新選擇') break else: panduan_yn = input('你的輸入有誤,請輸入"yes"or"no"') if tuichu == 1: break elif click_dongzuo == '0': print('操作已取消') break else: print('請輸入正確的操作(輸入“0”或“1”)') if panduan == '步驟塊': panduan_step = [] steps, steps_name = daoru() xunhuan_yn = input('這個(gè)步驟塊是否循環(huán)執(zhí)行直至條件觸發(fā)?(輸入"yes"or"no")\n') while 1: if xunhuan_yn == 'yes': panduan_step.append('步驟塊') panduan_step.append('循環(huán)') panduan_step.append(steps_name) panduan_step.append(steps) panduans.append(panduan_step) break elif xunhuan_yn == 'no': panduan_step.append('步驟塊') panduan_step.append('不循環(huán)') panduan_step.append(steps_name) panduan_step.append(steps) panduans.append(panduan_step) break else: xunhuan_yn = input('你的輸入有誤,請輸入"yes"or"no":') if panduan == '等待': panduan_if = [] print('你選擇了等待,程序?qū)r(shí)刻檢測目標(biāo)位置的顏色以執(zhí)行接下來的步驟塊') panduan_if.append('等待') panduans.append(panduan_if) if panduan != '步驟塊' and panduan != '如果' and panduan != '等待': print('你的輸入有誤') if len(panduans) > 0: print('你一錄入了至少一個(gè)邏輯判斷,請選擇繼續(xù)選擇目標(biāo)位置的顏色來觸發(fā)接下來你選擇的步驟塊') panduan_if = [] while 1: click_dongzuo = input('請移動(dòng)鼠標(biāo)到目標(biāo)位置上吸取顏色,輸入“1”完成動(dòng)作,輸入“0”取消動(dòng)作') if click_dongzuo == '1': xy = get_xy() click_color = GetColor(xy) panduan_yn = input(f'這個(gè)位置的RGB為:{click_color},是否確定為下一步驟塊的判斷根據(jù)?(輸入"yes"or"no")\n') while 1: if panduan_yn == 'yes': get_caozuo_name() print(f'請選擇滿足當(dāng)顏色為{click_color}時(shí)要執(zhí)行的步驟包:') steps, steps_name = daoru() xunhuan_yn = input('這個(gè)步驟塊是否循環(huán)執(zhí)行直至條件觸發(fā)?(輸入"yes"or"no")\n') while 1: if xunhuan_yn == 'yes': panduan_if.append('如果') panduan_if.append(xy) panduan_if.append(click_color) panduan_if.append(steps_name) panduan_if.append('循環(huán)') panduan_if.append(steps) panduans.append(panduan_if) print('添加成功,該步驟包將會(huì)循環(huán)') break elif xunhuan_yn == 'no': panduan_if.append('如果') panduan_if.append(xy) panduan_if.append(click_color) panduan_if.append(steps_name) panduan_if.append('不循環(huán)') panduan_if.append(steps) panduans.append(panduan_if) print('添加成功,該步驟包將只執(zhí)行一次') break else: xunhuan_yn = input('你的輸入有誤,請輸入"yes"or"no":') tuichu = 1 break if panduan_yn == 'no': print('請重新選擇') break else: panduan_yn = input('你的輸入有誤,請輸入"yes"or"no"') if tuichu == 1: break elif click_dongzuo == '0': print('操作已取消') break else: print('請輸入正確的操作(輸入“0”或“1”)') if choose == '邏輯執(zhí)行': print('這里是邏輯執(zhí)行庫,所有的邏輯判斷都會(huì)存儲(chǔ)到這里') print(panduans) xiancheng = threading.Thread(target=zhixingbuzhoukuai, args=('等待', '1', '循環(huán)')) xiancheng.setDaemon(True) xiancheng.start() for pan in panduans: state = '未觸發(fā)' if pan[0] == '如果': print(pan[5]) print(len(pan[5])) bu = str(pan[5]) print(bu) bu = readListFromStr(bu) zhixing(bu) print(bu) if state == '未觸發(fā)': if pan[4] == '循環(huán)': rgb = pan[2] rgb_xy = pan[1] _thread.start_new_thread(jianshi, ()) while 1: if state == '觸發(fā)': xiancheng = threading.Thread(target=zhixingbuzhoukuai, args=(pan[3], pan[5], '循環(huán)')) xiancheng.start() state = '未觸發(fā)' break if pan[4] == '不循環(huán)': rgb = pan[2] rgb_xy = pan[1] _thread.start_new_thread(jianshi, ()) while 1: if state == '觸發(fā)': xiancheng = threading.Thread(target=zhixingbuzhoukuai, args=(pan[3], pan[5], '不循環(huán)')) xiancheng.start() state = '未觸發(fā)' break if pan[0] == '步驟塊': stop_thread(xiancheng) if pan[1] == '循環(huán)': xiancheng = threading.Thread(target=zhixingbuzhoukuai, args=(pan[2], pan[3], '循環(huán)')) xiancheng.start() if pan[1] == '不循環(huán)': xiancheng = threading.Thread(target=zhixingbuzhoukuai, args=(pan[2], pan[3], '不循環(huán)')) xiancheng.start() if pan[0] == '等待': print('程序正在監(jiān)測目標(biāo)位置RGB值') print('邏輯執(zhí)行已全部執(zhí)行完畢') break if choose == '邏輯塊存儲(chǔ)': yorn = input("確定保存?\n輸入'yes'or'no'\n") while 1: if yorn == 'yes': if len(panduans) == 0: print('還未記錄你任何操作,請?zhí)砑硬僮髟偈褂眠壿媺K存儲(chǔ)功能') else: do_name = input('請為以上操作命名吧:') if '邏輯塊存儲(chǔ)' in do_name: do_name = input('抱歉,你的命名里不允許包含”邏輯塊存儲(chǔ)“,請重新命名') else: path = r"{}邏輯塊存儲(chǔ).txt".format(do_name) listtotxt(list=panduans, path=path) break if yorn == 'no': print('你已取消存儲(chǔ)') break else: yorn = input("請輸入'yes'or'no':\n") if choose == '邏輯塊導(dǎo)入': caozuojiyi = get_caozuokuai_name() while True: xunhuan_choose = input('已存儲(chǔ)的操作有:{}\n請輸入導(dǎo)入操作的操作名:'.format(caozuojiyi)) if xunhuan_choose in caozuojiyi: break else: print('邏輯塊存儲(chǔ)庫中并無你想要的操作,請重新輸入:') caozuopath = r"{}邏輯塊存儲(chǔ).txt".format(xunhuan_choose) panduans = txttolist(path=caozuopath) if choose == 'q' or choose == 'quit' or choose == '退出' or choose == 'close': break if choose == 'tips' or choose == '提示' or choose == 'help' or choose == '幫助': print( '''你可以輸入'點(diǎn)擊', '右擊', '中擊', '邏輯執(zhí)行', '判斷', '滾動(dòng)', '延時(shí)', '存儲(chǔ)', '執(zhí)行', '循環(huán)執(zhí)行', '拖拽', '連點(diǎn)', '輸入', '雙擊', '查看', '導(dǎo)入', 'q', 'quit','退出', 'close', 'tips', '提示', 'help', '幫助', '按鍵'來幫助你完成你的自動(dòng)化操作''') if not choose in ['點(diǎn)擊', '右擊', '中擊', '邏輯執(zhí)行', '判斷', '滾動(dòng)', '延時(shí)', '存儲(chǔ)', '執(zhí)行', '循環(huán)執(zhí)行', '拖拽', '連點(diǎn)', '輸入', '雙擊', '查看', '導(dǎo)入', 'q', 'quit','退出', 'close', 'tips', '提示', 'help', '幫助', '按鍵']: print('你的輸入有誤或暫未開發(fā)此功能,請重新輸入(輸入”help“獲得提示)') print('代碼已全部執(zhí)行完畢,程序已退出')
這是我的2.0版本,之前把邏輯板塊之外的功能都寫出來了之后迫不及待地玩了一下,幫朋友買了四價(jià),做了微信信息轟炸的程序,看著鼠標(biāo)把文件夾拖來拖去,做了些拳皇腳本打出超級連招,等會(huì)我再試一下盲僧的馬氏三角殺哈哈哈,想想就興奮~~
本來還想做多點(diǎn)功能的,比如:檢測目標(biāo)區(qū)域的文字來執(zhí)行判斷操作(這聽起來不難);
寫個(gè)語音輸入功能,當(dāng)執(zhí)行完什么操作了就讓電腦說:“你好厲害啊”、“真的是我的偶像”、“快來看看我搶到四價(jià)了”(現(xiàn)在我就能寫出來);
import pyttsx3 engine = pyttsx3.init() engine.say('我搶到了!快來看一下') engine.runAndWait()
以上就是關(guān)于“怎么利用Python實(shí)現(xiàn)自定義連點(diǎn)器”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。