python用pyqt5制作登录界面
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" alt="python用pyqt5制作登录界面(python3+pyqt5+itchat微信定时发送消息的方法)" border="0" /> 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.代码复制粘贴完毕后,运行,界面如图(可以将程序中的图标改成你喜欢的图片)
2.点获取好友列表按钮,第一次会弹出微信网页版登录的二维码(第二次运行,itchat会记录你登录记录,只需要点击你手机微信的确认登录的按钮),用手机微信扫描登录即可.时间框里会显示当前的时间,你改成设定时间发送即可(若约定时间已经过了,则消息会立即发送),在好友的下拉列表里选择你要发送的好友(如果好友有备注,则显示备注,否则显示微信昵称),在下面的文本框输入你想要发送的消息,目前只支持文字,不支持图片
最后一步,点击定期发送按钮,静待好友收到消息,并且我设置了文件助手也会收到这条消息,不过你也可以删除.
最后的效果图
以上这篇python3+pyqt5+itchat微信定时发送消息的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持开心学习网。
原文链接:https://blog.csdn.net/weiyang_tang/article/details/82886697