ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I'd suggest using PySide for Qt/ROS integration. Because of the way callbacks are handled in rospy, you can get away with giving control of the main thread over to the GUI without bad things happening. You need to install PySide from here first.

Here's a trivial example of publishing and subscribing to get you started:

#!/usr/bin/env python

import roslib; roslib.load_manifest('example')
import rospy

from std_msgs.msg import String

from PySide.QtCore import * 
from PySide.QtGui import * 
import sys

class GUI:
    def __init__(self): 
        # A publisher for messages
        self.pub = rospy.Publisher('example', String)

        # A subscriber for our own messages
        self.sub = rospy.Subscriber('example', String, self.sub_callback)

        self.button = QPushButton('push\nme')
        self.button.clicked.connect(self.pub_callback)
        self.button.show()

    def pub_callback(self):
        self.pub.publish('Thank You!')

    def sub_callback(self, msg):
        print 'Got message:', msg.data


if __name__ == '__main__':
    # Initialize the node
    rospy.init_node('example')

    # Initialize Qt
    app = QApplication(sys.argv)

    gui = GUI()

   app.exec_()

You'll also need the following lines in your manifest.xml

<depend package="rospy" />
<depend package="std_msgs" />

I'd suggest using PySide for Qt/ROS integration. Because of the way callbacks are handled in rospy, you can get away with giving control of the main thread over to the GUI without bad things happening. You need to install PySide from here first.

Here's a trivial example of publishing and subscribing to get you started:

#!/usr/bin/env python

import roslib; roslib.load_manifest('example')
import rospy

from std_msgs.msg import String

from PySide.QtCore import * 
from PySide.QtGui import * 
import sys

class GUI:
    def __init__(self): 
        # A publisher for messages
        self.pub = rospy.Publisher('example', String)

        # A subscriber for our own messages
        self.sub = rospy.Subscriber('example', String, self.sub_callback)

        self.button = QPushButton('push\nme')
        self.button.clicked.connect(self.pub_callback)
        self.button.show()

    def pub_callback(self):
        self.pub.publish('Thank You!')

    def sub_callback(self, msg):
        print 'Got message:', msg.data


if __name__ == '__main__':
    # Initialize the node
    rospy.init_node('example')

    # Initialize Qt
    app = QApplication(sys.argv)

    gui = GUI()

    app.exec_()

You'll also need the following lines in your manifest.xml

<depend package="rospy" />
<depend package="std_msgs" />

I'd suggest using PySide for Qt/ROS integration. Because of the way callbacks are handled in rospy, you can get away with giving control of the main thread over to the GUI without bad things happening. You need to install PySide from here first.

Here's a trivial example of publishing and subscribing to get you started:

#!/usr/bin/env python

import roslib; roslib.load_manifest('example')
import rospy

from std_msgs.msg import String

from PySide.QtCore import * 
from PySide.QtGui import * 
import sys

class GUI:
    def __init__(self): 
        # A publisher for messages
        self.pub = rospy.Publisher('example', String)

        # A subscriber for our own messages
        self.sub = rospy.Subscriber('example', String, self.sub_callback)

        self.button = QPushButton('push\nme')
        self.button.clicked.connect(self.pub_callback)
        self.button.show()

    def pub_callback(self):
        self.pub.publish('Thank You!')

    def sub_callback(self, msg):
        print 'Got message:', msg.data


if __name__ == '__main__':
    # Initialize the node
    rospy.init_node('example')

    # Initialize Qt
    app = QApplication(sys.argv)

    gui = GUI()

    app.exec_()

You'll also need the following lines in your manifest.xml

<depend package="rospy" />
<depend package="std_msgs" />