前言:上一期我们编写了基于tkinter和pynput的微信信息轰炸器(进阶版)。但是,在后续的使用过程中,我发现了几个问题:一,轰炸器虽然能够实时锁定微信输入框位置并能对识别结果进行处理,但这个处理结果常常与轰炸器在处理用户输入的文本的结果存在逻辑混乱的现象;二,轰炸器一但没能锁定微信输入框位置(无论是微信输入框在屏幕上但识别错误,还是微信输入框不在屏幕上无法识别),都会导致轰炸器自动关闭,这明显不符合我们希望轰炸器在给出相应报错提示,用户作出对应调整后轰炸器依然处于打开状态。因此,我又对上一期的代码进行了部分片段的优化和调整,以更符合我们的使用习惯。
一:优化及调整
1,优化部分
优化后:
# 对话框位置获取区
def get_dialog_position():
try:
image_file_1 = 'smile.png'
image_file_2 = 'cutter.png'
location_1 = pyautogui.locateOnScreen(image_file_1)
location_2 = pyautogui.locateOnScreen(image_file_2)
x_position = pyautogui.center(location_1)
y_position = pyautogui.center(location_2)
list_1 = [x_position, y_position]
except pyautogui.ImageNotFoundException:
list_1=[]
return list_1
优化前:
#对话框位置获取区
try:
image_file_1 = 'smile.png'
image_file_2 = 'cutter.png'
location_1 = pyautogui.locateOnScreen(image_file_1)
location_2 = pyautogui.locateOnScreen(image_file_2)
x_position = pyautogui.center(location_1)
y_position = pyautogui.center(location_2)
except pyautogui.ImageNotFoundException:
messagebox.showerror('系统提示','未识别到微信窗口,请先保证微信对话框在屏幕上')
exit()
2,调整部分
调整后
def command_start(): # 命令执行键(Command)
try:
if get_dialog_position()==[]:
messagebox.showerror('系统提示',
'未识别到微信窗口,请先保证微信对话框在屏幕上')
else:
count_number = eval(entry_1.get())
my_mouse = mouse.Controller()
my_keyboard = keyboard.Controller()
my_mouse.position = (get_dialog_position()[0][0],
get_dialog_position()[0][1]
+ int((get_dialog_position()[1][0]
- get_dialog_position()[0][0]) / 2))
time.sleep(1)
my_mouse.click(mouse.Button.left)
content = entry_2.get()
time_food = eval(entry_3.get())
for i in range(count_number):
my_keyboard.type('{}'.format(content))
my_keyboard.press(keyboard.Key.enter)
my_keyboard.release(keyboard.Key.enter)
time.sleep(time_food)
except:
messagebox.showwarning('系统提示', '输入有误,请仔细检查输入内容')
调整前:
def command_start(): #命令执行键(Command)
try:
count_number = eval(entry_1.get())
my_mouse = mouse.Controller()
my_keyboard = keyboard.Controller()
my_mouse.position = (x_position[0],
x_position[1]+int((y_position[0]-x_position[0])/2))
time.sleep(1)
my_mouse.click(mouse.Button.left)
content = entry_2.get()
time_food = eval(entry_3.get())
for i in range(count_number + 1):
my_keyboard.type('{}'.format(content))
my_keyboard.press(keyboard.Key.enter)
my_keyboard.release(keyboard.Key.enter)
time.sleep(time_food)
except:
messagebox.showwarning('系统提示','输入有误,请仔细检查输入内容')
二,完整代码
# 导入库区
import time
from pynput import keyboard, mouse
from tkinter import messagebox
import tkinter as tk
import pyautogui
# 对话框位置获取区
def get_dialog_position():
try:
image_file_1 = 'smile.png'
image_file_2 = 'cutter.png'
location_1 = pyautogui.locateOnScreen(image_file_1)
location_2 = pyautogui.locateOnScreen(image_file_2)
x_position = pyautogui.center(location_1)
y_position = pyautogui.center(location_2)
list_1 = [x_position, y_position]
except pyautogui.ImageNotFoundException:
list_1=[]
return list_1
# 窗口主体设置区
w = tk.Tk()
w.title('微信信息轰炸器')
screen_width = w.winfo_screenwidth()
screen_height = w.winfo_screenheight()
width = 922
height = 200
x = int((screen_width - width) / 2)
y = int((screen_height - height) / 2)
w.geometry('{}x{}+{}+{}'.format(
width, height,
x, y))
w.iconbitmap('warning')
w.configure(bg='white')
w.resizable(False, False)
# 文本框设置区
var_1 = tk.StringVar()
var_2 = tk.StringVar()
var_3 = tk.StringVar()
entry_1 = tk.Entry(w, relief='ridge', textvariable=var_1,
highlightcolor='red',
highlightthickness=1,
font=('Arial', 10))
entry_1.place(width=100, height=45, x=100, y=40)
label_1 = tk.Label(w, text='轰炸次数:',
font=('Arial', 10),
bg='pink')
label_1.place(width=85, height=45, x=10, y=40)
entry_1.insert(0, '示例:12')
entry_2 = tk.Entry(w, relief='ridge', textvariable=var_2,
highlightcolor='red',
highlightthickness=1,
font=('Arial', 10))
entry_2.place(width=350, height=45, x=330, y=40)
label_2 = tk.Label(w, text='轰炸内容:',
font=('Arial', 10),
bg='gold')
label_2.place(width=85, height=45, x=240, y=40)
entry_2.insert(0, '示例:Hello World!')
entry_3 = tk.Entry(w, relief='ridge', textvariable=var_3,
highlightcolor='red',
highlightthickness=1,
font=('Arial', 10))
entry_3.place(width=100, height=45, x=810, y=40)
label_3 = tk.Label(w, text='轰炸间隔(/秒):',
font=('Arial', 10),
bg='tomato')
label_3.place(width=85, height=45, x=720, y=40)
entry_3.insert(0, '>=0.1即可')
# 内部函数区
state = 0
def button_close_win(): # 关闭窗口键(Destroy)
w.destroy()
def button_clean_up(): # 轰炸次数归零键(Reset)
var_1.set('')
def button_clear_up(): # 轰炸内容归零键(Reset)
var_2.set('')
def button_delete_1(new_text): # 轰炸次数删除键(Delete)
start_text = entry_1.get()
length = len(start_text)
entry_1.delete(length - 1, tk.END)
entry_1.insert(length - 1, new_text)
def button_delete_2(new_text): # 轰炸内容删除键(Delete)
start_text = entry_2.get()
length = len(start_text)
entry_2.delete(length - 1, tk.END)
entry_2.insert(length - 1, new_text)
def change_focus(): # 焦点切换键(Change)
global state
if state == 0:
entry_1.focus_set()
state = 1
elif state == 1:
entry_2.focus_set()
state = 2
elif state == 2:
entry_3.focus_set()
state = 0
def command_start(): # 命令执行键(Command)
try:
if get_dialog_position()==[]:
messagebox.showerror('系统提示',
'未识别到微信窗口,请先保证微信对话框在屏幕上')
else:
count_number = eval(entry_1.get())
my_mouse = mouse.Controller()
my_keyboard = keyboard.Controller()
my_mouse.position = (get_dialog_position()[0][0],
get_dialog_position()[0][1]
+ int((get_dialog_position()[1][0]
- get_dialog_position()[0][0]) / 2))
time.sleep(1)
my_mouse.click(mouse.Button.left)
content = entry_2.get()
time_food = eval(entry_3.get())
for i in range(count_number):
my_keyboard.type('{}'.format(content))
my_keyboard.press(keyboard.Key.enter)
my_keyboard.release(keyboard.Key.enter)
time.sleep(time_food)
except:
messagebox.showwarning('系统提示', '输入有误,请仔细检查输入内容')
# 按钮设置区
button_1 = tk.Button(w, text='次数删除键',
font=('Arial', 10),
command=lambda: button_delete_1(''),
bg='salmon', bd=6,
fg='black')
button_1.place(width=100, height=45, x=50, y=130)
button_3 = tk.Button(w, text='次数归零键',
font=('Arial', 10),
command=lambda: button_clean_up(),
bg='plum', bd=6,
fg='black')
button_3.place(width=100, height=45, x=170, y=130)
button_8 = tk.Button(w, text='内容删除键',
font=('Arial', 10),
command=lambda: button_delete_2(''),
bg='orange', bd=6,
fg='black')
button_8.place(width=100, height=45, x=290, y=130)
button_4 = tk.Button(w, text='内容归零键',
font=('Arial', 10),
command=lambda: button_clear_up(),
bg='teal', bd=6,
fg='black')
button_4.place(width=100, height=45, x=410, y=130)
button_5 = tk.Button(w, text='关闭窗口键',
font=('Arial', 10),
command=lambda: button_close_win(),
bg='cyan', bd=6,
fg='black')
button_5.place(width=100, height=45, x=530, y=130)
button_6 = tk.Button(w, text='切换焦点键',
font=('Arial', 10),
command=lambda: change_focus(),
bg='yellow', bd=6,
fg='black')
button_6.place(width=100, height=45, x=650, y=130)
button_7 = tk.Button(w, text='命令执行键',
font=('Arial', 10),
command=lambda: command_start(),
bg='red', bd=6,
fg='black')
button_7.place(width=100, height=45, x=770, y=130)
# 主循环区
w.mainloop()
三,运行效果展示
(轰炸器这块可能就此结束了,感谢你的阅读,还望点赞/关注支持一下)
标签:tkinter,get,Python,微信,button,keyboard,position,entry,my From: https://blog.csdn.net/2401_83954530/article/details/144594182