|
| 1 | +import os |
1 | 2 | import tkinter as tk
|
2 | 3 | from tkinter import filedialog
|
| 4 | +from openness import TIAProject |
| 5 | +from xmlHandler import extract_info |
3 | 6 |
|
4 |
| -class FileParameterUI: |
| 7 | +class ProjectGeneratorUI: |
5 | 8 | def __init__(self, master):
|
6 | 9 | self.master = master
|
7 | 10 | self.master.iconbitmap('.\\cora.ico')
|
8 | 11 | self.master.title("TIA Project Generator")
|
9 | 12 |
|
| 13 | + self.tia_project = TIAProject() |
| 14 | + |
10 | 15 | self.xml_path = ""
|
11 | 16 | self.project_path = ""
|
12 | 17 | self.project_name = ""
|
| 18 | + self.devices = [] |
13 | 19 |
|
14 |
| - self.file_label = tk.Label(master, text="Tia Selection Tool:") |
| 20 | + self.file_label = tk.Label(master, text="1. Choose the HW file (TIA Selection Tool|*.tia):") |
15 | 21 | self.file_label.grid(row=0, column=0, sticky="w", padx=10, pady=5)
|
16 | 22 |
|
17 |
| - self.file_label = tk.Label(master, text="Select File (.tia):") |
18 |
| - self.file_label.grid(row=1, column=0, sticky="w", padx=10, pady=5) |
19 |
| - |
20 | 23 | self.file_entry = tk.Entry(master, width=50)
|
21 |
| - self.file_entry.grid(row=1, column=1, padx=10, pady=5) |
| 24 | + self.file_entry.grid(row=0, column=1, padx=10, pady=5) |
| 25 | + |
| 26 | + self.browse_button = tk.Button(master, text="Search", command=self.browse_xml) |
| 27 | + self.browse_button.grid(row=0, column=2, padx=10, pady=5) |
| 28 | + |
| 29 | + self.project_label = tk.Label(master, text="2. Choose how to save the project:") |
| 30 | + self.project_label.grid(row=1, column=0, sticky="w", padx=10, pady=5) |
22 | 31 |
|
23 |
| - self.browse_button = tk.Button(master, text="Browse", command=self.browse_file) |
24 |
| - self.browse_button.grid(row=1, column=2, padx=10, pady=5) |
| 32 | + self.project_entry = tk.Entry(master, width=50) |
| 33 | + self.project_entry.grid(row=1, column=1, padx=10, pady=5) |
25 | 34 |
|
26 |
| - self.parameter_label = tk.Label(master, text="Enter Parameter:") |
27 |
| - self.parameter_label.grid(row=2, column=0, sticky="w", padx=10, pady=5) |
| 35 | + self.browse_project_button = tk.Button(master, text="Search", command=self.browse_project) |
| 36 | + self.browse_project_button.grid(row=1, column=2, padx=10, pady=5) |
28 | 37 |
|
29 |
| - self.parameter_entry = tk.Entry(master, width=50) |
30 |
| - self.parameter_entry.grid(row=2, column=1, padx=10, pady=5) |
| 38 | + self.create_button = tk.Button(master, text="Create Project", command=self.create_project) |
| 39 | + self.create_button.grid(row=3, columnspan=3, padx=10, pady=10) |
31 | 40 |
|
32 |
| - self.submit_button = tk.Button(master, text="Submit", command=self.submit) |
33 |
| - self.submit_button.grid(row=3, columnspan=3, padx=10, pady=10) |
| 41 | + self.output_label = tk.Label(master, text="Progress:") |
| 42 | + self.output_label.grid(row=4, column=0, sticky="w", padx=10, pady=5) |
34 | 43 |
|
35 |
| - def browse_file(self): |
36 |
| - file_path = filedialog.askopenfilename() |
| 44 | + self.output_text = tk.Text(master, width=50, height=10) |
| 45 | + self.output_text.grid(row=5, columnspan=3, padx=10, pady=5) |
| 46 | + |
| 47 | + def browse_xml(self): |
| 48 | + file_path = filedialog.askopenfilename(filetypes=[("TIA Selection Tools Files", "*.tia")]) |
| 49 | + self.xml_path = file_path |
| 50 | + self.devices = extract_info(self.xml_path) |
37 | 51 | self.file_entry.delete(0, tk.END)
|
38 |
| - self.file_entry.insert(0, file_path) |
| 52 | + self.file_entry.insert(0, self.xml_path) |
| 53 | + |
| 54 | + def browse_project(self): |
| 55 | + project_path = filedialog.asksaveasfilename() |
| 56 | + self.project_path = os.path.dirname(project_path) |
| 57 | + self.project_name = os.path.basename(project_path) |
| 58 | + self.project_entry.delete(0, tk.END) |
| 59 | + self.project_entry.insert(0, project_path) |
| 60 | + |
| 61 | + def create_project(self): |
| 62 | + self.output_text.delete(1.0, tk.END) # Clear previous output |
| 63 | + if not self.xml_path or not self.project_path or not self.project_name: |
| 64 | + self.output_text.insert(tk.END, "Please, fill in all fields.\n") |
| 65 | + return |
| 66 | + |
| 67 | + #start tia |
| 68 | + self.output_text.delete(1.0, tk.END) |
| 69 | + self.output_text.insert(tk.END, "Inicializing TIA Portal without UI...\n") |
| 70 | + self.output_text.see(tk.END) |
| 71 | + self.master.update() |
| 72 | + try: |
| 73 | + self.tia_project.startTIA(False) |
| 74 | + except Exception as e: |
| 75 | + self.output_text.insert(tk.END, f"❌ Error to open TIA: {e}\n") |
| 76 | + self.output_text.see(tk.END) |
| 77 | + return |
| 78 | + else: |
| 79 | + self.output_text.insert(tk.END, f"✔ TIA started successfully\n") |
| 80 | + self.output_text.see(tk.END) |
| 81 | + self.master.update() |
39 | 82 |
|
40 |
| - def submit(self): |
41 |
| - file_path = self.file_entry.get() |
42 |
| - parameter = self.parameter_entry.get() |
43 |
| - # Do something with the file and parameter, e.g., process the file using the parameter |
44 |
| - print("File:", file_path) |
45 |
| - print("Parameter:", parameter) |
| 83 | + #create project |
| 84 | + self.output_text.insert(tk.END, f"Creating the project {self.project_name}...\n") |
| 85 | + self.output_text.see(tk.END) |
| 86 | + self.master.update() |
| 87 | + try: |
| 88 | + self.tia_project.create_project(self.project_path, self.project_name) |
| 89 | + except Exception as e: |
| 90 | + self.output_text.insert(tk.END, f"❌ Error to create the project: {e}\n") |
| 91 | + self.output_text.see(tk.END) |
| 92 | + return |
| 93 | + else: |
| 94 | + self.output_text.insert(tk.END, f"✔ Project created successfully\n") |
| 95 | + self.output_text.see(tk.END) |
| 96 | + self.master.update() |
| 97 | + |
| 98 | + #create devices |
| 99 | + self.output_text.insert(tk.END, f"Creating devices...\n") |
| 100 | + self.master.update() |
| 101 | + for idx, device in enumerate(self.devices, start=1): |
| 102 | + self.output_text.insert(tk.END, f"Creating Device {idx}: {device.name}\n") |
| 103 | + self.output_text.see(tk.END) |
| 104 | + self.master.update() |
| 105 | + try: |
| 106 | + self.tia_project.create_device(device.name, device.modules[0], 10.0) |
| 107 | + except Exception as e: |
| 108 | + self.output_text.insert(tk.END, f"❌ Error to create the device: {e}\n") |
| 109 | + self.output_text.see(tk.END) |
| 110 | + return |
| 111 | + else: |
| 112 | + self.output_text.insert(tk.END, f"✔ Device created successfully\n") |
| 113 | + self.output_text.see(tk.END) |
| 114 | + self.master.update() |
| 115 | + # def output_animated_text(self, text): |
| 116 | + # self.output_text.delete(1.0, tk.END) |
| 117 | + # if count <= 3: |
| 118 | + # dots = "." * count |
| 119 | + # self.output_text.insert(tk.END, f"{text}{dots}") |
| 120 | + # count += 1 |
| 121 | + # elif self.animateText: |
| 122 | + # count = 0 |
| 123 | + # self.output_text.insert(tk.END, text) |
| 124 | + # else: |
| 125 | + # return |
| 126 | + # self.master.after(500, lambda: self.output_animated_text(text)) |
46 | 127 |
|
47 | 128 | def main():
|
48 | 129 | root = tk.Tk()
|
49 |
| - app = FileParameterUI(root) |
| 130 | + app = ProjectGeneratorUI(root) |
50 | 131 | root.mainloop()
|
51 | 132 |
|
52 | 133 | if __name__ == "__main__":
|
|
0 commit comments