python获取ps4手柄信息并使用QT界面显示读取内容

这篇具有很好参考价值的文章主要介绍了python获取ps4手柄信息并使用QT界面显示读取内容。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

本文使用ps4手柄旨在控制机器人行走,故只获取了手柄的部分信息。
硬件:PS4手柄
环境:win7/10,python3.7,opencv4.0,Pyqt5,pygame,time,math,sys

一、使用pygame库获取手柄信息

主要代码如下:

import pygame
import time
import numpy as np
import math
import sys
class JoyStick(object):
    def __init__(self, id):
        pygame.init()
        pygame.joystick.init()
        if pygame.joystick.get_count() == 0:
            raise Exception("Joystick not found!")

        self.joystick = pygame.joystick.Joystick(id)
        self.joystick.init()

        self.axis = np.zeros(self.joystick.get_numaxes())
        self.__alpha = 0.0

        self.button = [self.joystick.get_button(i) for i in range(self.joystick.get_numbuttons())]
        self.hat = [self.joystick.get_hat(i) for i in range(self.joystick.get_numhats())]

        self.lst_button = [self.joystick.get_button(i) for i in range(self.joystick.get_numbuttons())]
        self.lst_hat = [self.joystick.get_hat(i) for i in range(self.joystick.get_numhats())]

        self.button_click = self._check_button_click(self.lst_button, self.button)
        self.hat_click = self._check_hat_click(self.lst_hat, self.hat)

        self.dead_zone = 0.1

    @staticmethod
    def _check_button_click(lst, cur):
        rise = [0] * len(lst)
        for i in range(len(lst)):
            if not lst[i] and cur[i]:
                rise[i] = 1
        return rise

    @staticmethod
    def _check_hat_click(lst, cur):
        rise = [(0, 0)] * len(lst)
        for i in range(len(lst)):
            h0 = 1 if not lst[i][0] and cur[i][0] else 0
            h1 = 1 if not lst[i][1] and cur[i][1] else 0
            rise[i] = (h0, h1)
        return rise

    @staticmethod
    def count():
        pygame.init()
        return pygame.joystick.get_count()

    @staticmethod
    def device_name(id):
        if id < JoyStick.count():
            return pygame.joystick.Joystick(id).get_name()
        else:
            return None

    def name(self):
        return self.joystick.get_name()

    def refresh(self):
        pygame.event.pump()

        for i in range(self.joystick.get_numaxes()):
            axis = self.joystick.get_axis(i)
            self.axis[i] = self.axis[i] * self.__alpha + axis * (1.0 - self.__alpha)
            if -self.dead_zone < self.axis[i] < self.dead_zone:
                self.axis[i] = 0

        self.lst_button = self.button
        self.lst_hat = self.hat

        self.button = [self.joystick.get_button(i) for i in range(self.joystick.get_numbuttons())]
        self.hat = [self.joystick.get_hat(i) for i in range(self.joystick.get_numhats())]

        self.button_click = self._check_button_click(self.lst_button, self.button)
        self.hat_click = self._check_hat_click(self.lst_hat, self.hat)

class PS4Robot(object):
    def __init__(self, id=0):

        self.ps4 = JoyStick(id)
        if self.ps4.name().find("PS4") < 0:
            raise Exception("No a PS4 handle")

        self.dx = 0.0
        self.dy = 0.0
        self.step = 0.0
        self.yaw = 0.0
        self.speed = 0.0

    def refresh(self):
        self.ps4.refresh()

        x = self.ps4.axis[2]
        y = -self.ps4.axis[3]
        xy = math.sqrt(x * x + y * y)
        if xy < 0.1:
            self.dx = 0.0
            self.dy = 0.0
            self.step = 0.0
        else:
            self.dx = x / xy
            self.dy = y / xy
            self.step = max(math.fabs(x), math.fabs(y))
        self.yaw = self.ps4.axis[0]

        if self.ps4.button_click[3]:
            self.speed += 0.1
        elif self.ps4.button_click[0]:
            self.speed -= 0.1
        self.speed = min(1.0, max(0.0, self.speed))
        msg_list=[]
        msg_list.append(self.dx)
        msg_list.append(self.dy)
        msg_list.append(self.step)
        msg_list.append(self.yaw)
        msg_list.append(self.speed)
        return msg_list
        # print("Yaw = %3.1f" % self.yaw)
        # print("Speed = %3.1f" % self.speed)

二、QT界面绘制以及程序生成

使用QT Designer绘制好界面,然后保存为.ui文件,最后将.ui生成.py文件导入程序中即可使用。
QT Designer软件界面如下:
python获取ps4手柄信息并使用QT界面显示读取内容
使用命令行pyuic5 -o login.py login.ui转换成.py文件。调用格式为pyuic5 -o {输出文件名} {输入designer设计好的.ui后缀界面文件}。

三、完整展示

分为两部分程序,主程序以及qt界面程序:
ps4_contrlo.py程序为主程序,login.py为界面程序,放入一个目录下,运行ps4_control.py即可运行(安装好所需库)

  1. ps4_control
import pygame
import time
import numpy as np
import math
import sys
#导入designer工具生成的login模块
from login import Ui_Form
from PyQt5 import QtCore, QtGui, QtWidgets
#PyQt5中使用的基本控件都在PyQt5.QtWidgets模块中
from PyQt5.QtWidgets import *

class JoyStick(object):
    def __init__(self, id):
        pygame.init()
        pygame.joystick.init()
        if pygame.joystick.get_count() == 0:
            raise Exception("Joystick not found!")

        self.joystick = pygame.joystick.Joystick(id)
        self.joystick.init()

        self.axis = np.zeros(self.joystick.get_numaxes())
        self.__alpha = 0.0

        self.button = [self.joystick.get_button(i) for i in range(self.joystick.get_numbuttons())]
        self.hat = [self.joystick.get_hat(i) for i in range(self.joystick.get_numhats())]

        self.lst_button = [self.joystick.get_button(i) for i in range(self.joystick.get_numbuttons())]
        self.lst_hat = [self.joystick.get_hat(i) for i in range(self.joystick.get_numhats())]

        self.button_click = self._check_button_click(self.lst_button, self.button)
        self.hat_click = self._check_hat_click(self.lst_hat, self.hat)

        self.dead_zone = 0.1

    @staticmethod
    def _check_button_click(lst, cur):
        rise = [0] * len(lst)
        for i in range(len(lst)):
            if not lst[i] and cur[i]:
                rise[i] = 1
        return rise

    @staticmethod
    def _check_hat_click(lst, cur):
        rise = [(0, 0)] * len(lst)
        for i in range(len(lst)):
            h0 = 1 if not lst[i][0] and cur[i][0] else 0
            h1 = 1 if not lst[i][1] and cur[i][1] else 0
            rise[i] = (h0, h1)
        return rise

    @staticmethod
    def count():
        pygame.init()
        return pygame.joystick.get_count()

    @staticmethod
    def device_name(id):
        if id < JoyStick.count():
            return pygame.joystick.Joystick(id).get_name()
        else:
            return None

    def name(self):
        return self.joystick.get_name()

    def refresh(self):
        pygame.event.pump()

        for i in range(self.joystick.get_numaxes()):
            axis = self.joystick.get_axis(i)
            self.axis[i] = self.axis[i] * self.__alpha + axis * (1.0 - self.__alpha)
            if -self.dead_zone < self.axis[i] < self.dead_zone:
                self.axis[i] = 0

        self.lst_button = self.button
        self.lst_hat = self.hat

        self.button = [self.joystick.get_button(i) for i in range(self.joystick.get_numbuttons())]
        self.hat = [self.joystick.get_hat(i) for i in range(self.joystick.get_numhats())]

        self.button_click = self._check_button_click(self.lst_button, self.button)
        self.hat_click = self._check_hat_click(self.lst_hat, self.hat)

class PS4Robot(object):
    def __init__(self, id=0):

        self.ps4 = JoyStick(id)
        if self.ps4.name().find("PS4") < 0:
            raise Exception("No a PS4 handle")

        self.dx = 0.0
        self.dy = 0.0
        self.step = 0.0
        self.yaw = 0.0
        self.speed = 0.0

    def refresh(self):
        self.ps4.refresh()

        x = self.ps4.axis[2]
        y = -self.ps4.axis[3]
        xy = math.sqrt(x * x + y * y)
        if xy < 0.1:
            self.dx = 0.0
            self.dy = 0.0
            self.step = 0.0
        else:
            self.dx = x / xy
            self.dy = y / xy
            self.step = max(math.fabs(x), math.fabs(y))
        self.yaw = self.ps4.axis[0]

        if self.ps4.button_click[3]:
            self.speed += 0.1
        elif self.ps4.button_click[0]:
            self.speed -= 0.1
        self.speed = min(1.0, max(0.0, self.speed))
        msg_list=[]
        msg_list.append(self.dx)
        msg_list.append(self.dy)
        msg_list.append(self.step)
        msg_list.append(self.yaw)
        msg_list.append(self.speed)
        return msg_list
        # print("Yaw = %3.1f" % self.yaw)
        # print("Speed = %3.1f" % self.speed)
class MyMainForm(QMainWindow, Ui_Form):
    def __init__(self, robot,parent=None):
        super(MyMainForm, self).__init__(parent)
        self.setupUi(self)
        self.robot=robot

        self.pushButton_2.clicked.connect(self.close)
        self.timer=QtCore.QTimer()
        self.timer.timeout.connect(self.display)
        self.timer.start(100)



    def display(self):
        msg=self.robot.refresh()
       

        self.line_2.setGeometry(QtCore.QRect(130+self.robot.yaw*80, 30, 20, 31))
        temp=80
        self.line_3.setGeometry(QtCore.QRect(125+self.robot.dx*temp, 202-self.robot.dy*temp, 50, 20))

        self.line_4.setGeometry(QtCore.QRect(140+self.robot.dx*temp, 187-self.robot.dy*temp, 20, 50))

        self.line_10.setGeometry(QtCore.QRect(299, 300-self.robot.step*200, 20, 20+self.robot.step*200))
        # 利用text Browser控件对象setText()函数设置界面显示
        self.progressBar.setProperty("value",self.robot.speed*100)
        self.textBrowser.setText( "x: " + str(self.robot.dx) + "\n"+"y: "+str(self.robot.dy)+"\n"+"step: " + str(self.robot.step) +"\n"+ "yaw: " + str(self.robot.yaw)+"\n"+"speed: " + str(self.robot.speed))



if __name__ == '__main__':

    # 初始化
    robot = PS4Robot()
    print(robot.ps4.name())
    app = QApplication(sys.argv)
    myWin = MyMainForm(robot)
    #将窗口控件显示在屏幕上
    myWin.show()
    #程序运行,sys.exit方法确保程序完整退出。
    myWin.display()
    time.sleep(0.1)
    sys.exit(app.exec_())


```、

2. login.py(将login.ui文件生成的.py文件),将login.py文件放置程序当前目录可直接使用

```python
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'login.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(639, 414)
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(85, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(85, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(85, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(85, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
        Form.setPalette(palette)
        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(120, 380, 101, 31))
        font = QtGui.QFont()
        font.setPointSize(15)
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.pushButton_2 = QtWidgets.QPushButton(Form)
        self.pushButton_2.setGeometry(QtCore.QRect(440, 360, 111, 31))
        self.pushButton_2.setObjectName("pushButton_2")
        self.progressBar = QtWidgets.QProgressBar(Form)
        self.progressBar.setGeometry(QtCore.QRect(50, 360, 221, 23))
        self.progressBar.setMaximum(100)
        self.progressBar.setProperty("value", 0)
        self.progressBar.setObjectName("progressBar")
        self.label_2 = QtWidgets.QLabel(Form)
        self.label_2.setGeometry(QtCore.QRect(120, 60, 71, 20))
        self.label_2.setBaseSize(QtCore.QSize(0, 0))
        font = QtGui.QFont()
        font.setPointSize(15)
        font.setBold(True)
        font.setWeight(75)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.textBrowser = QtWidgets.QTextBrowser(Form)
        self.textBrowser.setGeometry(QtCore.QRect(350, 90, 261, 251))
        font = QtGui.QFont()
        font.setPointSize(15)
        self.textBrowser.setFont(font)
        self.textBrowser.setFrameShadow(QtWidgets.QFrame.Plain)
        self.textBrowser.setLineWidth(5)
        self.textBrowser.setObjectName("textBrowser")
        self.line = QtWidgets.QFrame(Form)
        self.line.setGeometry(QtCore.QRect(60, 40, 160, 20))
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(120, 120, 120))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush)
        self.line.setPalette(palette)
        self.line.setFrameShadow(QtWidgets.QFrame.Plain)
        self.line.setLineWidth(9)
        self.line.setMidLineWidth(6)
        self.line.setFrameShape(QtWidgets.QFrame.HLine)
        self.line.setObjectName("line")
        self.line_2 = QtWidgets.QFrame(Form)
        self.line_2.setGeometry(QtCore.QRect(130, 30, 20, 31))
        self.line_2.setFrameShadow(QtWidgets.QFrame.Raised)
        self.line_2.setLineWidth(19)
        self.line_2.setMidLineWidth(4)
        self.line_2.setFrameShape(QtWidgets.QFrame.VLine)
        self.line_2.setObjectName("line_2")
        self.label_4 = QtWidgets.QLabel(Form)
        self.label_4.setGeometry(QtCore.QRect(400, 60, 141, 20))
        self.label_4.setBaseSize(QtCore.QSize(0, 0))
        font = QtGui.QFont()
        font.setPointSize(15)
        font.setBold(True)
        font.setWeight(75)
        self.label_4.setFont(font)
        self.label_4.setObjectName("label_4")
        self.label_5 = QtWidgets.QLabel(Form)
        self.label_5.setGeometry(QtCore.QRect(144, 320, 21, 20))
        self.label_5.setBaseSize(QtCore.QSize(0, 0))
        font = QtGui.QFont()
        font.setPointSize(15)
        font.setBold(True)
        font.setWeight(75)
        self.label_5.setFont(font)
        self.label_5.setObjectName("label_5")
        self.label_6 = QtWidgets.QLabel(Form)
        self.label_6.setGeometry(QtCore.QRect(20, 204, 21, 20))
        self.label_6.setBaseSize(QtCore.QSize(0, 0))
        font = QtGui.QFont()
        font.setPointSize(15)
        font.setBold(True)
        font.setWeight(75)
        self.label_6.setFont(font)
        self.label_6.setObjectName("label_6")
        self.line_3 = QtWidgets.QFrame(Form)
        self.line_3.setGeometry(QtCore.QRect(125, 202, 50, 20))
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(120, 120, 120))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush)
        self.line_3.setPalette(palette)
        self.line_3.setFrameShadow(QtWidgets.QFrame.Plain)
        self.line_3.setLineWidth(10)
        self.line_3.setMidLineWidth(10)
        self.line_3.setFrameShape(QtWidgets.QFrame.HLine)
        self.line_3.setObjectName("line_3")
        self.line_4 = QtWidgets.QFrame(Form)
        self.line_4.setGeometry(QtCore.QRect(140, 187, 20, 50))
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(120, 120, 120))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush)
        self.line_4.setPalette(palette)
        self.line_4.setFrameShadow(QtWidgets.QFrame.Plain)
        self.line_4.setLineWidth(10)
        self.line_4.setMidLineWidth(10)
        self.line_4.setFrameShape(QtWidgets.QFrame.VLine)
        self.line_4.setObjectName("line_4")
        self.line_5 = QtWidgets.QFrame(Form)
        self.line_5.setGeometry(QtCore.QRect(30, 100, 20, 220))
        self.line_5.setFrameShadow(QtWidgets.QFrame.Plain)
        self.line_5.setLineWidth(5)
        self.line_5.setFrameShape(QtWidgets.QFrame.VLine)
        self.line_5.setObjectName("line_5")
        self.line_6 = QtWidgets.QFrame(Form)
        self.line_6.setGeometry(QtCore.QRect(250, 100, 20, 220))
        self.line_6.setFrameShadow(QtWidgets.QFrame.Plain)
        self.line_6.setLineWidth(5)
        self.line_6.setFrameShape(QtWidgets.QFrame.VLine)
        self.line_6.setObjectName("line_6")
        self.line_7 = QtWidgets.QFrame(Form)
        self.line_7.setGeometry(QtCore.QRect(38, 92, 224, 16))
        self.line_7.setFrameShadow(QtWidgets.QFrame.Plain)
        self.line_7.setLineWidth(5)
        self.line_7.setFrameShape(QtWidgets.QFrame.HLine)
        self.line_7.setObjectName("line_7")
        self.line_8 = QtWidgets.QFrame(Form)
        self.line_8.setGeometry(QtCore.QRect(40, 310, 220, 16))
        self.line_8.setFrameShadow(QtWidgets.QFrame.Plain)
        self.line_8.setLineWidth(5)
        self.line_8.setFrameShape(QtWidgets.QFrame.HLine)
        self.line_8.setObjectName("line_8")
        self.label_7 = QtWidgets.QLabel(Form)
        self.label_7.setGeometry(QtCore.QRect(20, 110, 21, 20))
        self.label_7.setBaseSize(QtCore.QSize(0, 0))
        font = QtGui.QFont()
        font.setPointSize(15)
        font.setBold(True)
        font.setWeight(75)
        self.label_7.setFont(font)
        self.label_7.setObjectName("label_7")
        self.label_8 = QtWidgets.QLabel(Form)
        self.label_8.setGeometry(QtCore.QRect(10, 290, 21, 20))
        self.label_8.setBaseSize(QtCore.QSize(0, 0))
        font = QtGui.QFont()
        font.setPointSize(15)
        font.setBold(True)
        font.setWeight(75)
        self.label_8.setFont(font)
        self.label_8.setObjectName("label_8")
        self.label_9 = QtWidgets.QLabel(Form)
        self.label_9.setGeometry(QtCore.QRect(240, 320, 21, 20))
        self.label_9.setBaseSize(QtCore.QSize(0, 0))
        font = QtGui.QFont()
        font.setPointSize(15)
        font.setBold(True)
        font.setWeight(75)
        self.label_9.setFont(font)
        self.label_9.setObjectName("label_9")
        self.label_10 = QtWidgets.QLabel(Form)
        self.label_10.setGeometry(QtCore.QRect(40, 320, 21, 20))
        self.label_10.setBaseSize(QtCore.QSize(0, 0))
        font = QtGui.QFont()
        font.setPointSize(15)
        font.setBold(True)
        font.setWeight(75)
        self.label_10.setFont(font)
        self.label_10.setObjectName("label_10")
        self.line_9 = QtWidgets.QFrame(Form)
        self.line_9.setGeometry(QtCore.QRect(300, 300, 21, 21))
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(255, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(120, 120, 120))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush)
        self.line_9.setPalette(palette)
        font = QtGui.QFont()
        font.setPointSize(12)
        self.line_9.setFont(font)
        self.line_9.setFrameShadow(QtWidgets.QFrame.Raised)
        self.line_9.setLineWidth(10)
        self.line_9.setMidLineWidth(3)
        self.line_9.setFrameShape(QtWidgets.QFrame.VLine)
        self.line_9.setObjectName("line_9")
        self.label_11 = QtWidgets.QLabel(Form)
        self.label_11.setGeometry(QtCore.QRect(290, 330, 51, 20))
        self.label_11.setBaseSize(QtCore.QSize(0, 0))
        font = QtGui.QFont()
        font.setPointSize(15)
        font.setBold(True)
        font.setWeight(75)
        self.label_11.setFont(font)
        self.label_11.setObjectName("label_11")
        self.line_10 = QtWidgets.QFrame(Form)
        self.line_10.setGeometry(QtCore.QRect(299, 300, 20, 20))
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(0, 255, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 255, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(120, 120, 120))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush)
        self.line_10.setPalette(palette)
        self.line_10.setFrameShadow(QtWidgets.QFrame.Plain)
        self.line_10.setLineWidth(15)
        self.line_10.setMidLineWidth(3)
        self.line_10.setFrameShape(QtWidgets.QFrame.VLine)
        self.line_10.setObjectName("line_10")
        self.line_11 = QtWidgets.QFrame(Form)
        self.line_11.setGeometry(QtCore.QRect(302, 90, 16, 10))
        self.line_11.setFrameShadow(QtWidgets.QFrame.Raised)
        self.line_11.setLineWidth(10)
        self.line_11.setMidLineWidth(3)
        self.line_11.setFrameShape(QtWidgets.QFrame.VLine)
        self.line_11.setObjectName("line_11")
        self.line_12 = QtWidgets.QFrame(Form)
        self.line_12.setGeometry(QtCore.QRect(293, 100, 20, 201))
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(0, 255, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 255, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(120, 120, 120))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush)
        self.line_12.setPalette(palette)
        self.line_12.setFrameShadow(QtWidgets.QFrame.Plain)
        self.line_12.setLineWidth(3)
        self.line_12.setFrameShape(QtWidgets.QFrame.VLine)
        self.line_12.setObjectName("line_12")
        self.line_13 = QtWidgets.QFrame(Form)
        self.line_13.setGeometry(QtCore.QRect(306, 100, 20, 201))
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(0, 255, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 255, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(120, 120, 120))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush)
        self.line_13.setPalette(palette)
        self.line_13.setFrameShadow(QtWidgets.QFrame.Plain)
        self.line_13.setLineWidth(3)
        self.line_13.setFrameShape(QtWidgets.QFrame.VLine)
        self.line_13.setObjectName("line_13")
        self.line_10.raise_()
        self.label.raise_()
        self.pushButton_2.raise_()
        self.progressBar.raise_()
        self.label_2.raise_()
        self.textBrowser.raise_()
        self.line.raise_()
        self.line_2.raise_()
        self.label_4.raise_()
        self.label_5.raise_()
        self.label_6.raise_()
        self.line_3.raise_()
        self.line_4.raise_()
        self.line_5.raise_()
        self.line_6.raise_()
        self.line_7.raise_()
        self.line_8.raise_()
        self.label_7.raise_()
        self.label_8.raise_()
        self.label_9.raise_()
        self.label_10.raise_()
        self.label_11.raise_()
        self.line_11.raise_()
        self.line_12.raise_()
        self.line_13.raise_()
        self.line_9.raise_()

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.label.setText(_translate("Form", "speed"))
        self.pushButton_2.setText(_translate("Form", "退出"))
        self.label_2.setText(_translate("Form", "yaw"))
        self.label_4.setText(_translate("Form", "data display"))
        self.label_5.setText(_translate("Form", "x"))
        self.label_6.setText(_translate("Form", "y"))
        self.label_7.setText(_translate("Form", "1"))
        self.label_8.setText(_translate("Form", "-1"))
        self.label_9.setText(_translate("Form", "1"))
        self.label_10.setText(_translate("Form", "-1"))
        self.label_11.setText(_translate("Form", "step"))


运行界面展示

程序中需要手柄提供控制方向x,y,yaw(偏航角)以及机器人速度speed和步长step。

python获取ps4手柄信息并使用QT界面显示读取内容
python获取ps4手柄信息并使用QT界面显示读取内容文章来源地址https://www.toymoban.com/news/detail-418825.html

到了这里,关于python获取ps4手柄信息并使用QT界面显示读取内容的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Unity PS4/PS5搭建开发环境/打补丁包和一些填坑经验

    首先,主机游戏PlayStation/Nintendo Switch都是比较闭塞的,开发者账号是必须的。 开发环境有两个部分,一是SDK Kit(各种开发调试环境),二是Unity的支持库(安装后才能在Unity中切换到PS平台); 需要严格按照SDK版本和对应支持的Unity版本安装开发环境。 1. 安装PS4/PS5 SDK: 登录开发

    2024年02月14日
    浏览(139)
  • mac m1/m2 芯片安装 ps 2023 插件无法显示扩展界面

    碎碎念:一直在踩坑的路上,甚至想休息时间玩一会儿 ps 都能踩坑 新的 m2 芯片 mac 安装了色环插件后,在窗口界面中没有找到扩展,且在首选项-增效工具的旧版扩展也是灰色的 题外话:记录一下 mac 的 photoshop 安装插件的方法,不然下次又忘记咯 Q:如何安装插件? A:找到

    2024年02月11日
    浏览(150)
  • PyQt5 Qt Designer使用(界面显示与业务逻辑分离模式)

    Qt Designer运行界面: MainWinSignalSlog02.ui Ui_MainWinSignalSlog02.py CallMainWinSignalSlog02.py

    2024年02月15日
    浏览(37)
  • 启动了容器 但是 docker ps 不显示任何容器信息

    1.镜像存再 2.启动显示成功 3.docker ps 不显示任何容器 4.其实已经启动了,可以使用 docker ps -a 5.原因分析 为了理解为什么某个容器看不到,我们需要了解 Dcker 的内部工作原理Docker 使用了-个守护进程(Docker Daemon) 来管理容器和像,当我们使用 docker rur命令来创建一个容器时,实

    2024年04月13日
    浏览(36)
  • stm32 cubemx ps2无线(有线)手柄

    本文讲解使用cubemx配置PS2手柄实现对手柄的按键和模拟值的读取。 很简单,库已经封装好了,直接就可以了。 文件 这个很简单,不需要走协议,只要配置gpio就可以了 使用我的两个库 bsp_hal_ps2.c bsp_hal_ps2.h 初始化 主循环

    2024年02月14日
    浏览(44)
  • Unity针对XBOX,SWITCH,PS5手柄的适配踩坑

    记录一点最近在做手柄适配问题的踩坑。 这里推荐一款Unity做手柄适配的插件- Rewired Rewired官方文档链接Rewired Documentation | Supported Controllers Rewired插件里面有个是Player类,这个类获取到当前玩家的输入设备,输入的值等。 我用Player.GetAxisRaw和Player.GetAxis发现对手柄而言,这两个

    2024年01月20日
    浏览(73)
  • 【STM32】SPI与PS2手柄解码(CUBEMX+HAL库)

    本文工程文件以及ps2数据手册在这个链接,我设置成免费了 【免费】STM32PS2解码工程以及代码(CUBEMX+HAL库)资源-CSDN文库 目录   SPI简介 SPI引脚说明 一些参数的含义 通信的四种模式 通信过程简介 关于SPI的常用HAL库函数 PS2简介 ps2手柄 ps2接收器 PS2解码 CUBEMX工程配置 PS2解码

    2024年04月17日
    浏览(33)
  • 设计qt界面并通过代码显示

    方法 1: 在 ui 文件所在的文件夹中,按快捷键 Shift+鼠标右键,在弹出的菜单栏中选择“在此处打开 Powershell\\\" 在终端输入指令:pyuic5 -o xxx.py py xxx.ui 例子:pyuic5 -o 界面.py untitled.ui 方法 2:通过 python 代码直接调用终端输入命令 方法 1.界面生成的代码 + python 代码 方法 2.ui 文件

    2024年02月09日
    浏览(38)
  • qt 获取登录界面账号和密码

     

    2024年02月16日
    浏览(41)
  • 【pyqt5界面化工具开发-11】界面化显示检测信息

    目录 0x00 前言: 一、布局的设置 二、消息的显示 我们在10讲的基础上,需要将其输出到界面上 思路: 1、消息的传递 2、布局的设置 先考虑好消息的传递,再来完善布局 其实先完善布局,再来设置消息的传递也行 (我不是中国人,就是外国人) 将布局设置好以后,我们就

    2024年02月10日
    浏览(45)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包