class WidChatMain(QWidget): # 窗体 def __init__(self, chat_): super().__init__() # self.setGeometry(100, 100, 800, 600) # self.setWindowTitle('聊天') self.chat = chat_ self.ui_main = Ui_Form() self.ui_main.setupUi(self) # 处理chat收到信息的信号 self.chat.sign_coming_msg.connect(self.show_msg) # 添加模式,和列表框绑在一起 self.model = QStandardItemModel() self.ui_main.listView.setModel(self.model) # 列表框绑定信号处理,(点击的对象)传递给函数index self.ui_main.listView.clicked.connect(self.select_user) # 发送按钮绑定信号处理,发送信息 self.ui_main.pushButton.clicked.connect(self.send_msg) def show_user_list(self): # 从helper中获取用户列表 lst_users = self.chat.get_friends() # 显示列表到列表框 for user_ in lst_users: user_name = user_['UserName'] # 用户id nick_name = user_['NickName'] # 用户昵称 icon_head = QIcon('imgs/user.jpg') # 自己头像,图片路径 item_ = QStandardItem(icon_head, nick_name) # 形成模式中的数据项 item_.setData(user_name) # 用户id也放在模式中,但是隐式存放,item_中显示只有图标和昵称 self.model.appendRow(item_) # 添加到模式中 def select_user(self, index): # 返回选择的行号 row = index.row() # index.data() # 用户昵称 # self.model.item(row).data() # 模型中隐式的数据(用户id),通过行号来找到对应用户的id self.current_user = self.model.item(row).data() def send_msg(self): # 获取文本信息 msg = self.ui_main.lineEdit.text() # 发送,使用helper self.chat.send_msg(self.current_user, msg) def show_msg(self, msg): self.setWindowTitle(msg) # 收到信息显示在标题上
def __init__(self, chat_): super().__init__() self.chat = chat_ self.ui = Ui_Form() self.ui.setupUi(self) # 处理chat发送过来的信号 self.chat.sign_coming_msg.connect(self.show_msg) # 添加模式 self.model = QStandardItemModel() # 构建一个模式 self.ui.listView.setModel(self.model) #绑定信号处理 self.ui.listView.clicked.connect(self.select_user) # 点击信息 self.ui.pushButton.clicked.connect(self.send_msg)
class WidChatMain(QWidget): def __init__(self, chat_): super().__init__() self.chat = chat_ self.ui = Ui_Form() self.ui.setupUi(self) # 处理chat发送过来的信号 self.chat.sign_coming_msg.connect(self.show_msg) # 添加模式 self.model = QStandardItemModel() # 构建一个模式 self.ui.listView.setModel(self.model) #绑定信号处理 self.ui.listView.clicked.connect(self.select_user) # 点击信息 self.ui.pushButton.clicked.connect(self.send_msg) def send_msg(self): # 获取文本信息 msg = self.ui.lineEdit.text() # 得到文本 # 发送 需要使用辅助类 self.chat.send_msg(self.current_user, msg) # 其实要判断下用户是否选择 def select_user(self, index): # 获取当前用户 row = index.row() self.current_user = self.model.item(row).data() # 绑定在模型中不显示的数据 def show_msg(self, msg): self.window() def show_user_list(self): #调用辅助类 获取用户列表 lst_users = self.chat.get_friends() # 显示用户列表到别表框 for user_ in lst_users: user_name = user_['UserName'] # 发送时使用的用户ID nick_name = user_['NickName'] # 显示这个类 icon_head = QIcon('imgs/user.jpg') item_ = QStandardItem(icon_head, nick_name) # 得到数据项 item_.setData(user_name) self.model.appendRow(item_) # 添加到模型里面
def __init__(self, chat_): super().__init__() # self.setGeometry(100, 100, 800, 600) # self.setWindowTitle('聊天') self.chat = chat_ self.ui_main = Ui_Form() self.ui_main.setupUi(self) # 处理chat收到信息的信号 self.chat.sign_coming_msg.connect(self.show_msg) # 添加模式,和列表框绑在一起 self.model = QStandardItemModel() self.ui_main.listView.setModel(self.model) # 列表框绑定信号处理,(点击的对象)传递给函数index self.ui_main.listView.clicked.connect(self.select_user) # 发送按钮绑定信号处理,发送信息 self.ui_main.pushButton.clicked.connect(self.send_msg)
class WidChatMain(QWidget): def __init__(self, chat_): super().__init__() self.chat = chat_ self.ui = Ui_Form() self.ui.setupUi(self) self.__y = 5 # 对话消息的位置 # 处理chat发送过来的信号 self.chat.sign_coming_msg.connect(self.show_msg) # 添加模式 self.model = QStandardItemModel() self.ui.listView.setModel(self.model) # 绑定信号处理 self.ui.listView.clicked.connect(self.select_user) # 点击信息 self.ui.pushButton.clicked.connect(self.send_msg) def select_user(self, index): # 获取当前用户 row = index.row() self.current_user = self.model.item(row).data() # 绑定模型中不显示的数据 def send_msg(self): # 获取文本信息 msg = self.ui.lineEdit.text() # 发送(辅助类) self.chat.send_msg(self.current_user, msg) # 判定下用户是否选择 self.and_msg(msg=msg) def show_msg(self, msg, user): if user == self.current_user: self.and_msg(msg=msg, align_left=False) # self.setWindowTitle(msg) def show_user_list(self): # 调用辅助类获取列表 lst_users = self.chat.get_friends() # 显示列表到列表框 for user_ in lst_users: user_name = user_['UserName'] # 发送的使用用户ID nick_name = user_['NickName'] # 显示 icon_head = QIcon('imgs/user.jpg') item_ = QStandardItem(icon_head, nick_name) item_.setData(user_name) self.model.appendRow(item_) # 添加消息到滚动框 def and_msg(self, msg, align_left=True): lbl_msg = QLabel(self.ui.scrollAreaWidgetContents) lbl_msg.setText(msg) lbl_msg.resize(350, 350) lbl_msg.show() # 下面代码居右,需要计算字符串长度,调整x位置(这里省略)。 if align_left: print('发送消息', msg) lbl_msg.setAlignment(Qt.AlignLeft) lbl_msg.move(0, self.__y) else: print('接收消息', msg) lbl_msg.setAlignment(Qt.AlignRight) lbl_msg.move(0, self.__y) self.__y += 28
def __init__(self, chat_): super().__init__() self.allchat = {} self.chatlists = {} self.chat = chat_ self.form = Ui_Form() self.form.setupUi(self) self.form.tabWidget.setWindowFlags(Qt.FramelessWindowHint) self.chat.sign_coming_msg.connect(self.get_msg) self.friendmodel = QStandardItemModel() self.currentmodel = QStandardItemModel() self.form.friends.setModel(self.friendmodel) self.form.friends.clicked.connect(self.select_user) self.mpmodel = QStandardItemModel() self.form.mplist.setModel(self.mpmodel) self.form.mplist.clicked.connect(self.select_mp) self.form.current.setModel(self.currentmodel) self.form.current.clicked.connect(self.select_current) self.form.send.clicked.connect(self.send_msg) self.form.closeButton.clicked.connect(self.exit) self.form.searchButton.clicked.connect(self.search) self.form.searchmpButton.clicked.connect(self.searchmp) self.message_label = QLabel() self.form.scrollArea.setWidget(self.message_label) self.current_user = None self.current = set() self.form.clean.clicked.connect(lambda: self.form.textEdit.setText("")) self.form.send.setShortcut("Ctrl+Return") self.form.min.clicked.connect(lambda: self.showMinimized()) self.m_flag = False self.setWindowTitle("微信Qt") self.message_label.setGeometry( QRect(self.message_label.x() + 10, self.message_label.y(), 310, self.message_label.height())) self.message_label.setScaledContents(True) font = QFont() font.setPointSize(10) self.message_label.setFont(font) self.message_label.setWordWrap(True) self.mpview1 = QWebEngineView(self.form.mpframe) self.mpview = QWebEnginePage(self.mpview1) self.mpview1.setGeometry(QRect(0, 0, 441, 391)) self.mpview.setObjectName("mpview") self.mpview.urlChanged.connect(self.reload) self.mpb = QPushButton(self.form.mpframe) self.mpb.setGeometry(QRect(420, 0, 21, 21)) self.mpb.setObjectName("mpb") self.mpb.setText("刷") # self.mpview.link_clicked().connect(lambda x:print(x)) self.form.tabWidget.setCurrentIndex(1) self.form.mpframe.hide() self.mpb.clicked.connect(lambda: self.mpview.runJavaScript(""" var list = document.getElementsByTagName("a") for (var i= list.length; i-->0;) list[i].removeAttribute("target"); """)) self.form.friendsound.setTristate(False) self.form.groupsound.setTristate(False) # self.recapture = uis.recapture.recap() # self.recapture.show() self.form.translateButton.clicked.connect(self.translate)
class WidChatMain(QWidget): def __init__(self, chat_): super().__init__() self.allchat = {} self.chatlists = {} self.chat = chat_ self.form = Ui_Form() self.form.setupUi(self) self.form.tabWidget.setWindowFlags(Qt.FramelessWindowHint) self.chat.sign_coming_msg.connect(self.get_msg) self.friendmodel = QStandardItemModel() self.currentmodel = QStandardItemModel() self.form.friends.setModel(self.friendmodel) self.form.friends.clicked.connect(self.select_user) self.mpmodel = QStandardItemModel() self.form.mplist.setModel(self.mpmodel) self.form.mplist.clicked.connect(self.select_mp) self.form.current.setModel(self.currentmodel) self.form.current.clicked.connect(self.select_current) self.form.send.clicked.connect(self.send_msg) self.form.closeButton.clicked.connect(self.exit) self.form.searchButton.clicked.connect(self.search) self.form.searchmpButton.clicked.connect(self.searchmp) self.message_label = QLabel() self.form.scrollArea.setWidget(self.message_label) self.current_user = None self.current = set() self.form.clean.clicked.connect(lambda: self.form.textEdit.setText("")) self.form.send.setShortcut("Ctrl+Return") self.form.min.clicked.connect(lambda: self.showMinimized()) self.m_flag = False self.setWindowTitle("微信Qt") self.message_label.setGeometry( QRect(self.message_label.x() + 10, self.message_label.y(), 310, self.message_label.height())) self.message_label.setScaledContents(True) font = QFont() font.setPointSize(10) self.message_label.setFont(font) self.message_label.setWordWrap(True) self.mpview1 = QWebEngineView(self.form.mpframe) self.mpview = QWebEnginePage(self.mpview1) self.mpview1.setGeometry(QRect(0, 0, 441, 391)) self.mpview.setObjectName("mpview") self.mpview.urlChanged.connect(self.reload) self.mpb = QPushButton(self.form.mpframe) self.mpb.setGeometry(QRect(420, 0, 21, 21)) self.mpb.setObjectName("mpb") self.mpb.setText("刷") # self.mpview.link_clicked().connect(lambda x:print(x)) self.form.tabWidget.setCurrentIndex(1) self.form.mpframe.hide() self.mpb.clicked.connect(lambda: self.mpview.runJavaScript(""" var list = document.getElementsByTagName("a") for (var i= list.length; i-->0;) list[i].removeAttribute("target"); """)) self.form.friendsound.setTristate(False) self.form.groupsound.setTristate(False) # self.recapture = uis.recapture.recap() # self.recapture.show() self.form.translateButton.clicked.connect(self.translate) # self.form.translatesoundButton.clicked.connect(self.translatesound) def exit(self): shutil.rmtree("tmp", ignore_errors=True) QApplication.quit() def reload(self): self.mpview.load(self.mpview.url()) # self.mpview. # time.sleep(2) self.mpview.loadFinished.connect(lambda: self.mpview.runJavaScript(""" var list = document.getElementsByTagName("a") for (var i= list.length; i-->0;) list[i].removeAttribute("target"); """)) # self.mpview1.setPage(self.mpview) def mousePressEvent(self, QMouseEvent): if QMouseEvent.button() == Qt.LeftButton and \ (QMouseEvent.globalPos() - self.pos()).x() < 700 and \ (QMouseEvent.globalPos() - self.pos()).y() < 60: self.m_flag = True self.m_Position = QMouseEvent.globalPos() - self.pos( ) # 获取鼠标相对窗口的位置 QMouseEvent.accept() self.setCursor(QCursor(Qt.OpenHandCursor)) # 更改鼠标图标 else: super().mousePressEvent(QMouseEvent) def mouseMoveEvent(self, QMouseEvent): if Qt.LeftButton and self.m_flag: self.move(QMouseEvent.globalPos() - self.m_Position) # 更改窗口位置 QMouseEvent.accept() else: super().mouseMoveEvent(QMouseEvent) def mouseReleaseEvent(self, QMouseEvent): self.m_flag = False self.setCursor(QCursor(Qt.ArrowCursor)) super().mouseReleaseEvent(QMouseEvent) def search(self): search_string = self.form.searchbar.text() items = self.friendmodel.findItems(search_string, Qt.MatchStartsWith) for item in items: if search_string: self.friendmodel.takeRow(item.row()) self.friendmodel.insertRow(0, item) def searchmp(self): search_string = self.form.searchmp.text() items = self.mpmodel.findItems(search_string, Qt.MatchStartsWith) for item in items: if search_string: self.mpmodel.takeRow(item.row()) self.mpmodel.insertRow(0, item) def show_message(self): try: a = "" for _ in self.chatlists.get(self.current_user): a += _ self.message_label.setText(a) self.message_label.setTextInteractionFlags( Qt.TextSelectableByMouse) self.form.scrollArea.verticalScrollBar().setValue(9999) except Exception as e: self.message_label.setText("无消息") def select_user(self, index): self.form.mpframe.hide() row = index.row() self.form.chatname.setText( self.allchat[self.friendmodel.item(row).data()]) self.current_user = self.friendmodel.item(row).data() self.show_message() def select_current(self, index): self.form.mpframe.hide() row = index.row() self.form.chatname.setText( self.allchat[self.currentmodel.item(row).data()]) self.current_user = self.currentmodel.item(row).data() self.current.remove(self.current_user) self.currentmodel.removeRow(row) self.show_message() def select_mp(self, index): row = index.row() self.form.mpframe.show() self.form.chatname.setText(self.allchat[self.mpmodel.item(row).data()]) self.current_user = self.mpmodel.item(row).data() name = self.allchat[self.mpmodel.item(row).data()] a = "http://www.sogou.com/web?ie=utf8&query={} 公众号".format(name) self.mpview.load(QUrl(a)) self.mpview1.setPage(self.mpview) def send_msg(self): msg = self.form.textEdit.toPlainText().strip() if msg: if self.current_user: self.chat.send_msg(self.current_user, msg) self.form.textEdit.setText("") if self.chatlists.get(self.current_user): self.chatlists[self.current_user].append( "<p style='color:red;'>{}---------------</p>".format( datetime.datetime.now().strftime( "%m-%d %H:%M:%S")) + msg) else: self.chatlists[self.current_user] = [ "<p style='color:red;'>{}---------------</p>".format( datetime.datetime.now().strftime("%m-%d %H:%M:%S")) + msg ] self.show_message() else: QMessageBox.about(self, "错误", "请输入内容") def get_msg(self, msg): if self.chatlists.get(msg[0]): self.chatlists[msg[0]].append( "<p style='color:green;'>{}--{}-------------</p>".format( datetime.datetime.now().strftime("%m-%d %H:%M:%S"), msg[2]) + msg[1], ) else: self.chatlists[msg[0]] = [ "<p style='color:green;'>{}--{}-------------</p>".format( datetime.datetime.now().strftime("%m-%d %H:%M:%S"), msg[2]) + msg[1] ] if msg[0].startswith("@@"): msg[2] = itchat.search_chatrooms(userName=msg[0])["NickName"] icon_head = QIcon("imgs/group.png") if self.form.groupsound.checkState(): QSound.play("sounds/notice.wav") else: msg[2] = itchat.search_friends(userName=msg[0])["NickName"] icon_head = QIcon("imgs/friends.png") if self.form.friendsound.checkState(): QSound.play("sounds/notice.wav") if msg[0] not in self.current and msg[0] != self.current_user: user_name = msg[0] nick_name = msg[2] item_ = QStandardItem(icon_head, nick_name) item_.setData(user_name) self.currentmodel.appendRow(item_) self.current.add(msg[0]) self.show_message() def show_user_list(self): lst_group = self.chat.get_groups() for user_ in lst_group: user_name = user_["UserName"] nick_name = user_["NickName"] icon_head = QIcon("imgs/group.png") item_ = QStandardItem(icon_head, nick_name) item_.setData(user_name) self.friendmodel.appendRow(item_) self.allchat[user_name] = nick_name lst_user = self.chat.get_friends() for user_ in lst_user: user_name = user_["UserName"] nick_name = user_["NickName"] icon_head = QIcon("imgs/friends.png") item_ = QStandardItem(icon_head, nick_name) item_.setData(user_name) self.friendmodel.appendRow(item_) self.allchat[user_name] = nick_name myimg = itchat.get_head_img( userName=itchat.search_friends()["UserName"]) self_img = QPixmap.fromImage(QImage.fromData(myimg)) self.form.selfpic.setPixmap(self_img) self.form.selfpic.setScaledContents(True) def show_mp_list(self): lst_mp = self.chat.get_mp() for user_ in lst_mp: user_name = user_["UserName"] nick_name = user_["NickName"] icon_head = QIcon("imgs/mps.png") item_ = QStandardItem(icon_head, nick_name) item_.setData(user_name) self.mpmodel.appendRow(item_) self.allchat[user_name] = nick_name def translate(self): trans = TranslateHelper(self.form.translateText.text()) self.form.translateResult.setText(trans.webxml()) self.form.translatesoundButton.clicked.connect(self.translateplay) def translateplay(self): try: os.startfile("tmp/{}.mp3".format(self.form.translateText.text())) except AttributeError: subprocess.call([ 'xdg-open', "tmp/{}.mp3".format(self.form.translateText.text()) ])