Advertisement
creativesamurai1982

pyGUI_1.0.0

Mar 10th, 2024
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.23 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import ttk
  3. from tkinter import *
  4. import serial.tools.list_ports
  5. import serial
  6.  
  7. # --- Serial Frame Global variables ---
  8. serial_connection = None
  9.  
  10. # --- Transport Frame Global variables ---
  11.  
  12. # --- Defines for Serial Frame
  13. def connect_serial():
  14.     global serial_connection
  15.     try:
  16.         port = port_options.get()
  17.         if not port:
  18.             raise ValueError("No port selected")
  19.         if port not in [port.device for port in serial.tools.list_ports.comports()]:
  20.             raise ValueError(f"Selected port '{port}' is no longer available")
  21.        
  22.         baud_rate = baud_options.get()
  23.         if not baud_rate:
  24.             raise ValueError("No baud rate selected")
  25.        
  26.         serial_connection = serial.Serial(port, baud_rate)
  27.         connect_button.config(state=tk.DISABLED)
  28.         disconnect_button.config(state=tk.NORMAL)
  29.         status_label.config(text=f"Connected to {port} at {baud_rate} baud")
  30.        
  31.         root.geometry("1080x720")  # Set the new width and height of the window
  32.        
  33.     except ValueError as ve:
  34.         status_label.config(text=str(ve))
  35.     except serial.SerialException:
  36.         status_label.config(text="Failed to connect to serial port")
  37.  
  38. def disconnect_serial():
  39.     global serial_connection
  40.     if serial_connection is not None:
  41.         serial_connection.close()
  42.         connect_button.config(state=tk.NORMAL)
  43.         disconnect_button.config(state=tk.DISABLED)
  44.         status_label.config(text="Disconnected")
  45.        
  46.         # Restore the window size
  47.         root.geometry("")  # Reset the window size to its default
  48.  
  49. def update_com_ports():    
  50.     port_list = [port.device for port in serial.tools.list_ports.comports()]
  51.     port_combo['values'] = port_list
  52.     root.after(1000, update_com_ports)  # Schedule this function to run every 1000 milliseconds (1 second)
  53.  
  54.  
  55. # --- Initialize Tkinter ---
  56. root = tk.Tk()
  57. img = PhotoImage(file='icon.png')
  58. root.iconphoto(False, img)
  59. root.title("Serial Monitor")
  60.  
  61. # --- Variables for Serial ---
  62. #Frame Get available ports and baud rates
  63. port_list = [port.device for port in serial.tools.list_ports.comports()]
  64. baud_list = ["110", "300", "600", "1200", "2400", "4800", "9600", "14400", "19200", "38400", "57600", "115200", "128000", "256000"]
  65.  
  66. # Variables for selected options
  67. port_options = tk.StringVar()
  68. baud_options = tk.StringVar()
  69.  
  70. # --- Create Frames ---
  71. left_frame = tk.Frame(root, bg="cyan")
  72. right_frame = tk.Frame(root, bg="pink")
  73. serial_frame = tk.Frame(left_frame, bg="blue")
  74. transport_frame = tk.Frame(left_frame, bg="green")
  75. transport2_frame = tk.Frame(right_frame, bg="yellow")
  76.  
  77. # --- Create Serial Widgets ---
  78. serial_title = tk.Label(serial_frame, text="Serial Settings")  # Set label background color to blue
  79. port_label = tk.Label(serial_frame, text="COM Port:")  # Set label background color to blue
  80. baud_label = tk.Label(serial_frame, text="Baud Rate:")  # Set label background color to blue
  81. port_combo = ttk.Combobox(serial_frame, textvariable=port_options, values=port_list, state="readonly")
  82. baud_combo = ttk.Combobox(serial_frame, textvariable=baud_options, values=baud_list, state="readonly")
  83. connect_button = tk.Button(serial_frame, text="Connect", command=connect_serial)
  84. disconnect_button = tk.Button(serial_frame, text="Disconnect", command=disconnect_serial, state=tk.DISABLED)
  85. status_label = tk.Label(serial_frame, text="")  # Set label background color to blue
  86.  
  87.  
  88. # --- Create Transport Widgets ---
  89. transport_title = tk.Label(transport_frame, text="Transport Controls", bg="green")
  90. start_button = tk.Button(transport_frame, text="Start")
  91. stop_button = tk.Button(transport_frame, text="Stop")
  92.  
  93. # --- Create test Widgets ---
  94. transport2_title = tk.Label(transport2_frame, text="Transport Controls", bg="green")
  95. start2_button = tk.Button(transport2_frame, text="Start")
  96. stop2_button = tk.Button(transport2_frame, text="Stop")
  97.  
  98. # --- Publish Grid layout ---
  99. left_frame.grid(row=0, column=0, padx=10, pady=10)
  100. right_frame.grid(row=0, column=1, padx=10, pady=10)
  101. serial_frame.grid(row=0, column=0, padx=10, pady=10) # Grid the serial frame
  102. transport_frame.grid(row=1, column=0, padx=10, pady=10)
  103. transport2_frame.grid(row=1, column=0, padx=10, pady=10)
  104.  
  105. # --- Publish Serial Widgets ---
  106. serial_title.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
  107. port_label.grid(row=1, column=0, padx=10, pady=5)
  108. baud_label.grid(row=2, column=0, padx=10, pady=5)
  109. port_combo.grid(row=1, column=1, padx=10, pady=5)
  110. baud_combo.grid(row=2, column=1, padx=10, pady=5)
  111. connect_button.grid(row=1, column=2, padx=10, pady=5)
  112. disconnect_button.grid(row=2, column=2, padx=10, pady=5)
  113. status_label.grid(row=3, column=0, columnspan=3, padx=10, pady=10)
  114.  
  115. # --- Publish Transport Widgets ---
  116. transport_title.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
  117. start_button.grid(row=1, column=1, padx=10, pady=5)
  118. stop_button.grid(row=2, column=1, padx=10, pady=5)
  119.  
  120. # --- Publish Test Widgets ---
  121. transport2_title.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
  122. start2_button.grid(row=1, column=1, padx=10, pady=5)
  123. stop2_button.grid(row=2, column=1, padx=10, pady=5)
  124.  
  125. update_com_ports()  # Starts the function to update COM ports periodically
  126.  
  127. # Start Tkinter event loop
  128. root.mainloop()
  129.  
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement