How to use the TKInter package for Ros Tools
import tkinter as tk
import rospydef turn_function():
t=Twist()
t.angular.z=0.5
cmd_vel.publish(t)
def stop_function():
t=Twist()
t.angular.z=0
cmd_vel.publish(t)rospy.init_node("tkinter_example")
cmd_vel = rospy.Publisher('cmd_vel', Twist, queue_size=1)root=tk.Tk()
root.wm_title("Test TKinter Window")
root.geometry("250x250") #set size of windowturn_button=tk.Button(
root,
text="turn",
bg="grey",
command=turn_function
)
stop_button=tk.Button(
root,
text="stop",
bg="grey",
command=stop_function
)turn_button.pack()
stop_button.pack()root.mainloop()#!/usr/bin/env python
import tkinter as tk #import tkinter
from geometry_msgs.msg import Twist
import rospy
#Put callbacks here
#Put all standard functions here
#Put functions that are called by pressing GUI buttons here
def turn_function():
t=Twist() #creates a twist object
t.angular.z=0.5 #sets the angular velocity of that object so the robot will turn
cmd_vel.publish(t) #publishes the twist
def stop_function():
t=Twist()
t.angular.z=0
cmd_vel.publish(t)
#initialize rospy node, publishers, and subscribers
rospy.init_node("tkinter")
cmd_vel = rospy.Publisher('cmd_vel', Twist, queue_size=1) #standard publisher to control movement
#initialize tkinter window
root=tk.Tk() #initialize window
root.wm_title("Test TKinter Window") #set window name
root.geometry("250x250") #set size of window
#create widgets
turn_button=tk.Button( #creates a button
root, #sets the button to be on the root, but this could also be a frame or canvas if you want
text="turn", #The text on the button
bg="green", #The background of the button, tkinter lets you write out color names for many standard colors, but you can also use hex colors or rgb values
command=turn_function #command will tie a previously defined function to your button. You must define this function earlier in the code.
)
stop_button=tk.Button(
root,
text="stop",
bg="red",
command=stop_function
)
#adding the buttons to the window
turn_button.pack() #pack will simply stack each widget on the screen in the order they were packed, but that can be changed with various arguments in the pack method. Please check the tkinter documentation to see more options.
stop_button.pack()
#Tkinter Mainloop
root.mainloop()