python学习 之 PyQt5发送模拟触摸到指定IP(TCP协议-客户端)

1 使用QT API获取鼠标位置

# -*- coding: utf-8 -*-

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

import socket
import time
import sys

from threading import Thread,Lock

LCD_W = 240
LCD_H = 240

LCD_R = 0

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(("192.168.3.80",1347))

x = 0
y = 0
pressed = False

# 自定义信号源对象类型,一定要继承自 QObject
class MySignals(QObject):

    # 定义一种信号,两个参数 类型分别是: QTextBrowser 和 字符串
    # 调用 emit方法 发信号时,传入参数 必须是这里指定的 参数类型
    mouse_update = pyqtSignal([int,int],[int,int,int])

# 实例化
mouse_single = MySignals()  

def task():
    bankLock = Lock()

    def update_mouse(posx, posy):
        # 操作共享数据前,申请获取锁
        bankLock.acquire()
        global x, y
        x = posx
        y = posy
        # 操作完共享数据后,申请释放锁
        bankLock.release()

    def update_mouse_pressed(posx, posy, state):
        # 操作共享数据前,申请获取锁
        bankLock.acquire()
        global x, y, pressed
        x = posx
        y = posy
        pressed = state
        # 操作完共享数据后,申请释放锁
        bankLock.release()

    mouse_single.mouse_update[int,int].connect(update_mouse)
    mouse_single.mouse_update[int,int,int].connect(update_mouse_pressed)

    def threadFunc():
        global x, y
        client_isConnected = True
        while True:
            # 设置client断开连接时,重新连接
            try:
                if ( x >= 0 and x <= LCD_W ) and ( y >= 0 and y <= LCD_H ):
                    send_data = ("{\"point\":[%d,%d],\"state\":\"%d\"}\n"%(x,y,pressed)).encode("utf-8")
                    print(send_data)
                    client.send(send_data)
                    # client.send(("mouse:"+str(x)+","+str(y)+","+str(pressed)+"\n").encode("utf-8"))
            except:
                client_isConnected = False

            if client_isConnected == False:
                try:
                    client.close()
                    client.connect(("192.168.3.80",1347))
                    client_isConnected = True
                except:
                    client_isConnected = False

            time.sleep(0.02)

    thread = Thread(target = threadFunc, daemon=True)
    thread.start()


class Window(QMainWindow):

    def __init__(self):
        QMainWindow.__init__(self)
        self.resize(LCD_W, LCD_H)
        # 设置最大大小
        self.setFixedSize(QSize(LCD_W, LCD_H))
        self.setWindowOpacity(0.6)
        self.setWindowTitle('Touch')
        self.setWindowIcon(QIcon('./images/boy.png'))

        self.pix = QPixmap()
        self.lastPoint = QPoint()
        self.endPoint = QPoint()
        self.pix = QPixmap(LCD_W, LCD_H)
        self.pix.fill(Qt.white)

        self.mouse_x = 0
        self.mouse_y = 0
        self.mouse_pressed = 0

        self.label_mouse_x = QLabel(self)
        self.label_mouse_x.setGeometry(190, 5, 80, 30)
        self.label_mouse_x.setText('x:0')
        self.label_mouse_x.setMouseTracking(True)
 
        self.label_mouse_y = QLabel(self)
        self.label_mouse_y.setText('y:0')
        self.label_mouse_y.setGeometry(190, 40, 80, 30)
        self.label_mouse_y.setMouseTracking(True)
    
    def paintEvent(self, event):
        paint = QPainter(self.pix)
        paint.setPen(QPen(QColor(0, 160, 230), 1))
        # 根据鼠标指针前后两个位置绘制直线
        paint.drawLine(LCD_W//4*0, 0, LCD_W//4*0, LCD_H)
        paint.drawLine(LCD_W//4*1, 0, LCD_W//4*1, LCD_H)
        paint.drawLine(LCD_W//4*2, 0, LCD_W//4*2, LCD_H)
        paint.drawLine(LCD_W//4*3, 0, LCD_W//4*3, LCD_H)
        paint.drawLine(LCD_W//4*4 - 1, 0, LCD_W//4*4 - 1, LCD_H)

        paint.drawLine(0, LCD_H//4*0, LCD_W, LCD_H//4*0)
        paint.drawLine(0, LCD_H//4*1, LCD_W, LCD_H//4*1)
        paint.drawLine(0, LCD_H//4*2, LCD_W, LCD_H//4*2)
        paint.drawLine(0, LCD_H//4*3, LCD_W, LCD_H//4*3)
        paint.drawLine(0, LCD_H//4*4 - 1, LCD_W, LCD_H//4*4 - 1)

        if(self.mouse_pressed == True):
            if(self.lastPoint == self.endPoint):
                paint.setPen(QPen(QColor(255, 0, 0), 8))
                paint.drawPoint(self.endPoint)
            else:
                paint.setPen(QPen(QColor(255, 0, 0), 1))
                paint.drawLine(self.lastPoint, self.endPoint)
        else:
            pass
        # 这样就能实现画出连续的线
        self.lastPoint = self.endPoint
        painter = QPainter(self)
        painter.drawPixmap(0, 0, self.pix)

    def mousePressEvent(self, event):
        s = event.windowPos()
        self.setMouseTracking(True)
        self.mouse_x = int(s.x())
        self.mouse_y = int(s.y())

        if ( self.mouse_x >= 0 and self.mouse_x <= LCD_W ) and ( self.mouse_y >= 0 and self.mouse_y <= LCD_H ):
            self.label_mouse_x.setText('X:' + str(int(self.mouse_x)))
            self.label_mouse_y.setText('Y:' + str(int(self.mouse_y)))

        self._ActiveButton = event.button()

        if self._ActiveButton == Qt.LeftButton:
            self.pix.fill(Qt.white)
            mouse_single.mouse_update[int,int,int].emit(self.mouse_x, self.mouse_y, 1)
            self.endPoint = event.pos()
            self.lastPoint = self.endPoint
            self.mouse_pressed = True
            self.update()
        elif self._ActiveButton == Qt.MiddleButton:
            self.pix.fill(Qt.white)
            self.endPoint = event.pos()
            self.lastPoint = self.endPoint
            self.mouse_pressed = False
            self.update()

    def mouseMoveEvent(self, event):
        s = event.windowPos()
        self.setMouseTracking(True)
        self.label_mouse_x.setText('X:' + str(int(s.x())))
        self.label_mouse_y.setText('Y:' + str(int(s.y())))
        self.mouse_x = int(s.x())
        self.mouse_y = int(s.y())

        mouse_single.mouse_update[int,int].emit(self.mouse_x, self.mouse_y)

        if event.buttons() and Qt.LeftButton:
            self.endPoint = event.pos()
            mouse_single.mouse_update[int,int,int].emit(self.mouse_x, self.mouse_y, 1)
            self.update()
        else:
            mouse_single.mouse_update[int,int,int].emit(self.mouse_x, self.mouse_y, 0)
            self.endPoint = self.lastPoint
            self.update()

    def mouseReleaseEvent(self, event):
        s = event.windowPos()
        self.setMouseTracking(True)
        self.label_mouse_x.setText('X:' + str(int(s.x())))
        self.label_mouse_y.setText('Y:' + str(int(s.y())))
        self.mouse_x = int(s.x())
        self.mouse_y = int(s.y())
        # 鼠标左键释放
        self._ActiveButton = event.button()

        if self._ActiveButton == Qt.LeftButton:
            self.mouse_pressed = False
            mouse_single.mouse_update[int,int,int].emit(self.mouse_x, self.mouse_y, 0)
            self.pix.fill(Qt.white)
            self.endPoint = self.lastPoint
            self.update()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = Window()
    win.show()
    task()
    sys.exit(app.exec_())

2 使用Win API获取鼠标位置

from PyQt5.QtCore import QThread, pyqtSignal, QObject
from PyQt5.QtWidgets import QApplication, QLabel, QWidget
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QMainWindow,QApplication
from PyQt5.QtGui import QPainter, QPixmap, QColor, QPen
from PyQt5.QtCore import Qt, QPoint, QSize

import socket
import time
import sys
import qdarkstyle
import win32gui
import win32api

LCD_W = 240
LCD_H = 240

LCD_R = 0

def findTitle(window_title):
    '''
    查找指定标题窗口句柄
    @param window_title: 标题名
    @return: 窗口句柄
    '''
    hWndList = []
    # 函数功能:该函数枚举所有屏幕上的顶层窗口,办法是先将句柄传给每一个窗口,然后再传送给应用程序定义的回调函数。
    win32gui.EnumWindows(lambda hWnd, param: param.append(hWnd), hWndList)
    for hwnd in hWndList:
        # 函数功能:该函数获得指定窗口所属的类的类名。
        # clsname = win32gui.GetClassName(hwnd)
        # 函数功能:该函数将指定窗口的标题条文本(如果存在)拷贝到一个缓存区内
        title = win32gui.GetWindowText(hwnd)
        if (title == window_title):
            print("标题:", title, "句柄:", hwnd)
            break
    return hwnd

class BackendThread(QObject):
    # 通过类成员对象定义信号
    update_date = pyqtSignal(str)
    # 处理业务逻辑
    def run(self):

        client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        client.connect(("192.168.3.80",1347))

        window_title =  u'ESP32-Touch'
        hwnd = findTitle(window_title)
        print(hwnd)

        x,y,w,h = win32gui.GetWindowRect(hwnd)
        print(x,y,w,h)

        while True:
            #   GetCursorPos 获取鼠标指针的当前位置
            p = win32api.GetCursorPos()
            #  GetWindowRect 获得整个窗口的范围矩形,窗口的边框、标题栏、滚动条及菜单等都在这个矩形内 
            x,y,w,h = win32gui.GetWindowRect(hwnd)
            # 鼠标坐标减去指定窗口坐标为鼠标在窗口中的坐标值
            pos_x = p[0] - x - 5
            pos_y = p[1] - y - 30

            if ( pos_x >= 0 and pos_x <= LCD_W ) and ( pos_y >= 0 and pos_y <= LCD_H ):
                print(pos_x,pos_y)
                client.send((str(pos_x)+","+str(pos_y)+"\n").encode("utf-8"))
            
            time.sleep(0.02)

class Window(QMainWindow):

    def __init__(self):
        QMainWindow.__init__(self)
        self.setWindowTitle('ESP32-Touch')
        self.setWindowIcon(QIcon('./images/tiantiansifangmao.png'))
        self.resize(LCD_W, LCD_H)
        self.setFixedSize(QSize(LCD_W, LCD_H))
        self.setWindowOpacity(0.3)
        self.pix = QPixmap()
        self.lastPoint = QPoint()
        self.endPoint = QPoint()
        self.pix = QPixmap(LCD_W, LCD_H)
        self.pix.fill(Qt.white)
        self.initUI()

    def initUI(self):
        # 创建线程
        self.backend = BackendThread()
        # 连接信号
        self.backend.update_date.connect(self.handleDisplay)
        self.thread = QThread()
        self.backend.moveToThread(self.thread)
        # 开始线程
        self.thread.started.connect(self.backend.run)
        self.thread.start()
    
    def paintEvent(self, event):
        paint = QPainter(self.pix)
        paint.setPen(QPen(QColor(0, 160, 230), 1))
        # 根据鼠标指针前后两个位置绘制直线
        paint.drawLine(LCD_W//4*1, 0, LCD_W//4*1, LCD_H)
        paint.drawLine(LCD_W//4*2, 0, LCD_W//4*2, LCD_H)
        paint.drawLine(LCD_W//4*3, 0, LCD_W//4*3, LCD_H)

        paint.drawLine(0, LCD_H//4*1, LCD_W, LCD_H//4*1)
        paint.drawLine(0, LCD_H//4*2, LCD_W, LCD_H//4*2)
        paint.drawLine(0, LCD_H//4*3, LCD_W, LCD_H//4*3)

        paint.setPen(QPen(QColor(255, 0, 0), 2))
        paint.drawLine(self.lastPoint, self.endPoint)
        # 这样就能实现画出连续的线
        self.lastPoint = self.endPoint
        painter = QPainter(self)
        painter.drawPixmap(0, 0, self.pix)

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.lastPoint = event.pos()

    def mouseMoveEvent(self, event):
        if event.buttons() and Qt.LeftButton:
            self.endPoint = event.pos()
            self.update()

    def mouseReleaseEvent(self, event):
        # 鼠标左键释放
        if event.button() == Qt.LeftButton:
            self.endPoint = event.pos()
            self.pix.fill(Qt.white)
            self.update()

    # 将当前时间输出到文本框
    def handleDisplay(self, data):
        self.input.setText(data)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = Window()
    # win.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
    win.show()
    sys.exit(app.exec_())

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值