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

How to run rospy.spin() and Tkinter.mainloop() at the same time?

asked 2018-08-27 10:26:07 -0500

avalice gravatar image

Hello,

I'm trying to learn ROS by writing a really simple group-chat program in Python 2.7, Tkinter and ROS Kinetic.

I built a simple GUI on Tkinter that will publish the Entry's value to the topic when the user presses Enter.

I am stuck on this issue, where I need to run both rospy.spin() in order to subscribe to the topic, and Tkinter.mainloop(), in order for the GUI to work.

May I know how do I run both at the same time? From what I understand they each halt the program.

#!/usr/bin/env python

import rospy
import Tkinter as tk
from std_msgs.msg import String

def send(event):
        pub = rospy.Publisher("chat", String, queue_size=10)
        rospy.loginfo("attempting to send: " + entry_field.get())
        pub.publish(entry_field.get())
        entry_field.delete(0, "end")

def receive(msg):
        rospy.loginfo(rospy.get_caller_id() + msg)

if __name__ == "__main__":
        rospy.init_node("chat", anonymous=True)
        rospy.Subscriber("lets_talk", String, receive)

        root = tk.Tk()
        root.title("Let's chat!")

        msg_frame = tk.Frame(root)
        msg_frame.pack()

        msg_list = tk.Listbox(msg_frame, height=30, width=100)
        msg_list.pack()

        entry_field = tk.Entry(root)
        entry_field.bind("<Return>", send)
        entry_field.pack()

        tk.mainloop()
        rospy.spin()
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2018-08-27 10:38:41 -0500

gvdhoorn gravatar image

updated 2018-08-27 10:41:47 -0500

I am stuck on this issue, where I need to run both rospy.spin() in order to subscribe to the topic, and Tkinter.mainloop(), in order for the GUI to work.

May I know how do I run both at the same time?

This has a really short answer: you don't.

See #q106781 for some explanation.

Summary: rospy.spin() doesn't do much more than sleeping (here). In roscpp, things are different, see some of the other Q&As on this site for that.

edit flag offensive delete link more

Comments

Thank you! That solved my issue!

avalice gravatar image avalice  ( 2018-08-27 10:46:14 -0500 )edit
1

@avalice i am having the same trouble like you had, could you share how you where able to publish topics based on tkinter inputs?

basb1 gravatar image basb1  ( 2019-02-12 11:00:27 -0500 )edit

Question Tools

Stats

Asked: 2018-08-27 10:26:07 -0500

Seen: 2,403 times

Last updated: Aug 27 '18