Home > Software design >  PyQT GUI freezes when I update several labels' text 30 times in second
PyQT GUI freezes when I update several labels' text 30 times in second

Time:07-21

I wrote a PyQT5 code. It connects to a ROS topic which produces messages with 30 Hz (30 messages in 1 second).

This is an image from part of GUI: enter image description here

When a user selects a gaze topic from Combobox, I call create_subscriber_gaze_information function with the help of connect signal for the combo box.

    # create ROS subscriber for gaze information if the value of its combo box changed
    ui.gaze_info_topic_name_value.activated.connect(create_subscriber_gaze_information)

create_subscriber_gaze_information function creates a ROS subscriber that reads messages from the topic when it becomes available:

def create_subscriber_gaze_information():

    # unsubscribe to the previous topic
    if ui.gaze_info_topic_name_value.ros_subs is not None:
        ui.gaze_info_topic_name_value.ros_subs.unregister()

    # create a subscriber
    ui.gaze_info_topic_name_value.ros_subs = rospy.Subscriber(
                                                        name=ui.gaze_info_topic_name_value.currentText(), 
                                                        data_class=gazesense_msgs.msg.TrackedUserArray, 
                                                        callback=show_gaze_information,
                                                        queue_size=1)

Whenever a message becomes available (30 times per second), function show_gaze_information will be called automatically. This function updates the value of labels in GUI:

def show_gaze_information(gaze_msg):
   
    if ui.groupBox_gaze.is_lock == True:
        return

    ui.groupBox_gaze.is_lock = True

    for person in gaze_msg.people:
        
        #################################
        # head info
        #################################
        # set value of head tranlation x
        ui.label_head_translation_x_value.setText("{}".format(person.tracking_info.head_pose.transform.translation.x))
        # set value of head tranlation y
        ui.label_head_translation_y_value.setText("{}".format(person.tracking_info.head_pose.transform.translation.y))
        # set value of head tranlation z
        ui.label_head_translation_z_value.setText("{}".format(person.tracking_info.head_pose.transform.translation.z))
        # set value of head rotation x
        ui.label_head_rotation_x_value.setText("{}".format(person.tracking_info.head_pose.transform.rotation.x))
        # set value of head rotation y
        ui.label_head_rotation_y_value.setText("{}".format(person.tracking_info.head_pose.transform.rotation.y))
        # set value of head rotation z
        ui.label_head_rotation_z_value.setText("{}".format(person.tracking_info.head_pose.transform.rotation.z))
        # set value of head rotation w
        ui.label_head_rotation_w_value.setText("{}".format(person.tracking_info.head_pose.transform.rotation.w))
        # set value of is lost
        ui.label_head_is_lost_value.setText("{}".format(person.tracking_info.head_pose.is_lost))
        #################################
        # left eye
        #################################
        # set value of left eye orgin x
        ui.label_left_eye_orgin_x_value.setText("{}".format(person.tracking_info.gaze.left_eye_ray.origin.x))
        # set value of left eye orgin y
        ui.label_left_eye_orgin_y_value.setText("{}".format(person.tracking_info.gaze.left_eye_ray.origin.y))
        # set value of left eye orgin z
        ui.label_left_eye_orgin_z_value.setText("{}".format(person.tracking_info.gaze.left_eye_ray.origin.z))
        # set value of left eye dir x
        ui.label_left_eye_dir_x_value.setText("{}".format(person.tracking_info.gaze.left_eye_ray.direction.x))
        # set value of left eye dir y
        ui.label_left_eye_dir_y_value.setText("{}".format(person.tracking_info.gaze.left_eye_ray.direction.y))
        # set value of left eye dir z
        ui.label_left_eye_dir_z_value.setText("{}".format(person.tracking_info.gaze.left_eye_ray.direction.z))
        # set value of left confidence
        ui.label_left_eye_conf_value.setText("{}".format(person.tracking_info.gaze.confidence_left.value))
        # set value of is lost
        ui.label_left_eye_is_lost_value.setText("{}".format(person.tracking_info.gaze.is_lost))
 
    ui.groupBox_gaze.is_lock = False

However, the problem is that after running the program and selecting a topic from the combo box, it works for up to 10 seconds; after that, everything freezes.

CodePudding user response:

I found the problem.

The problem is that I update labels in show_gaze_information.

This function is a callback that is called when a message is published on the topic.

To correct the code, in function show_gaze_information, the message should be stored in a variable. Nothing should be modified in UI.

Then in main body of code, I added this part:

# update gaze infor periodically
timer_gaze = QtCore.QTimer()
timer_gaze.timeout.connect(update_gaze_information)
timer_gaze.setInterval(500)
timer_gaze.start()

This code periodically calls update_gaze_information every 0.5 seconds. Now, in this function, I update labels in the UI.

  • Related