from time import sleep
import random
import itchat
import sys
import os
import requests
from PyQt5.QtWidgets import (QWidget, QPushButton, QLineEdit,
QInputDialog, QApplication)
from PyQt5.QtWidgets import QWidget, QMessageBox, QApplication
from PyQt5 import QtCore, QtGui, QtWidgets
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
qbtn = QPushButton(‘退出‘, self)
qbtn.clicked.connect(QApplication.instance().quit)
qbtn.resize(qbtn.sizeHint())
qbtn.move(20, 200)
##退出按钮
self.btn = QPushButton(‘编辑信息‘, self)
self.btn.move(20, 20)
self.btn.clicked.connect(self.message)
##按钮
self.btn = QPushButton(‘符合条件‘, self)
self.btn.move(20, 60)
self.btn.clicked.connect(self.accordint)
self.le = QLineEdit(self)
#QLineEdit
self.le.move(120, 100)#(X,Y)原点为左上角
self.le.resize(220, 80)#(宽,长)
self.le.setMaxLength(20)
#self.le.adjustSize()
#self.le.setWordWrap(True)
##展示窗口
todaytn = QPushButton(‘登录‘, self)
todaytn.clicked.connect(self.get_frinends)
todaytn.move(20,100)
todaytn = QPushButton(‘发送‘, self)
todaytn.clicked.connect(self.send)
todaytn.move(20,150)
##天气查询
self.setGeometry(150, 100, 460, 350)
self.setWindowTitle(‘微信群发‘)
self.show()
##主界面
def closeEvent(self, event):
reply = QMessageBox.question(self, ‘信息‘,
"确定退出?", QMessageBox.Yes |
QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
def message(self):
global message
message, ok = QInputDialog.getText(self, ‘输入发送信息‘,
‘字符‘)
if ok:
self.le.setText(‘发送的信息为‘+str(message))
def accordint(self):
global accord
accord, ok = QInputDialog.getText(self, ‘输入约束条件‘,
‘关键字或all‘)
if ok:
accord = str(accord)
self.le.setText(‘发送给备注信息包含‘+str(accord)+‘好友‘)
def get_var(self,var):
itchat.login()
friends=itchat.get_friends(update=True)[0:]
variable=[]
for i in friends:
value=i[var]
variable.append(value)
return variable
def get_frinends(self):
global NickNamelist
NickNamelist=self.get_var(‘RemarkName‘)
print(‘好友数量‘+str(len(NickNamelist)))
def send(self):
for i in range(1,len(NickNamelist)):
loadtime = random.uniform(0.5, 1.5)
print(NickNamelist[i])
itcaht_u = itchat.search_friends(name=NickNamelist[i])
#print(itcaht_u)
try:
userName= itcaht_u[0][‘UserName‘]
except Exception as e:
print(‘对象不是一个好友‘)
else:
print(userName)
if accord in NickNamelist[i]:
itchat.send_msg(msg=message,toUserName=userName)
self.le.setText(str(NickNamelist[i]+‘发送成功‘))
sleep(loadtime)
elif accord == ‘all‘:
itchat.send_msg(message,toUserName=userName)
self.le.setText(str(NickNamelist[i]+‘发送成功‘))
print(‘发送成功‘)
sleep(loadtime)
else:
pass
print(‘信息已发送完成‘)
#Sex=get_var(‘Sex‘)
#Province=get_var(‘Province‘)
#City=get_var(‘City‘)
#Signature=get_var(‘Signature‘)
#RemarkName,NickName
if __name__ == ‘__main__‘:
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
微信群发工具图形界面(基于itchat+pyqt5)