python3+pyqt5+itchat微信定时发送消息的方法

编这个程序是想过节过年,一些重要的纪念日,给亲戚好友发祝福之类的,但要凌晨0点才显得比较有诚意,可我又比较贪睡,常常忘了,所以就有了编个微信定时发送消息小程序.

运行环境:

python 3.x,不支持python2

准备工作

由于我用到了微信的接口,所以引入itchat

界面用了pyqt5

安装命令如下:

pip install PyQt5
pip install itchat

代码部分

# -*- coding: utf-8 -*-
# @Time  : 2018/9/25 11:06
# @Author : Tangweeiyang
# @File  : weixinSender.py

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIcon,QFont
import sys
import datetime
import time
import itchat
import threading
from tkinter import messagebox

class FriendsList(QThread):
  sinOut = pyqtSignal(str)
  # 自定义信号,执行run()函数时,从相关线程发射此信号

  def __init__(self):
    super().__init__()

  def run(self):

    itchat.auto_login(hotReload=True)
    friendList = itchat.get_friends()
    for each in friendList:
      if len(each['RemarkName']) < 2: #如果 备注名为空,则用微信昵称
        self.sinOut.emit(each['NickName'])
      else:
        self.sinOut.emit(each['RemarkName'])

class sendMessage(QThread):

  def __init__(self,message,name,time):
    super().__init__()
    self.message=message
    self.name=name
    self.time=time

  def run(self):
    timer = threading.Timer(self.time,self.send)
    timer.start()

  def send(self):

    itchat.auto_login(hotReload=True)
    sender = itchat.search_friends(self.name)[0]['UserName']
    itchat.send(self.message, toUserName=sender)
    itchat.send(self.message, toUserName='filehelper')
    messagebox.showinfo('消息','发送成功')


class weixinSender(QListWidget):

  def __init__(self):
    super().__init__()
    self.Ui()
    # self.dataProduce()

  def Ui(self):
    self.year_label = QLabel('年')
    self.year=QLineEdit()
    self.month_label=QLabel('月')
    self.month=QLineEdit()
    self.day_label=QLabel('日')
    self.day=QLineEdit()
    self.friendList_lable=QLabel('好友')
    self.friendList=QComboBox()

    self.hour_label = QLabel('时')
    self.hour = QLineEdit()
    self.min_label = QLabel('分')
    self.min = QLineEdit()
    self.second_label = QLabel('秒')
    self.second = QLineEdit()

    self.text=QTextEdit() #文本域
    self.listbt = QPushButton('获取好友列表')
    self.OKbt=QPushButton('定期发送')


    grid = QGridLayout()
    grid.setSpacing(10) # 创建标签之间的空间

    grid.addWidget(self.year_label,1,1)
    grid.addWidget(self.year,1,0)
    grid.addWidget(self.month_label,1,3)
    grid.addWidget(self.month,1,2)
    grid.addWidget(self.day_label,1,5)
    grid.addWidget(self.day,1,4)
    grid.addWidget(self.friendList_lable,1,6,2,1)
    grid.addWidget(self.friendList,1,7,2,3)

    grid.addWidget(self.hour_label,2,1)
    grid.addWidget(self.hour,2,0)
    grid.addWidget(self.min_label,2,3)
    grid.addWidget(self.min,2,2)
    grid.addWidget(self.second_label,2,5)
    grid.addWidget(self.second,2,4)

    grid.addWidget(self.text,3,0,4,10)
    grid.addWidget(self.listbt,7,5,1,2)
    grid.addWidget(self.OKbt,7,7,1,2)

    self.setLayout(grid)

    self.friendList.addItem('好友1')
    self.friendList.addItem('好友2')

    self.text.setFont(QFont("楷体",12,QFont.Normal))

    self.OKbt.clicked.connect(self.send)
    self.listbt.clicked.connect(self.dataProduce)
    self.setGeometry(300, 300, 500, 300)
    self.setWindowTitle('定时发送消息')
    self.setWindowIcon(QIcon('weixinIcon.jpg')) #窗口图标
    self.show()

  def dataProduce(self):
    now=time.localtime()
    self.year.setText(str(now.tm_year))
    self.month.setText(str(now.tm_mon))
    self.day.setText(str(now.tm_mday))
    self.hour.setText(str(now.tm_hour))
    self.min.setText(str(now.tm_min))
    self.second.setText(str(now.tm_sec))

    self.friendList.clear()
    self.thread=FriendsList()
    self.thread.sinOut.connect(self.slotAdd)
    self.thread.start()

  def slotAdd(self,name):
    self.friendList.addItem(name)

  def send(self):
    self.target = datetime.datetime(int(self.year.text()),int(self.month.text()),int(self.day.text()),int(self.hour.text()),int(self.min.text()),int(self.second.text()))
    self.now = time.time()
    self.delay_time = self.target.timestamp() - self.now
    self.sendThread=sendMessage(self.text.toPlainText(),self.friendList.currentText(),self.delay_time)
    self.sendThread.start()

if __name__ == '__main__':
  app = QApplication(sys.argv)
  ex = weixinSender()
  sys.exit(app.exec_())

运行步骤

1.代码复制粘贴完毕后,运行,界面如图(可以将程序中的图标改成你喜欢的图片)

python3+pyqt5+itchat微信定时发送消息的方法

2.点获取好友列表按钮,第一次会弹出微信网页版登录的二维码(第二次运行,itchat会记录你登录记录,只需要点击你手机微信的确认登录的按钮),用手机微信扫描登录即可.时间框里会显示当前的时间,你改成设定时间发送即可(若约定时间已经过了,则消息会立即发送),在好友的下拉列表里选择你要发送的好友(如果好友有备注,则显示备注,否则显示微信昵称),在下面的文本框输入你想要发送的消息,目前只支持文字,不支持图片

最后一步,点击定期发送按钮,静待好友收到消息,并且我设置了文件助手也会收到这条消息,不过你也可以删除.

最后的效果图

python3+pyqt5+itchat微信定时发送消息的方法

相关推荐