Tkinter and ROS publisher

asked 2019-02-12 01:49:54 -0500

basb1 gravatar image

updated 2019-02-12 02:26:55 -0500

gvdhoorn gravatar image

Hi all,

I am trying to put a GUI together which will control some relays using ros_serial. When a button is clicked, the state of a button is changed and a custom message is published.

At least that is what i am trying to accomplish. When a button is clicked, the publisher just does not publish.

Does anyone had similar experience with Tkinter and ROS?

Below is the code, Kind regards

import rospy
import Tkinter as tk
from alpha_driver.msg import BtnCtrl

class interface(object):
    def __init__(self): 
        self.publisher = rospy.Publisher('/button_control', BtnCtrl, queue_size=10)
        self.window = tk.Tk()
        self.window.title("Alpha control")
        self.window.geometry('500x500')
        self.states = BtnCtrl()

        self.btn_1_state = False
        self.btn_2_state = False
        self.btn_3_state = False
        self.btn_4_state = False
        self.btn_5_state = False
        self.btn_6_state = False

        self.lbl_btn_1 = tk.Label(self.window, text="Switch Empty")
        self.lbl_btn_1.grid(column=0, row=0)
        self.lbl_btn_1 = tk.Label(self.window, text="Empty")
        self.lbl_btn_1.grid(column=2, row=0)
        self.btn_btn_1 = tk.Button(self.window, text="connect", bg="grey", fg="red", command = self.but1Event)
        self.btn_btn_1.grid(column=1, row=0)
        self.btn_btn_1.config( height = 2, width = 8 )

        self.lbl_btn_2 = tk.Label(self.window, text="PWR DRIVERS")
        self.lbl_btn_2.grid(column=0, row=1)
        self.btn_btn_2 = tk.Button(self.window, text="PWR", bg="grey", fg="red", command = self.but2Event)
        self.btn_btn_2.grid(column=1, row=1)
        self.btn_btn_2.config( height = 2, width = 8 )

        self.lbl_btn_3 = tk.Label(self.window, text="Reset base")
        self.lbl_btn_3.grid(column=0, row=2)
        self.btn_btn_3 = tk.Button(self.window, text="RST", bg="grey", fg="red", command = self.but3Event)
        self.btn_btn_3.grid(column=1, row=2)
        self.btn_btn_3.config( height = 2, width = 8 )

        self.lbl_btn_4 = tk.Label(self.window, text="Emergency STOP")
        self.lbl_btn_4.grid(column=0, row=3)
        self.btn_btn_4 = tk.Button(self.window, text="STOP", bg="grey", fg="red", command = self.but4Event)
        self.btn_btn_4.grid(column=1, row=3)
        self.btn_btn_4.config( height = 4, width = 8 )

        self.lbl_bat_4 = tk.Label(self.window, text=" ") 
        self.lbl_bat_4.grid(column=0, row=5)
        self.lbl_bat_4 = tk.Label(self.window, text="------------- Robot Status -------------") 
        self.lbl_bat_4.grid(column=0, row=6)

        self.lbl_stat_1 = tk.Label(self.window, text="Minimal")
        self.lbl_stat_1.grid(column=0, row=7)
        self.lbl_stat_val_1 = tk.Label(self.window, text="False", fg="red")
        self.lbl_stat_val_1.grid(column=1, row=7)

        self.lbl_stat_2 = tk.Label(self.window, text="Navigation")
        self.lbl_stat_2.grid(column=0, row=8)
        self.lbl_stat_val_2 = tk.Label(self.window, text="False", fg="red")
        self.lbl_stat_val_2.grid(column=1, row=8)

        self.lbl_stat_3 = tk.Label(self.window, text="Laser")
        self.lbl_stat_3.grid(column=0, row=9)
        self.lbl_stat_val_3 = tk.Label(self.window, text="False", fg="red")
        self.lbl_stat_val_3.grid(column=1, row=9)

        self.window.protocol("WM_DELETE_WINDOW", rospy.signal_shutdown('alpha_control_node'))

        self.window.mainloop()

    def destroyWindow(self):
        self.window.destroy()

    def but1Event(self): #CONNECT BATTERIES
        if self.btn_1_state == False:
            self.btn_btn_1.configure(bg="yellow")
            self.btn_1_state = True
        elif self.btn_1_state == True:
            self.btn_btn_1.configure(bg="grey")
            self.btn_1_state = False

        self.publish()

    def but2Event ...
(more)
edit retag flag offensive close merge delete

Comments

I have been reading about threading and i am trying to put the node part in a thread and the interface as main class.

basb1 gravatar image basb1  ( 2019-02-12 02:38:12 -0500 )edit