-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
205 lines (155 loc) · 7.05 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
"""This module contains the GUI code for the PLC Translator."""
import logging
import logging.config
import os
from pathlib import Path
from tkinter import Text, filedialog
import customtkinter as ctk
import plctranslator
from config.config import get_config
_CONFIG = get_config()
if _CONFIG.logging is not None:
logging.config.dictConfig(_CONFIG.logging)
ctk.set_appearance_mode("dark") # "Dark" eller "Light"
ctk.set_default_color_theme("dark-blue") # Endre temaet etter behov
def choose_sourcefile():
"""Open a file dialog to select a source file."""
filepath = filedialog.askopenfilename(filetypes=[("SCL files", "*.scl")])
if filepath:
sourcefile_var.set(filepath)
if plctranslator.check(plctranslator.read_scl_file(filepath)):
status_label.configure(
text="Convertion is possible",
text_color="black",
fg_color="orange",
) # Endret fra config til configure
# Tømmer textboxen før ny tekst legges til
textbox.delete("1.0", "end")
# Anta at `resultat` er teksten du ønsker å vise i textboxen. Hvis funksjonen
# `translate` returnerer teksten du vil vise, kan du bruke den direkte. Ellers,
# erstatte `resultat` med riktig variabel eller streng du vil vise.
textbox.insert("1.0", plctranslator.log_stream.getvalue())
plctranslator.log_stream.truncate()
plctranslator.log_stream.seek(0)
else:
textbox.delete("1.0", "end")
status_label.configure(text="Convertion is not possible", fg_color="red") # Endret fra config til configure
converting_btn.configure(state="disabled")
textbox.insert("1.0", plctranslator.log_stream.getvalue())
plctranslator.log_stream.truncate()
plctranslator.log_stream.seek(0)
check_convertion()
def choose_destinationfolder():
"""Open a file dialog to select a target folder."""
mappesti = filedialog.askdirectory()
if mappesti:
destinationfolder_var.set(mappesti)
check_convertion()
def check_convertion():
"""Check if conversion is possible based on the selected source file and target folder."""
sourcefile = sourcefile_var.get()
destinationfolder = destinationfolder_var.get()
is_possible = (
(sourcefile != "") and (destinationfolder != "") and plctranslator.check(plctranslator.read_scl_file(sourcefile))
)
plctranslator.log_stream.truncate()
plctranslator.log_stream.seek(0)
if is_possible:
converting_btn.configure(state="normal")
open_file_explorer_button.configure(state="normal")
show_full_info_button.configure(state="normal")
def converter():
"""Converts the source file to the target folder."""
file = sourcefile_var.get()
plctranslator.translate(file, destinationfolder_var.get())
end_index = textbox.index(ctk.END)
lines = textbox.get("1.0", end_index).count("\n")
textbox.insert(float(lines), plctranslator.log_stream.getvalue())
plctranslator.log_stream.truncate()
plctranslator.log_stream.seek(0)
status_label.configure(text="Convertion successful", text_color="white", fg_color="green")
converting_btn.configure(state="disabled")
def open_file_explorer_destenation_folder():
"""Opens the file explorer to the destination folder."""
sourcefile = sourcefile_var.get()
destinationfolder = destinationfolder_var.get()
is_possible = (
(sourcefile != "") and (destinationfolder != "") and plctranslator.check(plctranslator.read_scl_file(sourcefile))
)
if is_possible:
os.startfile(destinationfolder_var.get()) # noqa: S606
def show_full_info_from_converting():
"""Shows the full info from the converting in the textbox."""
fulltext = plctranslator.read_scl_file(sourcefile_var.get())
sourcefile = sourcefile_var.get()
destinationfolder = destinationfolder_var.get()
is_possible = (
(sourcefile != "") and (destinationfolder != "") and plctranslator.check(plctranslator.read_scl_file(sourcefile))
)
if is_possible:
textbox.delete("1.0", "end")
textbox.insert("1.0", fulltext)
plctranslator.log_stream.truncate()
root = ctk.CTk()
root.title("Goodtech TIA to TwinCAT Translator")
script_dir = Path(__file__).parent # Henter mappen hvor scriptet ligger
icon_path = "final_gt_ico.ico"
try:
root.iconbitmap(icon_path)
except Exception as e:
print(f"Feil ved setting av ikon: {e}")
root.geometry("850x500") # Oppdatert for ekstra plass
root.resizable(width=True, height=True)
root.minsize(850, 500)
# Opprette rammer for layout-separasjon
left_top_frame = ctk.CTkFrame(master=root)
left_top_frame.pack(side=ctk.LEFT, fill=ctk.BOTH, expand=True)
left_btm_f = ctk.CTkFrame(master=left_top_frame)
left_btm_f.pack(side=ctk.BOTTOM, fill=ctk.BOTH, expand=True)
left_btm_f_l = ctk.CTkFrame(master=left_btm_f)
left_btm_f_l.pack(side=ctk.LEFT, fill=ctk.BOTH, expand=True)
left_btm_f_r = ctk.CTkFrame(master=left_btm_f)
left_btm_f_r.pack(side=ctk.RIGHT, fill=ctk.BOTH, expand=True)
right_frame = ctk.CTkFrame(master=root)
right_frame.pack(side=ctk.RIGHT, fill=ctk.BOTH, expand=True)
sourcefile_var = ctk.StringVar()
destinationfolder_var = ctk.StringVar()
"""Left frame"""
# sourcefile part
ctk.CTkLabel(left_top_frame, text="Choose sourcefile:").pack(pady=10)
ctk.CTkEntry(left_top_frame, textvariable=sourcefile_var, state="readonly").pack()
ctk.CTkButton(left_top_frame, text="Browse", command=choose_sourcefile).pack()
# destination folder part
ctk.CTkLabel(left_top_frame, text="Choose destination folder:").pack(padx=10)
ctk.CTkEntry(left_top_frame, textvariable=destinationfolder_var, state="readonly").pack()
ctk.CTkButton(left_top_frame, text="Browse", command=choose_destinationfolder).pack(padx=20)
# Statuslabel for converting, if possible or not
status_label = ctk.CTkLabel(left_top_frame, text="Waiting For Input", fg_color="gray")
status_label.pack(fill=ctk.X, pady=40, padx=5)
# Convert button
converting_btn = ctk.CTkButton(left_top_frame, text="Convert", command=converter, state="disabled")
converting_btn.pack(pady=3)
# Open file explorer button
open_file_explorer_button = ctk.CTkButton(
left_btm_f_l, text="Open destination folder", command=open_file_explorer_destenation_folder, state="disabled"
)
open_file_explorer_button.pack(pady=25)
# Show full info from converting
show_full_info_button = ctk.CTkButton(
left_btm_f_r, text="Show convertion result", command=show_full_info_from_converting, state="disabled"
)
show_full_info_button.pack(pady=25)
# clear button
textbox: Text
clear_button = ctk.CTkButton(left_btm_f_l, text="Clear", command=lambda: textbox.delete("1.0", "end"))
clear_button.pack()
# Exit button
exit_button = ctk.CTkButton(left_btm_f_r, text="Exit", command=root.quit)
exit_button.pack()
"""Right frame"""
# Label for right_frame
ctk.CTkLabel(right_frame, text="Output window for checks:").pack(pady=10)
# Textboks in right_frame
textbox = Text(right_frame, height=20, width=50)
textbox.pack(fill=ctk.BOTH, expand=True, padx=(0, 10), pady=(0, 10))
root.mainloop()