Advertisement
adwas33

Untitled

Mar 17th, 2023
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import ttk
  3. import networkx as nx
  4. import matplotlib.pyplot as plt
  5. from networkx.drawing.nx_agraph import graphviz_layout
  6. from networkx.algorithms.dag import dag_longest_path, dag_longest_path_length
  7.  
  8.  
  9. class CPMApp(tk.Tk):
  10. def __init__(self):
  11. super().__init__()
  12. self.title("CPM - Critical Path Method")
  13. self.geometry("600x400")
  14.  
  15. self.activities = []
  16. self.create_widgets()
  17.  
  18. def create_widgets(self):
  19. self.input_frame = ttk.Frame(self)
  20. self.input_frame.pack(pady=10)
  21.  
  22. self.activity_label = ttk.Label(self.input_frame, text="Aktywność:")
  23. self.activity_label.grid(row=0, column=0, padx=5)
  24.  
  25. self.activity_entry = ttk.Entry(self.input_frame, width=10)
  26. self.activity_entry.grid(row=0, column=1)
  27.  
  28. self.duration_label = ttk.Label(self.input_frame, text="Czas trwania:")
  29. self.duration_label.grid(row=0, column=2, padx=5)
  30.  
  31. self.duration_entry = ttk.Entry(self.input_frame, width=10)
  32. self.duration_entry.grid(row=0, column=3)
  33.  
  34. self.dependencies_label = ttk.Label(self.input_frame, text="Zależności (rozdzielone przecinkiem):")
  35. self.dependencies_label.grid(row=1, column=0, columnspan=2, pady=10, padx=5)
  36.  
  37. self.dependencies_entry = ttk.Entry(self.input_frame, width=20)
  38. self.dependencies_entry.grid(row=1, column=2, columnspan=2)
  39.  
  40. self.add_button = ttk.Button(self.input_frame, text="Dodaj aktywność", command=self.add_activity)
  41. self.add_button.grid(row=2, column=1, columnspan=2, pady=10)
  42.  
  43. self.plot_button = ttk.Button(self, text="Wykreśl CPM", command=self.plot_cpm)
  44. self.plot_button.pack(pady=10)
  45.  
  46. def add_activity(self):
  47. activity = self.activity_entry.get()
  48. duration = int(self.duration_entry.get())
  49. dependencies = [dep.strip() for dep in self.dependencies_entry.get().split(',') if dep.strip()]
  50.  
  51. self.activities.append((activity, duration, dependencies))
  52.  
  53. self.activity_entry.delete(0, tk.END)
  54. self.duration_entry.delete(0, tk.END)
  55. self.dependencies_entry.delete(0, tk.END)
  56.  
  57. def plot_cpm(self):
  58. G = nx.DiGraph()
  59. for activity, duration, dependencies in self.activities:
  60. G.add_node(activity, duration=duration)
  61. for dep in dependencies:
  62. G.add_edge(dep, activity)
  63.  
  64. pos = nx.spring_layout(G, seed=42)
  65. edge_labels = {(u, v): G.nodes[v]['duration'] for u, v in G.edges()}
  66.  
  67. critical_path = dag_longest_path(G)
  68. critical_path_edges = [(critical_path[i], critical_path[i + 1]) for i in range(len(critical_path) - 1)]
  69. critical_path_length = dag_longest_path_length(G)
  70.  
  71. plt.figure(figsize=(8, 6))
  72. nx.draw(G, pos, with_labels=True, node_size=2000, node_color='lightblue', arrowsize=20, font_size=12)
  73. nx.draw_networkx_edges(G, pos, edgelist=critical_path_edges, edge_color='red', width=2.5)
  74. nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=12)
  75.  
  76. plt.title(f"Critical Path: {' -> '.join(critical_path)}\nLength: {critical_path_length}")
  77. plt.savefig("cpm_plot.png")
  78. plt.show()
  79.  
  80.  
  81. if __name__ == "__main__":
  82. app = CPMApp()
  83. app.mainloop()
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement