How to run rospy.spin() and Tkinter.mainloop() at the same time?
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()