Qt Designer从使用Python的Line Edit存储和解析用户输入到机器人操作系统ROS

问题描述

我目前正在尝试创建一个GUI,以允许用户键入他们想要翻译成R2-D2语音的任何单词。

我已经使用Qt 5 Designer通过Line Edit和一个按钮将用户输入发布到ROS中的指定主题,以创建用户输入。我还使用pyuic5 -o作为r2d2_sound_control.py将其转换为python文件我有一个名为main_r2d2_sound_control.py的主文件,将运行该文件来初始化GUI。

如何使用python存储和解析Line Edit中的字符串?我不知道如何在单击按钮时从用户输入中声明字符串并进行解析。我正在使用pyqt 4

谢谢。

链接到GitHub https://github.com/koide3/ros2d2中的R2-D2语音包

r2d2_sound_control.py的内容

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

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

from PyQt5 import QtCore,QtGui,QtWidgets

class Ui_Dialog(object):
    def setupUi(self,Dialog):
        Dialog.setobjectName("Dialog")
        Dialog.resize(505,114)
        self.pushButton_speak = QtWidgets.QPushButton(Dialog)
        self.pushButton_speak.setGeometry(QtCore.QRect(300,40,89,25))
        self.pushButton_speak.setobjectName("pushButton_speak")
        self.lineEdit_speak = QtWidgets.QLineEdit(Dialog)
        self.lineEdit_speak.setGeometry(QtCore.QRect(60,201,25))
        self.lineEdit_speak.setobjectName("lineEdit_speak")

        self.retranslateUi(Dialog)
        self.pushButton_speak.clicked.connect(Dialog.clicked_speak)
        self.lineEdit_speak.returnpressed.connect(Dialog.clicked_speak)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self,Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setwindowTitle(_translate("Dialog","Dialog"))
        self.pushButton_speak.setText(_translate("Dialog","Say Out"))

main_r2d2_sound_control.py的内容

#! /usr/bin/python3
# -*- coding: utf-8 -*-

# GUI
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
#Import the automatically generated file
from r2d2_sound_control import Ui_Dialog

# ROS
import rospy
from std_msgs.msg import String


class Test(QDialog):
    def __init__(self,parent=None):
        # GUI
        super(Test,self).__init__(parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)

        # ROS pub settings
        self.r2d2_sound_controller_String = String()
        self.pub_r2d2_sound_controller_speak = rospy.Publisher('/ros2d2_node/speak',String,queue_size=10)

    def speak_content(self):
        self.input = string(lineEdit_speak)
        self.lineEdit_speak.setText(self.text())
        

    def clicked_speak(self):
        """
        The slot name specified in Qt Designer.
        Write the process you want to execute with the "Say Out" button.
        """
        self.r2d2_sound_controller_String.data = text()
        self.pub_r2d2_sound_controller_speak.publish(self.r2d2_sound_controller_String)
        self.r2d2_sound_controller_String.data = ''

if __name__ == '__main__':
    rospy.init_node('r2d2_sound_talker')
    app = QApplication(sys.argv)
    window = test()
    window.show()
    sys.exit(app.exec_())

解决方法

感谢我的朋友,我设法解决了这个问题。我对main_r2d2_sound_control.py

进行了以下更改

#! /usr/bin/python3
# -*- coding: utf-8 -*-

# GUI
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
#Import the automatically generated file
from r2d2_head_gripper_sound import Ui_Dialog

# ROS
import rospy
from std_msgs.msg import String

class Test(QDialog):
    def __init__(self,parent=None):
        # GUI
        super(Test,self).__init__(parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)

        # ROS pub settings
        self.r2d2_sound_controller_String = String()
        self.pub_r2d2_sound_controller_speak = rospy.Publisher('/ros2d2_node/speak',String,queue_size=10)

        self.r2d2_sound_controller_String.data = ''

    def clicked_speak(self):
        """
        The slot name specified in Qt Designer.
        Write the process you want to execute with the "Say Out" button.
        """
        text = self.ui.lineEdit_speak.text()
        self.r2d2_sound_controller_String.data = text
        self.pub_r2d2_sound_controller_speak.publish(self.r2d2_sound_controller_String)

if __name__ == '__main__':
    rospy.init_node('r2d2_head_and_gripper_talker')
    app = QApplication(sys.argv)
    window = Test()
    window.show()
    sys.exit(app.exec_())