这里写自定义目录标题
Pyside设置界面,外部类调用界面控件的方法
pyside逻辑部分
pyside逻辑部分,往往需要写一些除了MyForm之外的类,当外部类的信息需要显示在窗口控件的时候,就需要用到信号槽。
代码如下(示例):
import codecs
import configparser
import logging
from logging import FileHandler
import multiprocessing
import os
import sys
import time
import threading
import queue
import socket
import numpy as np
from PySide6.QtCore import QRect, Qt, QCoreApplication, QThread, Signal
from PySide6.QtGui import QPixmap, QPainter, QPen
from PySide6.QtWidgets import QMainWindow, QFileDialog, QApplication, QMessageBox, QLabel
from Tools import halconcode, XML, oneclickconversion, FTP, analysisJson
from design import Ui_MainWindow
class WriteQueue(QThread):
'''接收相机采集程序发来的TCP指令(采集完的图像存储的位置),然后存入队列'''
my_queue = queue.Queue()
list_write = []
message_signal = Signal(str)
def __init__(self):
super().__init__()
def doConnect(self, host, port): # 重连服务端
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.bind(('0.0.0.0', 2000))
sock.connect((host, port))
except:
pass
return sock
def run(self):
host, port = "127.0.0.1", 1000
sock_local = self.doConnect(host, port)
while True:
try:
recieve_meg = sock_local.recv(1024).decode('gbk')
self.my_queue.put(recieve_meg) # 存队列
if recieve_meg[-6:] != "FINISH":
self.list_write.append(recieve_meg) # 存进list,用来对比输出
# print(self.list_write)
meg = "收到:" + recieve_meg
sock_local.send(meg.encode("gbk"))
MyForm.logger.info("接收到采图程序TCP:" + recieve_meg + ",存入队列成功")
# MyForm.add_msg("接收到采图程序TCP" + recieve_meg + ",存入队列成功")
self.message_signal.emit("接收到采图程序TCP:" + recieve_meg + ",存入队列成功")
except socket.error:
MyForm.logger.info("连接图像采集程序错误,正在重连")
time.sleep(1)
sock_local = self.doConnect(host, port)
except Exception as e:
MyForm.logger.info(str(e))
time.sleep(1)
class MyForm(QMainWindow, Ui_MainWindow):
def __init__(self):
super(MyForm, self).__init__()
self.setupUi(self)
self.label_showimage = MyLabel(self.tab_platform)
self.label_showimage.setObjectName(u"label_showimage")
self.gridLayout.addWidget(self.label_showimage, 1, 0, 1, 1)
self.label_showimage.setText(QCoreApplication.translate("MainWindow", u" ", None))
'''文本框初始为空'''
self.current_folder_path = None
self.history_folder_path = None
self.current_filename = None
self.history_filename = None
'''目前用不到的两个按钮'''
# self.Button_select_history_folder.setEnabled(False)
# self.Button_select_history_file.setEnabled(False)
# self.textBrowser_recieve.setText("图像识别程序启动成功")
self.start()
def tcp_recieve_msg(self,message):
self.textBrowser_recieve.append(message) # 文本框逐条添加数据
self.textBrowser_recieve.moveCursor(self.textBrowser_recieve.textCursor().End) # 文本框显示到底部
time.sleep(0.1)
# threading.Thread(target=_slot1, args=(self.textBrowser, message)).start()
def tcp_send_msg(self,message):
self.textBrowser_send.append(message) # 文本框逐条添加数据
self.textBrowser_send.moveCursor(self.textBrowser_send.textCursor().End) # 文本框显示到底部
time.sleep(0.1)
def start(self):
self.write = WriteQueue()
self.write.start()
# print("read")
self.write.message_signal.connect(self.tcp_recieve_msg)
总结
通过WriteQueue构造的 message_signal = Signal(str),self.message_signal.emit(“接收到采图程序TCP:” + recieve_meg + “,存入队列成功”)发送数据,在Myform构造函数tcp_recieve_msg来接收数据, self.write.message_signal.connect(self.tcp_recieve_msg)显示到textBrowser控件上。