ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
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

2 Answers

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

answered 2022-12-02 16:11:59 -0500

ArH gravatar image

Hi highWaters,

After 6 years, I am now facing the same problem. And I have made a lot of effort into my Qt part, so I definitely want to keep that. Here is a trick that I found to solve this problem. In one sentence, set a timer before app.exec() to launch ROS::spin after app.exec().

A demo code:

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
class Window(QMainWindow):
    def __init__(self):
        self.sub_info = rospy.Subscriber("info", String, self.callback_info)
    def callback_info(self, data):

def timer_callback(event):
    global timer
    rospy.spin() # start the ros spin here

if __name__ == '__main__':
    # ROS
    rospy.init_node('GUI', anonymous=True)

    # create pyqt5 app
    App = QApplication(sys.argv)
    window = Window()

    # start ros spin
    timer = rospy.Timer(rospy.Duration(1),timer_callback) # start the ros spin after 1 s

    # start the app

It is a trick, and there should be some better ways, but it solves the problem, at least works for me.


-- Chen said: "Be professional and considerate. Be quick and dirty."

edit flag offensive delete link more

Question Tools

1 follower


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

Seen: 3,270 times

Last updated: May 11 '16