这主要根据tkinter,pyautogui,traceback编写的一个程序,下面是代码,我就不介绍了,因为有注释,所以你们应该能看懂
#coding=gbk
import tkinter as t
from tkinter.ttk import *
import pyautogui,random,time,threading,traceback,csv,os
from tkinter.messagebox import *
import ctypes
"""
更改任务栏图标,任务栏图标等于窗口图标
"""
myappid = 'mycompany.myproduct.subproduct.version'
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
keyboard = [chr(i) for i in range(32, 127)] + ["alt", "shift", "winleft", "capslock",
"numlock", "tab", "esc", "space", "backspace", "delete", "scrolllock",
"insert",
"ctrl", "enter", "fn", "home", "end", "pageup", "pagedown", "left",
"right", "up",
"down", "volumeup", "volumedown", "volumemute"] + ["f" + str(i) for i in
range(1, 13)]
keys = "shift ctrl esc", "ctrl s", "ctrl a", "ctrl c", "ctrl v", "alt f4", "ctrl n", "winleft d", \
"shift ctrl tab", "winleft b", "winleft l", "winleft f", "winleft m", "winleft shift m", \
"winleft r", "shift del", "shift alt", "shift volumemute", "shift volumedown", "shift volumeup", \
"shift scrolllock", "shift printscreen", "ctrl f", "winleft up", "winleft down", "winleft pagedn", "winleft pageup", \
"winleft crtl f", "winleft pause", "winleft u", "ctrl z", "ctrl space", "alt shift", "ctrl home", "ctrl end", "alt t"
allkeys=list(keys)+keyboard
def write_csv_key():
"""
将所有按键写入csv
:return:
"""
with open("keyboards.csv","a+") as f:
a=csv.writer(f)
a.writerow(allkeys)
def read_csv_key():
"""
从csv中读取按键列表
:return:
"""
if os.path.exists(os.path.join(os.getcwd(),"keyboards.csv"))==False:
write_csv_key()
try:
with open("keyboards.csv", "r") as f:
a = csv.reader(f)
b=list(a)
if len(b)==0:
write_csv_key()
with open("keyboards.csv", "r") as f:
a = csv.reader(f)
b=list(a)
return b[0]
except:
catch_E()
return False
def start_fun(fun):
"""
公共确定模块,用于开启线程
:param fun: 函数
:return:
"""
que=askokcancel("确定","你确定吗?")
if que:
t=threading.Thread(target=fun)
t.setDaemon(True)
t.start()
def start_allfunciton():
start_fun(allfunction)
def catch_E():
"""
处理异常
:return:
"""
nowtime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
errinfo = traceback.format_exc() # 打印异常信息 # G弹窗显示异常信息,这就不显示了
with open("error.txt", "a+") as f: # 将异常信息记录文件
f.write(nowtime + "\n" + errinfo + "\n")
def mange_range(tt,scope):
"""
将范围和时间输入的值转成整数
:param tt: 时间变量,即输入时间的文本框变量
:param scope: 范围变量,即输入范围的文本框变量
:return: 范围时间,范围的一个列表
"""
x,y=pyautogui.size()
try:
times = int(tt.get())
x1 = int(scope.get().split(",")[0])
y1 = int(scope.get().split(",")[1])
x2 = int(scope.get().split(",")[2])
y2 = int(scope.get().split(",")[3])
if x>=x1>0 and x>=x2>0 and y>=y1>0 and y>=y2>0 and times >0:
if x1<x2 and y1<y2:
a=[times,x1,y1,x2,y2]
return a
else:
showinfo("警告","请注意格式")
return False
else:
showwarning("警告",f"请输入正整数\n并且{x}>=水平范围>0\n{y}>=竖直范围在>0")
return False
except:
catch_E()
showwarning("警告","请重新输入")
return False
def hotkeys(*args):
"""
根据数组里的按键个数,一个一个的按下,松开
:param args: 按键数组
:return:
"""
if type(args[0])!=str:
for i in args[0]:
for j in i.split(" "):
pyautogui.keyDown(j)
for k in i.split(" "):
pyautogui.keyUp(k)
else:
for i in args[0].split(" "):
pyautogui.keyDown(i)
for k in args[0].split(" "):
pyautogui.keyDown(k)
def allfunction():
"""
根据勾选的框,和次数,进行随机操作
:return:
"""
seed=[]
#获取勾选的列表
for i in se:
if i.get():
seed.append(i)
vals = mange_range(otime7, scope4)
if vals!=False:
if seed !=[]:
numa = [random.randint(0, len(seed)) for i in range(vals[0])] # 用于生成随机的动作数字列表
numdict = {1: "移动", 2: "拖拽", 3: "键盘", 4: "鼠标",5:"滚轮"}
acts = [numdict.get(i) for i in numa]
print(f"这是移动的数量{acts.count('移动')}"
f"\n这是拖拽的数量{acts.count('拖拽')}"
f"\n这是键盘的数量{acts.count('键盘')}"
f"\n这是鼠标的数量{acts.count('鼠标')}")
f"\n这是滚轮的数量{acts.count('滚轮')}"
for i in acts:
if i == "移动":
pyautogui.moveTo(random.randint(vals[1], vals[3]), random.randint(vals[2], vals[4]))
elif i == "拖拽":
pyautogui.dragTo(random.randint(vals[1], vals[3]), random.randint(vals[2], vals[4]))
elif i == "键盘":
if read_csv_key() !=False:
hotkeys(random.choice(read_csv_key()))
else:
hotkeys(random.choice(allkeys))
elif i == "鼠标":
pyautogui.click(random.randint(vals[1], vals[3]), random.randint(vals[2], vals[4]),
button=random.choice(("left", "right", "middle")), clicks=random.randint(1, 4),
interval=0.1)
elif i == '滚轮':
pyautogui.scroll(random.randint(-1000,1000))
showinfo("提示","已经全部完成了")
else:
showwarning("警告","请选择动作")
win=t.Tk()
fonts="微软雅黑 20"
win.title("monkey")
win.iconbitmap("气球.ico")
k=LabelFrame(win,text="随机测试")
l=Frame(k)
aaa=("移动","拖拽","键盘","鼠标","滚轮")
se=[]#用于存放多选框的变量
"""
全部,pycharm
"""
select=Label(k,text="请选择你要模拟的操作",font=fonts)
select.grid(row=1,column=0,columnspan=4,sticky="w")
for i in range(0,5):
Vars = t.BooleanVar()
actions=t.Checkbutton(k,text=aaa[i],variable=Vars,font=fonts)
se.append(Vars)
actions.grid(row=2,column=i)
ran4=Label(l,text="请输入移动的范围:",font=fonts)
ran4.grid(row=0,column=0,sticky="w",columnspan=2)
scope4=Entry(l,font=fonts,width=20)
scope4.grid(row=0,column=2,sticky="w",columnspan=3)
itime7=Label(l, text="请输入随机的次数:",font=fonts)
itime7.grid(row=1, column=0,sticky="w",columnspan=2)
otime7=Entry(l,font=fonts,width=20)
otime7.grid(row=1,column=2,sticky="w",columnspan=3)
tb5=t.Button(k,text="确定",command=start_allfunciton,font=fonts,width=39,relief="groove")
tb5.grid(row=3,column=0,columnspan=5,sticky="w")
l.grid(row=0,column=0,columnspan=4)
k.grid(row=0,column=0,columnspan=4)
t.mainloop()