DynamicDataVisTool Python GUI
👤 Sharing: AI
import tkinter as tk
from tkinter import ttk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import pandas as pd
import numpy as np
import random
import threading
import time
class DynamicDataVisTool:
def __init__(self, master):
self.master = master
master.title("Dynamic Data Visualization Tool")
# --- Data Generation --- #
self.data = self.generate_initial_data()
self.df = pd.DataFrame(self.data)
# --- UI Elements --- #
self.notebook = ttk.Notebook(master)
self.notebook.pack(expand=True, fill="both", padx=10, pady=10)
self.tab_data_view = ttk.Frame(self.notebook)
self.tab_visualization = ttk.Frame(self.notebook)
self.tab_settings = ttk.Frame(self.notebook)
self.notebook.add(self.tab_data_view, text="Data View")
self.notebook.add(self.tab_visualization, text="Visualization")
self.notebook.add(self.tab_settings, text="Settings")
# --- Data View Tab --- #
self.treeview = ttk.Treeview(self.tab_data_view, columns=list(self.df.columns), show="headings")
for col in self.df.columns:
self.treeview.heading(col, text=col)
self.treeview.column(col, width=100)
self.treeview.pack(expand=True, fill="both", padx=10, pady=10)
self.populate_treeview()
# --- Visualization Tab --- #
self.fig, self.ax = plt.subplots(figsize=(6, 4))
self.canvas = FigureCanvasTkAgg(self.fig, master=self.tab_visualization)
self.canvas_widget = self.canvas.get_tk_widget()
self.canvas_widget.pack(expand=True, fill="both", padx=10, pady=10)
self.plot_data()
# --- Settings Tab --- #
self.label_update_interval = tk.Label(self.tab_settings, text="Update Interval (seconds):")
self.label_update_interval.pack(pady=5)
self.update_interval_entry = tk.Entry(self.tab_settings)
self.update_interval_entry.insert(0, "1") # Default interval
self.update_interval_entry.pack(pady=5)
self.button_details = tk.Button(self.tab_settings, text="Details", command=self.show_details)
self.button_details.pack(pady=10)
# --- Start Data Update Thread --- #
self.stop_event = threading.Event()
self.data_thread = threading.Thread(target=self.update_data_loop, daemon=True)
self.data_thread.start()
def generate_initial_data(self):
num_points = 50
return {
'X': np.linspace(0, 10, num_points),
'Y1': np.sin(np.linspace(0, 10, num_points)) + np.random.normal(0, 0.2, num_points),
'Y2': np.cos(np.linspace(0, 10, num_points)) + np.random.normal(0, 0.2, num_points),
'Random': [random.randint(1, 100) for _ in range(num_points)]
}
def update_data(self):
# Simulate new data points
new_x = self.data['X'][-1] + (self.data['X'][1] - self.data['X'][0])
self.data['X'] = np.append(self.data['X'][1:], new_x)
self.data['Y1'] = np.append(self.data['Y1'][1:], np.sin(new_x) + np.random.normal(0, 0.2))
self.data['Y2'] = np.append(self.data['Y2'][1:], np.cos(new_x) + np.random.normal(0, 0.2))
self.data['Random'] = np.append(self.data['Random'][1:], random.randint(1, 100))
self.df = pd.DataFrame(self.data)
def populate_treeview(self):
for item in self.treeview.get_children():
self.treeview.delete(item)
for index, row in self.df.iterrows():
self.treeview.insert("", "end", values=list(row))
def plot_data(self):
self.ax.clear()
self.ax.plot(self.df['X'], self.df['Y1'], label='Y1', marker='o')
self.ax.plot(self.df['X'], self.df['Y2'], label='Y2', marker='x')
self.ax.set_xlabel('X')
self.ax.set_ylabel('Y')
self.ax.set_title('Dynamic Data Plot')
self.ax.legend()
self.fig.tight_layout()
self.canvas.draw()
def update_data_loop(self):
while not self.stop_event.is_set():
try:
interval = float(self.update_interval_entry.get())
except ValueError:
interval = 1.0 # Default if invalid input
self.update_data()
self.populate_treeview()
self.plot_data()
time.sleep(interval)
def show_details(self):
details_window = tk.Toplevel(self.master)
details_window.title("Program Details")
details_text = tk.Text(details_window, wrap=tk.WORD)
details_text.pack(expand=True, fill="both", padx=10, pady=10)
details_text.insert(tk.END, """
Dynamic Data Visualization Tool Details:
This program provides a dynamic visualization of generated data. It features:
- **Data View:** A tabular representation of the data using a Treeview widget.
- **Visualization:** A Matplotlib plot that dynamically updates with new data points.
- **Settings:** An option to control the update interval for the data and visualization.
The program uses threading to update the data in the background, ensuring the GUI remains responsive. The data is simulated using NumPy to create oscillating waveforms with added noise, and random integer data. The pandas library helps organize the data efficiently, and matplotlib provides flexible plotting capabilities.
This tool can be adapted for various real-time data monitoring and visualization applications.
""")
details_text.config(state=tk.DISABLED)
def on_closing(self):
self.stop_event.set()
self.master.destroy()
if __name__ == '__main__':
root = tk.Tk()
tool = DynamicDataVisTool(root)
root.protocol("WM_DELETE_WINDOW", tool.on_closing)
root.mainloop()
👁️ Viewed: 6
Comments