adwas33

Untitled

Apr 24th, 2023
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 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.button_frame = ttk.Frame(self)
  41. self.button_frame.pack(pady=5)
  42.  
  43. self.add_button = ttk.Button(self.button_frame, text="Dodaj aktywność", command=self.add_activity)
  44. self.add_button.grid(row=0, column=0, padx=5)
  45.  
  46. self.edit_button = ttk.Button(self.button_frame, text="Edytuj aktywność", command=self.edit_activity)
  47. self.edit_button.grid(row=0, column=1, padx=5)
  48.  
  49. self.plot_button = ttk.Button(self.button_frame, text="Wykreśl CPM", command=self.plot_cpm)
  50. self.plot_button.grid(row=0, column=2, padx=5)
  51.  
  52. self.activity_listbox = ttk.Treeview(self, columns=("Activity", "Duration", "Dependencies"), show='headings')
  53. self.activity_listbox.heading("Activity", text="Aktywność")
  54. self.activity_listbox.heading("Duration", text="Czas trwania")
  55. self.activity_listbox.heading("Dependencies", text="Zależności")
  56. self.activity_listbox.pack(pady=10)
  57.  
  58.  
  59. def add_activity(self):
  60. activity = self.activity_entry.get()
  61. duration = int(self.duration_entry.get())
  62. dependencies = [dep.strip() for dep in self.dependencies_entry.get().split(',') if dep.strip()]
  63.  
  64. self.activities.append((activity, duration, dependencies))
  65.  
  66. self.activity_entry.delete(0, tk.END)
  67. self.duration_entry.delete(0, tk.END)
  68. self.dependencies_entry.delete(0, tk.END)
  69. self.refresh_activity_list()
  70. def edit_activity(self):
  71. selected_item = self.activity_listbox.selection()
  72. if not selected_item:
  73. return
  74.  
  75. index = self.activity_listbox.index(selected_item)
  76. activity, duration, dependencies = self.activities[index]
  77.  
  78. self.activity_entry.delete(0, tk.END)
  79. self.activity_entry.insert(tk.END, activity)
  80.  
  81. self.duration_entry.delete(0, tk.END)
  82. self.duration_entry.insert(tk.END, duration)
  83.  
  84. self.dependencies_entry.delete(0, tk.END)
  85. self.dependencies_entry.insert(tk.END, ', '.join(dependencies))
  86.  
  87. del self.activities[index]
  88. self.refresh_activity_list()
  89.  
  90. def refresh_activity_list(self):
  91. self.activity_listbox.delete(*self.activity_listbox.get_children())
  92.  
  93. for activity, duration, dependencies in self.activities:
  94. self.activity_listbox.insert("", tk.END, values=(activity, duration, ', '.join(dependencies)))
  95.  
  96. def plot_cpm(self):
  97. G = nx.DiGraph()
  98. for activity, duration, dependencies in self.activities:
  99. G.add_node(activity, duration=duration)
  100. for dep in dependencies:
  101. G.add_edge(dep, activity)
  102.  
  103. pos = nx.spring_layout(G, seed=42)
  104. edge_labels = {(u, v): G.nodes[v]['duration'] for u, v in G.edges()}
  105.  
  106. critical_path = dag_longest_path(G)
  107. critical_path_edges = [(critical_path[i], critical_path[i + 1]) for i in range(len(critical_path) - 1)]
  108. critical_path_length = dag_longest_path_length(G)
  109.  
  110. plt.figure(figsize=(8, 6))
  111. nx.draw(G, pos, with_labels=True, node_size=2000, node_color='lightblue', arrowsize=20, font_size=12)
  112. nx.draw_networkx_edges(G, pos, edgelist=critical_path_edges, edge_color='red', width=2.5)
  113. nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=12)
  114.  
  115. plt.title(f"Critical Path: {' -> '.join(critical_path)}\nLength: {critical_path_length}")
  116. plt.savefig("cpm_plot.png")
  117. plt.show()
  118.  
  119.  
  120. if __name__ == "__main__":
  121. app = CPMApp()
  122. app.mainloop()
  123.  
Add Comment
Please, Sign In to add comment