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
- 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)
- critical_path = nx.dag_longest_path(G, weight='duration')
- pos = nx.nx_agraph.graphviz_layout(G, prog='dot')
- plt.figure(figsize=(10, 5))
- nx.draw(G, pos, with_labels=True, node_color='lightblue', node_size=3000, font_size=12)
- nx.draw_networkx_edge_labels(G, pos, edge_labels={(u, v): d["duration"] for u, v, d in G.edges(data=True)}, font_size=12)
- nx.draw_networkx_edges(G, pos, edgelist=[(critical_path[i], critical_path[i + 1]) for i in range(len(critical_path) - 1)], width=3, edge_color='r')
- plt.title("Critical Path Method (CPM)")
- plt.show()
- if __name__ == "__main__":
- app = CPMApp()
- app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement