Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import ttk
- import networkx as nx
- import matplotlib.pyplot as plt
- from networkx.drawing.nx_agraph import graphviz_layout
- from networkx.algorithms.dag import dag_longest_path, dag_longest_path_length
- class CPMApp(tk.Tk):
- def __init__(self):
- super().__init__()
- self.title("CPM - Critical Path Method")
- self.geometry("600x400")
- self.activities = []
- self.create_widgets()
- def create_widgets(self):
- self.input_frame = ttk.Frame(self)
- self.input_frame.pack(pady=10)
- self.activity_label = ttk.Label(self.input_frame, text="Aktywność:")
- self.activity_label.grid(row=0, column=0, padx=5)
- self.activity_entry = ttk.Entry(self.input_frame, width=10)
- self.activity_entry.grid(row=0, column=1)
- self.duration_label = ttk.Label(self.input_frame, text="Czas trwania:")
- self.duration_label.grid(row=0, column=2, padx=5)
- self.duration_entry = ttk.Entry(self.input_frame, width=10)
- self.duration_entry.grid(row=0, column=3)
- self.dependencies_label = ttk.Label(self.input_frame, text="Zależności (rozdzielone przecinkiem):")
- self.dependencies_label.grid(row=1, column=0, columnspan=2, pady=10, padx=5)
- self.dependencies_entry = ttk.Entry(self.input_frame, width=20)
- self.dependencies_entry.grid(row=1, column=2, columnspan=2)
- self.button_frame = ttk.Frame(self)
- self.button_frame.pack(pady=5)
- self.add_button = ttk.Button(self.button_frame, text="Dodaj aktywność", command=self.add_activity)
- self.add_button.grid(row=0, column=0, padx=5)
- self.edit_button = ttk.Button(self.button_frame, text="Edytuj aktywność", command=self.edit_activity)
- self.edit_button.grid(row=0, column=1, padx=5)
- self.plot_button = ttk.Button(self.button_frame, text="Wykreśl CPM", command=self.plot_cpm)
- self.plot_button.grid(row=0, column=2, padx=5)
- self.activity_listbox = ttk.Treeview(self, columns=("Activity", "Duration", "Dependencies"), show='headings')
- self.activity_listbox.heading("Activity", text="Aktywność")
- self.activity_listbox.heading("Duration", text="Czas trwania")
- self.activity_listbox.heading("Dependencies", text="Zależności")
- self.activity_listbox.pack(pady=10)
- def add_activity(self):
- activity = self.activity_entry.get()
- duration = int(self.duration_entry.get())
- dependencies = [dep.strip() for dep in self.dependencies_entry.get().split(',') if dep.strip()]
- self.activities.append((activity, duration, dependencies))
- self.activity_entry.delete(0, tk.END)
- self.duration_entry.delete(0, tk.END)
- self.dependencies_entry.delete(0, tk.END)
- self.refresh_activity_list()
- def edit_activity(self):
- selected_item = self.activity_listbox.selection()
- if not selected_item:
- return
- index = self.activity_listbox.index(selected_item)
- activity, duration, dependencies = self.activities[index]
- self.activity_entry.delete(0, tk.END)
- self.activity_entry.insert(tk.END, activity)
- self.duration_entry.delete(0, tk.END)
- self.duration_entry.insert(tk.END, duration)
- self.dependencies_entry.delete(0, tk.END)
- self.dependencies_entry.insert(tk.END, ', '.join(dependencies))
- del self.activities[index]
- self.refresh_activity_list()
- def refresh_activity_list(self):
- self.activity_listbox.delete(*self.activity_listbox.get_children())
- for activity, duration, dependencies in self.activities:
- self.activity_listbox.insert("", tk.END, values=(activity, duration, ', '.join(dependencies)))
- def plot_cpm(self):
- G = nx.DiGraph()
- for activity, duration, dependencies in self.activities:
- G.add_node(activity, duration=duration)
- for dep in dependencies:
- G.add_edge(dep, activity)
- pos = nx.spring_layout(G, seed=42)
- edge_labels = {(u, v): G.nodes[v]['duration'] for u, v in G.edges()}
- critical_path = dag_longest_path(G)
- critical_path_edges = [(critical_path[i], critical_path[i + 1]) for i in range(len(critical_path) - 1)]
- critical_path_length = dag_longest_path_length(G)
- plt.figure(figsize=(8, 6))
- nx.draw(G, pos, with_labels=True, node_size=2000, node_color='lightblue', arrowsize=20, font_size=12)
- nx.draw_networkx_edges(G, pos, edgelist=critical_path_edges, edge_color='red', width=2.5)
- nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=12)
- plt.title(f"Critical Path: {' -> '.join(critical_path)}\nLength: {critical_path_length}")
- plt.savefig("cpm_plot.png")
- plt.show()
- if __name__ == "__main__":
- app = CPMApp()
- app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement