Skip to content
Toggle Navigation
Privacy Policy
Terms of Use
Toggle Navigation
Home
Products
LMPSH-IP
OnAir Light
HDZN-02
HRNL-25W
Protocols
TSL
Contact
Search for:
TSL 3.1 / Python Efficient Code Example
admin
2025-07-27T15:03:18+00:00
Copy to Clipboard
Syntax Highlighter
#!/usr/bin/env python3 from socket import AF_INET, socket, SOCK_STREAM import tkinter as tk from threading import Thread import binascii defaultWindowName = 'TSL3.1 Client' color_Background = '#4d4d4d' color_Black = '#000000' color_White = '#ffffff' color_Red = '#FF0000' color_Green = '#00FF00' color_Amber = '#FFFF00' color_Blue = '#0000FF' off = 0 red = 1 green = 2 amber = 4 blue = 8 client_socket = None # Initialize as None def set_black(): isRotate.set(0) send(off) btn_RED.configure(bg=color_Background, fg=color_White) btn_GREEN.configure(bg=color_Background, fg=color_White) btn_AMBER.configure(bg=color_Background, fg=color_White) btn_BLUE.configure(bg=color_Background, fg=color_White) def set_red(): send(red) btn_RED.configure(bg=color_Red, fg=color_White) btn_GREEN.configure(bg=color_Background, fg=color_White) btn_AMBER.configure(bg=color_Background, fg=color_White) btn_BLUE.configure(bg=color_Background, fg=color_White) def set_green(): send(green) btn_RED.configure(bg=color_Background, fg=color_White) btn_GREEN.configure(bg=color_Green, fg=color_Black) btn_AMBER.configure(bg=color_Background, fg=color_White) btn_BLUE.configure(bg=color_Background, fg=color_White) def set_amber(): send(amber) btn_RED.configure(bg=color_Background, fg=color_White) btn_GREEN.configure(bg=color_Background, fg=color_White) btn_AMBER.configure(bg=color_Amber, fg=color_Black) btn_BLUE.configure(bg=color_Background, fg=color_White) def set_blue(): send(blue) btn_RED.configure(bg=color_Background, fg=color_White) btn_GREEN.configure(bg=color_Background, fg=color_White) btn_AMBER.configure(bg=color_Background, fg=color_White) btn_BLUE.configure(bg=color_Blue, fg=color_White) def manage_connection(): if connection_status.get() == 0: socket_thread = Thread(target=open_connection) socket_thread.start() elif connection_status.get() == 1: close_connection() def open_connection(): global client_socket host = str(textBox_IP.get()) if host: try: port = int(textBox_PORT.get()) addr = (host, port) client_socket = socket(AF_INET, SOCK_STREAM) client_socket.settimeout(5) client_socket.connect(addr) when_connected() sent_message.set('') connection_status.set(1) except Exception as ex: print(ex) textBox_IP.configure(bg=color_Red) textBox_PORT.configure(bg=color_Red) sent_message.set('') connection_status.set(0) when_disconnected() def close_connection(): global client_socket set_black() try: if client_socket: client_socket.close() except: pass client_socket = None when_disconnected() sent_message.set('') connection_status.set(0) def send(color): global client_socket header = 128 brightness = brightness_value.get() try: if not textBox_DisplayID.get(): textBox_DisplayID.insert(0, 0) index = max(0, min(255, int(textBox_DisplayID.get()))) umd = str(textBox_UMD.get()) label_to_send = ("{:<16}".format(umd))[:16] message = bytearray([header + index, min(255, brightness + color)]) + label_to_send.encode() hex_message = (binascii.hexlify(message)).decode() sent_message.set('TSL: ' + hex_message) textBox_DisplayID.configure(bg=color_Black) if client_socket: client_socket.send(message) else: raise ConnectionError("Socket not connected") except Exception as ex: print(ex) textBox_DisplayID.configure(bg=color_Red) close_connection() def when_connected(): textBox_IP.configure(state=tk.DISABLED, disabledbackground=color_Background) textBox_PORT.configure(state=tk.DISABLED, disabledbackground=color_Background) label_connect.set('Disconnect') btn_OFF.configure(state=tk.NORMAL) btn_RED.configure(state=tk.NORMAL) btn_GREEN.configure(state=tk.NORMAL) btn_AMBER.configure(state=tk.NORMAL) btn_BLUE.configure(state=tk.NORMAL) def when_disconnected(): textBox_IP.configure(state=tk.NORMAL) textBox_PORT.configure(state=tk.NORMAL) label_connect.set('Connect') btn_OFF.configure(state=tk.DISABLED) btn_RED.configure(state=tk.DISABLED) btn_GREEN.configure(state=tk.DISABLED) btn_AMBER.configure(state=tk.DISABLED) btn_BLUE.configure(state=tk.DISABLED) main_window = tk.Tk() main_window.geometry('280x320') main_window.resizable(False, False) main_window.title(defaultWindowName) main_window['background'] = color_Background main_window.eval('tk::PlaceWindow . center') # Center the window brightness_value = tk.IntVar() connection_status = tk.IntVar() label_connect = tk.StringVar() sent_message = tk.StringVar() isRotate = tk.IntVar() # Insert default values textBox_IP = tk.Entry(main_window, width=15, bg=color_Black, fg=color_White) textBox_IP.grid(row=3, column=2, columnspan=4, padx=1, pady=1, sticky=tk.EW) textBox_IP.insert(0, '127.0.0.1') textBox_PORT = tk.Entry(main_window, width=15, bg=color_Black, fg=color_White) textBox_PORT.grid(row=4, column=2, columnspan=4, padx=1, pady=1, sticky=tk.EW) textBox_PORT.insert(0, '8900') textBox_DisplayID = tk.Entry(main_window, width=15, bg=color_Black, fg=color_White) textBox_DisplayID.grid(row=5, column=2, columnspan=4, padx=1, pady=1, sticky=tk.EW) textBox_DisplayID.insert(0, '0') textBox_UMD = tk.Entry(main_window, width=15, bg=color_Black, fg=color_White) textBox_UMD.grid(row=6, column=2, columnspan=4, padx=1, pady=1, sticky=tk.EW) # GUI layout components tk.Frame(main_window, bg=color_Background, height=7).grid(row=0, column=0, columnspan=7, sticky='nsew') tk.Frame(main_window, bg=color_Background, height=7).grid(row=2, column=0, columnspan=7, sticky='nsew') tk.Frame(main_window, bg=color_Background, height=7).grid(row=15, column=0, columnspan=8, sticky='nsew') tk.Frame(main_window, bg=color_Background, width=14).grid(row=0, column=0, rowspan=15, sticky='nsew') tk.Frame(main_window, bg=color_Background, width=14).grid(row=0, column=7, rowspan=15, sticky='nsew') tk.Label(main_window, text='TCP/IP: ', width=15, bg=color_Background, fg=color_White, anchor=tk.E).grid(row=3, column=1, padx=2, sticky=tk.EW) tk.Label(main_window, text='TCP/PORT: ', width=15, bg=color_Background, fg=color_White, anchor=tk.E).grid(row=4, column=1, padx=2, sticky=tk.EW) tk.Label(main_window, text='Display ID: ', width=15, bg=color_Background, fg=color_White, anchor=tk.E).grid(row=5, column=1, padx=2, sticky=tk.EW) tk.Label(main_window, text='Label: ', width=15, bg=color_Background, fg=color_White, anchor=tk.E).grid(row=6, column=1, padx=2, sticky=tk.EW) tk.Label(main_window, text='Brightness: ', width=15, bg=color_Background, fg=color_White, anchor=tk.E).grid(row=7, column=1, padx=2, sticky=tk.EW) for i, val in enumerate([0, 16, 32, 48]): tk.Radiobutton( main_window, text=str(i), variable=brightness_value, value=val, bg=color_Background, fg=color_White, activebackground=color_Background, activeforeground=color_White, selectcolor=color_Black ).grid(row=7, column=2 + i, sticky=tk.EW) btn_connect = tk.Button(main_window, textvariable=label_connect, width=14, bg=color_Background, fg=color_White, anchor=tk.CENTER, command=manage_connection) btn_connect.grid(row=8, column=1, columnspan=5, sticky=tk.EW) btn_OFF = tk.Button(main_window, text='Off', width=15, bg=color_Background, fg=color_White, anchor=tk.CENTER, command=set_black) btn_OFF.grid(row=9, column=1, columnspan=5, pady=1, sticky=tk.EW) btn_RED = tk.Button(main_window, text='Red', width=15, bg=color_Background, fg=color_White, anchor=tk.CENTER, command=set_red) btn_RED.grid(row=10, column=1, columnspan=5, pady=1, sticky=tk.EW) btn_GREEN = tk.Button(main_window, text='Green', width=15, bg=color_Background, fg=color_White, anchor=tk.CENTER, command=set_green) btn_GREEN.grid(row=11, column=1, columnspan=5, pady=1, sticky=tk.EW) btn_AMBER = tk.Button(main_window, text='Amber', width=15, bg=color_Background, fg=color_White, anchor=tk.CENTER, command=set_amber) btn_AMBER.grid(row=12, column=1, columnspan=5, pady=1, sticky=tk.EW) btn_BLUE = tk.Button(main_window, text='Blue', width=15, bg=color_Background, fg=color_White, anchor=tk.CENTER, command=set_blue) btn_BLUE.grid(row=13, column=1, columnspan=5, pady=1, sticky=tk.EW) tk.Label(main_window, textvariable=sent_message, width=10, bg=color_Background, fg=color_White, anchor=tk.W).grid(row=14, column=1, columnspan=6, sticky=tk.EW) # Final setup brightness_value.set(48) sent_message.set('') when_disconnected() main_window.mainloop()
Page load link
Go to Top