Nur für die Vollständigkeit und damit man auf einem aktuellen Stand ist, möchte ich hier ein letztes Mal den vollständigen Code posten, weil ich doch einiges hier und da verändert habe. Danach poste ich nur noch Code-Schnipsel, wo sich etwas verändert hat.
Code: Alles auswählen
# MODULES
import tkinter as tk
import tkinter.filedialog as fd
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure
# PROGRAM
class MainApplication(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.parent = parent
canvas = tk.Canvas(parent, height=768, width=1024)
canvas.pack()
self.list_csv = []
# Buttons
btn_add = tk.Button(parent, text="Add", height=1, width=6, padx=2, pady=2, fg="#22252D", bg="#F1F1EB",
command=self.open_csv)
btn_add.place(relx=0.025, rely=0.025)
btn_delete = tk.Button(parent, text="Delete", height=1, width=6, padx=2, pady=2, fg="#22252D", bg="#F1F1EB",
command=self.delete_csv)
btn_delete.place(relx=0.1, rely=0.025)
# Listbox
self.frame_listbox = tk.Frame(parent, bg="#DFDFDF")
self.frame_listbox.place(relwidth=0.2, relheight=0.9, relx=0.025, rely=0.075)
self.data_listbox = self.create_list()
self.data_listbox.bind("<Double-1>", self.plot_csv)
def create_list(self):
listbox = tk.Listbox(self.frame_listbox, bg="#DFDFDF", width=25, height=17)
scrollbar_y = tk.Scrollbar(self.frame_listbox, orient="vertical")
scrollbar_x = tk.Scrollbar(self.frame_listbox, orient="horizontal")
listbox.config(yscrollcommand = scrollbar_y.set, xscrollcommand = scrollbar_x.set)
scrollbar_y.config(command = listbox.yview)
scrollbar_x.config(command = listbox.xview)
scrollbar_x.pack(side="bottom", fill="x")
listbox.pack(side="left", fill="both")
scrollbar_y.pack(side="right", fill="y")
return listbox
def open_csv(self):
name = fd.askopenfilenames(parent=self.parent, filetypes=(("csv files", "*.csv"), ("all files", "*.*")))
for i in name:
if i not in self.list_csv:
self.list_csv.append(i)
file_parts = i.split("/")
self.data_listbox.insert(tk.END, file_parts[-1])
return name
def delete_csv(self):
item = self.data_listbox.curselection()
self.list_csv.remove(self.list_csv[item[0]])
self.data_listbox.delete(tk.ANCHOR)
return list_csv
def plot_csv(self, event):
id = self.data_listbox.curselection()
df = pd.read_csv(self.list_csv[id[0]], sep=";", header=0, skipfooter=0, engine="python")
Plotting(root=root, data=df)
class Plotting:
def __init__(self, root, data):
self.color_background = "#DFDFDF"
self.root = root
self.data = data
self.frame_main = tk.Frame(self.root, bg=self.color_background)
self.frame_main.place(relwidth=0.775, relheight=0.9, relx=0.225, rely=0.075)
self.frame_main_check = tk.Frame(self.frame_main, bg=self.color_background)
self.frame_main_check.place(relwidth=0.1, relheight=0.9, relx=0.875, rely=0.1)
self.frame_main_plot = tk.Frame(self.frame_main, bg=self.color_background)
self.frame_main_plot.place(relwidth=0.875, relheight=1.0, relx=0.0, rely=0.0)
self.names = list(self.data.columns.values)
self.names.pop(0)
self._states = {name: tk.BooleanVar(name=name) for name in self.names}
btn_select_all = tk.Button(self.frame_main, text="Select all", height=1, width=12, padx=1, pady=1,
fg="#22252D", bg="#F1F1EB",
command= lambda: self.cb_select_all(states=self._states))
btn_select_all.place(relx=0.875, rely=0.0)
btn_deselect_all = tk.Button(self.frame_main, text="Deselect all", height=1, width=12, padx=1, pady=1,
fg="#22252D", bg="#F1F1EB",
command= lambda: self.cb_deselect_all(states=self._states))
btn_deselect_all.place(relx=0.875, rely=0.04)
for name, state in self._states.items():
state.set(True)
cb = tk.Checkbutton(master=self.frame_main_check, variable=state, text=name,
height=1, width=15, anchor=tk.W, bg=self.color_background)
cb.pack()
print(name, state.get())
state.trace_add("write", self._value_changed)
self.fig = Figure(figsize=(1,2), facecolor="#FFFFFF")
self.ax = self.fig.add_subplot()
Plotting.show_plot(self, data=self.data, fig=self.fig, ax=self.ax, states=self._states)
def _value_changed(self, name, _, op):
print(name, self._states[name].get())
def cb_select_all(self, states):
for state in states.values():
state.set(True)
state.trace_add("write", self._value_changed)
#
def cb_deselect_all(self, states):
for state in states.values():
state.set(False)
state.trace_add("write", self._value_changed)
def show_plot(self, data, fig, ax, states):
data_plot = data.to_numpy()
times = data_plot[:, 0]
i = 1
for name, state in states.items():
if state.get():
ax.plot(times, data_plot[:, i], label=name, visible=True)
i += 1
else:
continue
ax.grid(True)
ax.set_axisbelow(True)
ax.set_xlabel("x", labelpad=0.5)
ax.set_ylabel("f(x)", labelpad=0.5)
ax.legend(fontsize="x-small", framealpha=1.0, bbox_to_anchor=(0.125, 0.02), loc=3, borderaxespad=0,
bbox_transform=plt.gcf().transFigure, ncol=4)
fig.tight_layout()
#
self.canvas = FigureCanvasTkAgg(fig, master=self.frame_main_plot)
self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
toolbar = NavigationToolbar2Tk(self.canvas, self.frame_main_plot)
toolbar.config(background=self.color_background)
toolbar._message_label.config(background=self.color_background)
toolbar.update()
self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
# EXECUTION
if __name__ == "__main__":
root = tk.Tk()
root.title("Tkinter - Plotting csv files")
MainApplication(root).pack(side="top", fill="both", expand=True)
root.mainloop()