随机选2个人扔垃圾并发送到QQ程序

import win32gui
import win32con
import win32clipboard as w
import random
import time

def getText():
    """获取剪贴板文本"""
    w.OpenClipboard()
    d = w.GetClipboardData(win32con.CF_UNICODETEXT)
    w.CloseClipboard()
    return d

def setText(aString):
    """设置剪贴板文本"""
    w.OpenClipboard()
    w.EmptyClipboard()
    w.SetClipboardData(win32con.CF_UNICODETEXT, aString)
    w.CloseClipboard()

def send_qq(to_who, msg):
    """发送qq消息
    to_who:qq消息接收人
    msg:需要发送的消息
    """
    # 将消息写到剪贴板
    setText(msg)
    print(getText())
    # 获取qq窗口句柄
    qq = win32gui.FindWindow(None, to_who)
    # 投递剪贴板消息到QQ窗体
    win32gui.SendMessage(qq, 258, 22, 2080193)
    win32gui.SendMessage(qq, 770, 0, 0)
    # 模拟按下回车键
    win32gui.SendMessage(qq, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
    win32gui.SendMessage(qq, win32con.WM_KEYUP, win32con.VK_RETURN, 0)

if __name__ == '__main__':
    # 发送消息
    list = ["A","B","C","D","E","F","G","H"]
    rengfan = random.sample(list, 2)
    str = ','.join(rengfan)
    time = time.strftime('%Y.%m.%d %H:%M:%S ',time.localtime(time.time()))
    message = ("今天扔垃圾的成员是: "+ str+"。\n抽取时间:" + time)
    to_who='要发送的人的昵称'
    msg=message
    send_qq(to_who, msg)

你可能感兴趣的:(随笔)