用python写一个自动发消息的小程序。并且可以在界面上设置发送消息的循环次数,每次循环的时间间隔和发送消息的内容。
注:需要python具有模块pynput
(点击开始运行,输入法切换为英文,把光标聚焦到需要发送的地方,比如微信)
输入循环次数: 设置消息发送的次数
每次循环间隔(ms): 设置发送一条消息的时间间隔500ms就是半秒发送一条
输入消息内容: 如果消息内容是一个存在的文件路径,则会一行一行读取文件内容进行发送,否则就发送输入框的文本内容
开始运行: 点击开始运行会进入五秒钟的到倒计时,倒计时结束后会自动打印消息内容到光标聚焦的位置,点击结束运行则会结束。
完整源码:
import time
import os
from pynput import mouse, keyboard
import tkinter as tk
import threading
def msg_send_loop():
global startflag
global num_loops
global step_loops
global msg_loops
m_mouse = mouse.Controller()
m_keyboard = keyboard.Controller()
i = 5
while(startflag):
str="准备... {}s"
button.configure(text=str.format(i))
time.sleep(1)
i -= 1
if i <= 0 :
break
button.configure(text="结束运行")
i = 1
while(startflag):
if os.path.isfile(msg_loops) :
f = open(msg_loops, 'r',encoding='utf-8')
for line in f:
m_keyboard.type(line)
time.sleep(step_loops/1000)
i += 1
if i > num_loops or startflag != True:
break
m_keyboard.press(keyboard.Key.enter)
m_keyboard.release(keyboard.Key.enter)
f.close()
else :
m_keyboard.type(msg_loops)
m_keyboard.press(keyboard.Key.enter)
m_keyboard.release(keyboard.Key.enter)
time.sleep(step_loops/1000)
i += 1
if i > num_loops :
break
startflag = False
button.configure(text="开始运行")
entry.config(state='normal')
entry1.config(state='normal')
entry2.config(state='normal')
def start_program():
global startflag
global num_loops
global step_loops
global msg_loops
num_loops = int(entry.get())
step_loops = int(entry1.get())
msg_loops = str(entry2.get())
if startflag :
startflag = False
button.configure(text="开始运行")
else :
startflag = True
button.configure(text="结束运行")
entry.config(state='disabled')
entry1.config(state='disabled')
entry2.config(state='disabled')
button.focus_set()
t = threading.Thread(target = msg_send_loop)
t.start()
if __name__ == "__main__":
root = tk.Tk()
root.geometry("250x180")
label = tk.Label(root, text="输入循环次数:")
label.pack()
entry = tk.Entry(root,textvariable=tk.StringVar(value="10"))
entry.pack()
label1 = tk.Label(root, text="每次循环间隔(ms):")
label1.pack()
entry1 = tk.Entry(root,textvariable=tk.StringVar(value="500"))
entry1.pack()
label2 = tk.Label(root, text="输入消息内容:")
label2.pack()
entry2 = tk.Entry(root,textvariable=tk.StringVar(value="msg loops"))
entry2.pack()
startflag=False
button = tk.Button(root, text="开始运行", command=start_program)
button.pack()
root.attributes('-topmost', True)
root.mainloop()