1
+ import tkinter as tk
2
+ from tkinter import filedialog , messagebox , ttk
3
+ import pandas as pd
4
+ import plotly .graph_objects as go
5
+ from plotly .subplots import make_subplots
6
+
7
+ class CSVPlotterApp :
8
+ def __init__ (self , root ):
9
+ self .root = root
10
+ self .root .title ("CSV Plotter GUI" )
11
+ self .filename = None
12
+ self .data = None
13
+ self .create_widgets ()
14
+
15
+ def create_widgets (self ):
16
+ # Create a frame for buttons and file name display
17
+ self .control_frame = tk .Frame (self .root , padx = 10 , pady = 10 )
18
+ self .control_frame .pack (pady = 10 )
19
+
20
+ # Button to load CSV file
21
+ self .load_button = tk .Button (self .control_frame , text = "Load CSV" , command = self .load_csv , bg = '#4CAF50' , fg = 'white' , font = ('Arial' , 12 ))
22
+ self .load_button .pack (side = tk .LEFT , padx = 5 )
23
+
24
+ # Label to display the name of the selected CSV file
25
+ self .file_label = tk .Label (self .control_frame , text = "No file selected" , font = ('Arial' , 12 ))
26
+ self .file_label .pack (side = tk .LEFT , padx = 5 )
27
+
28
+ # Frame for dropdown menu
29
+ self .dropdown_frame = tk .Frame (self .root , padx = 10 , pady = 10 )
30
+ self .dropdown_frame .pack (pady = 10 )
31
+
32
+ self .dropdown_label = tk .Label (self .dropdown_frame , text = "Select Channel to Plot:" , font = ('Arial' , 12 ))
33
+ self .dropdown_label .pack (side = tk .LEFT )
34
+
35
+ # Dropdown menu for channel selection
36
+ self .channel_selection = tk .StringVar ()
37
+ self .dropdown_menu = ttk .Combobox (self .dropdown_frame , textvariable = self .channel_selection , state = "normal" , font = ('Arial' , 12 ))
38
+ self .dropdown_menu .pack (side = tk .LEFT , padx = 5 , fill = tk .X , expand = True )
39
+
40
+ # Button to plot data
41
+ self .plot_button = tk .Button (self .root , text = "Plot Data" , command = self .plot_data , bg = '#2196F3' , fg = 'white' , font = ('Arial' , 12 ))
42
+ self .plot_button .pack (pady = 10 )
43
+
44
+ def load_csv (self ):
45
+ # Open file dialog to select a CSV file
46
+ self .filename = filedialog .askopenfilename (filetypes = [("CSV files" , "*.csv" )])
47
+ if self .filename :
48
+ try :
49
+ self .data = pd .read_csv (self .filename ) # Load the CSV file
50
+ self .setup_dropdown_menu ()
51
+ self .file_label .config (text = f"File: { self .filename .split ('/' )[- 1 ]} " )
52
+ except Exception as e :
53
+ messagebox .showerror ("Error" , f"Could not load CSV file: { e } " )
54
+
55
+ def setup_dropdown_menu (self ):
56
+ # Populate dropdown menu with the CSV column names (channels)
57
+ columns = list (self .data .columns )
58
+ self .dropdown_menu ['values' ] = columns
59
+ if columns :
60
+ self .channel_selection .set (columns [0 ]) # Default selection
61
+
62
+ def plot_data (self ):
63
+ selected_channel = self .channel_selection .get () # Get the selected channel
64
+ if not selected_channel :
65
+ messagebox .showerror ("Error" , "No channel selected for plotting" )
66
+ return
67
+
68
+ fig = go .Figure () # Plot the selected channel using Plotly
69
+ fig .add_trace (go .Scatter (x = self .data .index , y = self .data [selected_channel ], mode = 'lines' , name = selected_channel ))
70
+
71
+ fig .update_layout (
72
+ title = f"Channel: { selected_channel } " ,
73
+ xaxis_title = "Index" ,
74
+ yaxis_title = "Value" ,
75
+ template = "plotly_white"
76
+ )
77
+ fig .show ()
78
+
79
+ if __name__ == "__main__" :
80
+ root = tk .Tk ()
81
+ app = CSVPlotterApp (root )
82
+ root .mainloop ()
0 commit comments