Ask Your Question

How to update a gui (python) each time the callback function is called in a subscriber node?

asked 2016-05-11 16:59:28 -0500

highWaters gravatar image


I want to have several nodes send their position (x,y) to a 'visualizer' node which draws on some window/gui continuously as more data is published by the former nodes.

What I have so far (considering 1 publisher node and 1 visualizer node):

import sys
import rospy
from GITagent.msg import Position
from PyQt4.QtGui import *
from PyQt4.QtCore import *

app = QApplication(sys.argv)
w = QWidget()

def callback(data):
    rospy.loginfo(rospy.get_caller_id() + "I heard %d, %d", data.x_pos, data.y_pos)
    qp = QtGui.QPainter()
    drawPoint(data.x_pos, data.y_pos, qp)

def draw_xypoints(xp, yp, qp):
    qp.drawPoint(xp, yp)    

def visualizer():
    rospy.init_node('visualizer', anonymous=True)

    rospy.Subscriber('mypos_xytheta', Position, callback)


if __name__ == '__main__':
    w.setWindowTitle("1-2-3 Test")

When I run, the window pops up, but nothing else happens. Also I there is no feedback from the rospy.loginfo(). Apparently I am missing something here. (Obviously, I am not either a python or ros expert, just know some basic stuff). Any help is appreciated:).

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2016-05-11 19:30:06 -0500

robustify gravatar image

Your immediate problem is that app.exec_() blocks your program until you close the window, which hinders the functioning of your ROS node. Second, sys.exit() exits the program, so your ROS node will never get started anyway.

You'll have to run your QApplication in its own thread and manage them accordingly in order to do what you want I think. I would recommend creating a rqt_gui plugin instead:

A lot of things are done for you, and you can run your GUI within RQT. Also, you can use Qt Designer to layout the GUI graphically, or you can spawn GUI components manually in code as well.

edit flag offensive delete link more


I see. I'll try your suggestions. Thanks for your insights!!

highWaters gravatar image highWaters  ( 2016-05-12 14:57:05 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2016-05-11 16:59:28 -0500

Seen: 2,536 times

Last updated: May 11 '16