Advertisement
adwas33

Untitled

Mar 17th, 2023
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 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.  
  6. class CPMApp(tk.Tk):
  7. def __init__(self):
  8. super().__init__()
  9. self.title("CPM - Critical Path Method")
  10. self.geometry("600x400")
  11.  
  12. self.activities = []
  13. self.create_widgets()
  14.  
  15. def create_widgets(self):
  16. self.input_frame = ttk.Frame(self)
  17. self.input_frame.pack(pady=10)
  18.  
  19. self.activity_label = ttk.Label(self.input_frame, text="Aktywność:")
  20. self.activity_label.grid(row=0, column=0, padx=5)
  21.  
  22. self.activity_entry = ttk.Entry(self.input_frame, width=10)
  23. self.activity_entry.grid(row=0, column=1)
  24.  
  25. self.duration_label = ttk.Label(self.input_frame, text="Czas trwania:")
  26. self.duration_label.grid(row=0, column=2, padx=5)
  27.  
  28. self.duration_entry = ttk.Entry(self.input_frame, width=10)
  29. self.duration_entry.grid(row=0, column=3)
  30.  
  31. self.dependencies_label = ttk.Label(self.input_frame, text="Zależności (rozdzielone przecinkiem):")
  32. self.dependencies_label.grid(row=1, column=0, columnspan=2, pady=10, padx=5)
  33.  
  34. self.dependencies_entry = ttk.Entry(self.input_frame, width=20)
  35. self.dependencies_entry.grid(row=1, column=2, columnspan=2)
  36.  
  37. self.add_button = ttk.Button(self.input_frame, text="Dodaj aktywność", command=self.add_activity)
  38. self.add_button.grid(row=2, column=1, columnspan=2, pady=10)
  39.  
  40. self.plot_button = ttk.Button(self, text="Wykreśl CPM", command=self.plot_cpm)
  41. self.plot_button.pack(pady=10)
  42.  
  43. def add_activity(self):
  44. activity = self.activity_entry.get()
  45. duration = int(self.duration_entry.get())
  46. dependencies = [dep.strip() for dep in self.dependencies_entry.get().split(',') if dep.strip()]
  47.  
  48. self.activities.append((activity, duration, dependencies))
  49.  
  50. self.activity_entry.delete(0, tk.END)
  51. self.duration_entry.delete(0, tk.END)
  52. self.dependencies_entry.delete(0, tk.END)
  53.  
  54. def plot_cpm(self):
  55. G = nx.DiGraph()
  56. for activity, duration, dependencies in self.activities:
  57. G.add_node(activity, duration=duration)
  58. for dep in dependencies:
  59. G.add_edge(dep, activity)
  60.  
  61. critical_path = nx.dag_longest_path(G, weight='duration')
  62. pos = nx.nx_agraph.graphviz_layout(G, prog='dot')
  63.  
  64. plt.figure(figsize=(10, 5))
  65. nx.draw(G, pos, with_labels=True, node_color='lightblue', node_size=3000, font_size=12)
  66. 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)
  67. 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')
  68.  
  69. plt.title("Critical Path Method (CPM)")
  70. plt.show()
  71.  
  72.  
  73. if __name__ == "__main__":
  74. app = CPMApp()
  75. app.mainloop()
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement