Python Forum

Full Version: Graphic Interface with Tkinter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Foreros, I am making a graphical interface that shows me a couple of data that I receive from an Xbee antenna. Python has a specific library for these modules, so there is no problem with that section.

I'm going to upload two codes. The first code is to send a Broadcast message to all antennas from the PC and to show the data of the messages each time the message is received at the antenna. The second code is an interface that I have built to observe the data that reaches the computer.

THE PROBLEM: I have had a hard time making the data show up after I scratch the button. When I do it, the program does not run well, sometimes it throws me into trouble.


First Code "Sending and Receiving Xbee messages"

As you can see, this code sends a Broadcast message and remains open to receive messages all the time. Well, I look for the same in the interface, when I write the message, the message is sent and it remains open so that the voltages that carry the data are shown on the screens. At least I only occupy the example of how to do it with one, and then I will take care of replicating it in the date, time and average.

I want that after the button is scratched and the message is sent, it remains open receiving messages. And that it is deleted every time another message arrives.

from digi.xbee.devices import XBeeDevice

# TODO: Replace with the serial port where your local module is connected to.
PORT = "COM5"
# TODO: Replace with the baud rate of your local module.
BAUD_RATE = 9600
DATA_TO_SEND = "Hola XBee!"

resultado = 0

def main():
    print(" +--------------------------------------------------+")
    print(" | Sistema de Monitreo de Paneles Solares del SESLab|")
    print(" +--------------------------------------------------+\n")

    device = XBeeDevice(PORT, BAUD_RATE)

    try:
        device.open()

        device.flush_queues()

        print("Inicializando Xbee´s : %s..." % DATA_TO_SEND)

        device.send_data_broadcast(DATA_TO_SEND)

        print("Completado")

        print("Esperando Datos de Sensores...\n")

        while True:
            xbee_message = device.read_data()
            if xbee_message is not None:
                
                print("Dirección de Xbee %s >> %s" % (xbee_message.remote_device.get_64bit_addr(),
                                         xbee_message.data.decode()))
                
                data = xbee_message.data.decode()
                voltage = data.split(",")[0]
                tiempo= data.split(",")[2]
                print (tiempo)
                print (voltage)
                
                
                

    finally:
        if device is not None and device.is_open():
            device.close()


if __name__ == '__main__':
    main()
The second Code "User Interface":

This code shows a graphical interface that will show the time and date, the voltage values of each panel. The voltage value comes inside the data packet. There are three sensors, therefore 3 data frames and three voltages to display.

#-----------------Se importan las librerias--------------------------

from tkinter import*
from digi.xbee.devices import XBeeDevice

#------------------Se inicializan Variables y puertos-----------------

PORT = "COM5"
BAUD_RATE = 9600
device = XBeeDevice(PORT, BAUD_RATE)

#------------------- Se crea la pantalla principal--------------------

raiz=Tk()
raiz.title("Sistema de Monitoreo de Paneles Solares del SESLab")
raiz.config(bg="orange")

miframe=Frame(raiz,width="1200",height="600" )
miframe.pack()
miframe.config(bg="purple")

#-----------------Valores por Mostrar en la Pantallas-----------------

voltage=StringVar()

#--------------- Cuadros donde se muestran datos-----------------------

cuadroVisual= Entry(miframe, textvariable=voltage)
cuadroVisual.grid(row=1, column= 1, padx=1, pady=5)
cuadroVisual.config(fg="black",justify="center")

cuadroVisual= Entry(miframe, textvariable=voltage)
cuadroVisual.grid(row=2, column= 1, padx=1, pady=5)
cuadroVisual.config(fg="black",justify="center")

cuadroVisual= Entry(miframe, textvariable=voltage)
cuadroVisual.grid(row=4, column= 1, padx=1, pady=5)
cuadroVisual.config(fg="black",justify="center")

cuadroVisual= Entry(miframe, textvariable=voltage)
cuadroVisual.grid(row=5, column= 1, padx=1, pady=5)
cuadroVisual.config(fg="black",justify="center")

cuadroVisual= Entry(miframe, textvariable=voltage)
cuadroVisual.grid(row=6, column= 1, padx=1, pady=5)
cuadroVisual.config(fg="black",justify="center")

cuadroVisual= Entry(miframe, textvariable=voltage)
cuadroVisual.grid(row=7, column= 1, padx=1, pady=5)
cuadroVisual.config(fg="black",justify="center")

cuadroVisual= Entry(miframe, textvariable=voltage)
cuadroVisual.grid(row=9, column= 1, padx=1, pady=5)
cuadroVisual.config(fg="black",justify="center")

#----------------Cuadros donde esta el Texto "Voltage de Panel 1"--------

miLabel=Label(miframe, text="Sistema de monitoreo de Paneles del SESLab", font=18)
miLabel.grid(row=0, column= 0, padx= 5, pady=5, columnspan= 2)

nombrePanel=Label(miframe, text="Fecha de medición : ")
nombrePanel.grid(row=1, column= 0, padx= 2, pady=5)

nombrePanel=Label(miframe, text="Hora de medición : ")
nombrePanel.grid(row=2, column= 0, padx= 2, pady=5)

nombrePanel=Label(miframe, text="Voltage de Panel 1 : ")
nombrePanel.grid(row=4, column= 0, padx= 2, pady=5)

nombrePanel=Label(miframe, text="Voltage de Panel 2 : ")
nombrePanel.grid(row=5, column= 0, padx= 2, pady=5)

nombrePanel=Label(miframe, text="Voltage de Panel 3 : ")
nombrePanel.grid(row=6, column= 0, padx= 2, pady=5)

nombrePanel=Label(miframe, text="Promedio : ")
nombrePanel.grid(row=7, column= 0, padx= 2, pady=5)

nombrePanel=Label(miframe, text="Panel con Menor Voltaje : ")
nombrePanel.grid(row=9, column= 0, padx= 2, pady=5)

#----------------Función que manda un mensaje Broadcast a Xbees---------

def codigoIniciar():
    device.open()
    device.flush_queues()
    DATA_TO_SEND = "Hola XBee!"
    device.send_data_broadcast(DATA_TO_SEND)

#----------------Boton que llama a la función CodigoIniciar--------------

botonEnvio=Button(raiz, text="Iniciar " ,command=codigoIniciar)
botonEnvio.pack()

#----------------Loop de la ventana  --------------

raiz.mainloop()