IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> python学习 之 PyQt5发送模拟触摸到指定IP(TCP协议-客户端) -> 正文阅读

[网络协议]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_())

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2022-03-30 19:06:09  更:2022-03-30 19:07:13 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 5:27:43-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码