Advertisement
creativesamurai1982

pyGUI

Mar 8th, 2024
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.90 KB | None | 0 0
  1. '''
  2. Changes made:
  3.  
  4. Function and variable names are now in snake_case for
  5. consistency and readability.
  6.  
  7. Error handling in the connection function now updates
  8. a status label in the GUI interface to provide user feedback.
  9.  
  10. Added a status label to display the current connection status.
  11.  
  12. Changed the window title to "Serial Monitor" for clarity.
  13.  
  14. Improved layout by providing spacing and organization.
  15.  
  16. These changes should enhance the usability and readability
  17. of your code. If you have any further questions or
  18. need additional assistance, please feel free to ask!
  19. '''
  20.  
  21. import tkinter as tk
  22. from tkinter import ttk
  23. import serial.tools.list_ports
  24. import serial
  25.  
  26. # Global variables
  27. serial_connection = None
  28.  
  29. def connect_serial():
  30.     global serial_connection
  31.     try:
  32.         port = port_options.get()
  33.         if not port:
  34.             raise ValueError("No port selected")
  35.         baud_rate = baud_options.get()
  36.         serial_connection = serial.Serial(port, baud_rate)
  37.         connect_button.config(state=tk.DISABLED)
  38.         disconnect_button.config(state=tk.NORMAL)
  39.         status_label.config(text="Connected to " + port)
  40.     except ValueError as ve:
  41.         status_label.config(text=str(ve))
  42.     except serial.SerialException:
  43.         status_label.config(text="Failed to connect to serial port")
  44.  
  45. def disconnect_serial():
  46.     global serial_connection
  47.     if serial_connection is not None:
  48.         serial_connection.close()
  49.         connect_button.config(state=tk.NORMAL)
  50.         disconnect_button.config(state=tk.DISABLED)
  51.         status_label.config(text="Disconnected")
  52.  
  53. # Initialize Tkinter
  54. root = tk.Tk()
  55. root.title("Serial Monitor")
  56.  
  57. # Get available ports and baud rates
  58. port_list = [port.device for port in serial.tools.list_ports.comports()]
  59. baud_list = ["110", "300", "600", "1200", "2400", "4800", "9600", "14400", "19200", "38400", "57600", "115200", "128000", "256000"]
  60.  
  61. # Variables for selected options
  62. port_options = tk.StringVar()
  63. baud_options = tk.StringVar()
  64.  
  65. # Serial Connection Panel
  66. serial_title = tk.Label(root, text="Serial Settings")
  67.  
  68. port_label = tk.Label(root, text="COM Port:")
  69. baud_label = tk.Label(root, text="Baud Rate:")
  70. port_combo = ttk.Combobox(root, textvariable=port_options, values=port_list)
  71. baud_combo = ttk.Combobox(root, textvariable=baud_options, values=baud_list)
  72. connect_button = tk.Button(root, text="Connect", command=connect_serial)
  73. disconnect_button = tk.Button(root, text="Disconnect", command=disconnect_serial, state=tk.DISABLED)
  74. status_label = tk.Label(root, text="")
  75.  
  76. # Grid layout
  77. serial_title.grid(row=0, column=0, columnspan=3)
  78. port_label.grid(row=1, column=0)
  79. baud_label.grid(row=2, column=0)
  80. port_combo.grid(row=1, column=1)
  81. baud_combo.grid(row=2, column=1)
  82. connect_button.grid(row=1, column=2)
  83. disconnect_button.grid(row=2, column=2)
  84. status_label.grid(row=3, column=0, columnspan=3)
  85.  
  86. # Start Tkinter event loop
  87. root.mainloop()
  88.  
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement