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.add_button = ttk.Button(self.input_frame, text="Dodaj aktywność", command=self.add_activity)
- self.add_button.grid(row=2, column=1, columnspan=2, pady=10)
- self.plot_button = ttk.Button(self, text="Wykreśl CPM", command=self.plot_cpm)
- self.plot_button.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)
- 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