-
Notifications
You must be signed in to change notification settings - Fork 1
/
gui.py
247 lines (194 loc) · 9.73 KB
/
gui.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# gui and config processing
# gui
import os
import sys
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
from tkinter import filedialog
# configparser
from configparser import ConfigParser
# project files
import version as vs
import prepare_trends as ppt
import tag_names
import common_vars
class AppGUI:
# languages
languageChoices = ["English","Polski"] #,"Русский"
languageCodes = ["EN","PL"]
openedFileNames = [] #actually opened filename
paddingNSWE = 20 # default padding on all sides
configSettings = {
'configIniFile' : 'appconfig.ini',
'defaultSection': 'default',
'langDefault' : 'EN', # default language is English
'useMiddleEndianDefault' : True, # default is True
}
# fills variables with proper texts
def LoadTexts(self):
# button texts
lang = self.GetIniParam('lang') # get language
if(lang == 'PL'):
self.text_openButton = "Otwórz plik"
self.text_addButton = "Dodaj plik do istniejącego trendu"
#settings texts
self.text_settings = "Ustawienia:"
self.text_swapWords32bitVars = "Użyj Middle-endian dla zmiennych 32-bitowych"
self.text_language = "Język:"
self.text_alreadyOpened = "Ten plik został już otwarty!"
self.text_alert = "Ostrzeżenie"
else:
# button texts
self.text_openButton = "Open file"
self.text_addButton = "Add file to existing trend"
#settings texts
self.text_settings = "Settings:"
self.text_swapWords32bitVars = "Use Middle-endian for 32-bit variables"
self.text_language = "Language:"
self.text_alreadyOpened = "This file has already been opened!"
self.text_alert = "Warning"
# vymeni texty
def ChangeTexts(self):
self.openButton['text'] = self.text_openButton
self.addButton['text'] = self.text_addButton
self.labelSettings['text'] = self.text_settings
self.useMiddleEndianCheckBox['text'] = self.text_swapWords32bitVars
self.langLabel['text'] = self.text_language
self.copyrightInfoLabel['text'] = vs.appCopyrightInfo
# constructor, draws the window
def __init__(self, master=None):
# creating basic window
self.window = tk.Tk()
#self.window.geometry("312x324") # size of the window width:- 500, height:- 375
self.window.resizable(0, 0) # this prevents from resizing the window
self.window.title(vs.appTitle)
#icon file
iconFile = "images/vonsch.ico"
if(os.path.exists(iconFile)):
self.window.iconbitmap(iconFile) # ikona
else: # icon for PyInstaller package
fullIconPath = os.path.join(sys._MEIPASS, iconFile)
if(os.path.exists(fullIconPath)):
self.window.iconbitmap(fullIconPath)
self.window.protocol("WM_DELETE_WINDOW", self.on_closing)
# create the application
print("Starting GUI...")
# load configuration
self.LoadIni()
# load texts
self.LoadTexts()
# loads dictionary of tag names
tag_names.LoadTagNames(self.GetIniParam('lang'))
# buttony
self.openButton = tk.Button(self.window, text = self.text_openButton, command=self.openButton_click, fg = "black")
self.openButton.grid(row=0, column=0, padx=self.paddingNSWE, pady=self.paddingNSWE, sticky=tk.E+tk.N)# 'fg - foreground' is used to color the contents
self.addButton = tk.Button(self.window, text = self.text_addButton, command=self.addButton_click, fg = "red", state=tk.DISABLED)
self.addButton.grid(row=0, column=1, padx=self.paddingNSWE, pady=self.paddingNSWE, sticky=tk.W+tk.N) # 'text' is used to write the text on the Button
## settings
self.labelSettings = tk.Label(self.window,text=self.text_settings)
self.labelSettings.grid(row=2, column=0, columnspan=2, padx=self.paddingNSWE, pady=self.paddingNSWE, sticky=tk.W+tk.N)
# use Middle-Endian (Weintek block read)
self.useMiddleEndianCheckBox = tk.Checkbutton(self.window, text=self.text_swapWords32bitVars, variable=self.useMiddleEndianIntVar) # default je ON
self.useMiddleEndianCheckBox.grid(row=4,column=0,columnspan=2)
# language label
self.langLabel = tk.Label(self.window,text=self.text_language)
self.langLabel.grid(row=3, column=0) # , padx=self.paddingNSWE, pady=self.paddingNSWE, sticky=tk.E+tk.N
# language combo box
lang = self.GetIniParam('lang')
langIndex = self.languageCodes.index(lang) #nacitanie indexu
self.languageComboBox=ttk.Combobox(self.window,values=self.languageChoices,width=20)
self.languageComboBox.grid(row=3,column=1,padx=self.paddingNSWE,pady=self.paddingNSWE,sticky=tk.E+tk.N)
self.languageComboBox.current(langIndex)
self.languageComboBox.bind('<<ComboboxSelected>>', self.languageComboBox_change)
# copyright info
self.copyrightInfoLabel = tk.Label(self.window, text=vs.appCopyrightInfo)
self.copyrightInfoLabel.grid(row=5,column=0,columnspan=2,pady=self.paddingNSWE,sticky=tk.S)
def EnableOrDisableAddFile(self):
if('.dtl' in self.openedFileNames[0]):
self.addButton.configure(state=tk.NORMAL)
else:
self.addButton.configure(state=tk.DISABLED)
# kliknutie na openButton
def openButton_click(self):
common_vars.ClearVars() # erases all data before opening new file
self.openedFileNames.clear() # clear list of opened files
self.UpdateUseMiddleEndianVars() # updates the middle endian variable, no bind function
openFile = self.OpenFileDialogGetFileName()
self.openedFileNames.append(openFile) # add file to list of opened files
self.EnableOrDisableAddFile() # can another file be appended?, digable/enable button
ppt.OpenTrendFile(openFile) #opens file
# kliknutie na addButton
def addButton_click(self):
addFile = self.AddFileDialogGetFileName()
if addFile in self.openedFileNames:
tk.messagebox.showinfo(self.text_alert, self.text_alreadyOpened) # warning that it has been already opened
else: # OK
self.openedFileNames.append(addFile) # add file
ppt.AddTrendFile(addFile) #opens file and draws everything
# zmena jazyka
def languageComboBox_change(self, event=None):
#tk.messagebox.showinfo("Info","Zmenil si jazyk!")
langIndex = self.languageComboBox.current()
lang = self.languageCodes[langIndex]
self.configObj.set(self.configSettings['defaultSection'], 'lang', lang)
# load and change the texts
self.LoadTexts()
self.ChangeTexts()
# loads dictionary of tag names
tag_names.LoadTagNames(lang)
# debug info to console
if event: # <-- this works only with bind because `command=` doesn't send event
print("Language changed to:", event.widget.get())
# otvori dialog a vrati nazov suboru
def OpenFileDialogGetFileName(self):
file_path = filedialog.askopenfilename(filetypes = (("Supported HMI panel logs",".txt .dtl"),("All files","*.*"))) # samotny dialog na vyber okna
return file_path
# doplnenie suboru DTL
def AddFileDialogGetFileName(self):
baseFileName = os.path.basename(self.openedFileNames[0]) #first file opened is taken
file_path = filedialog.askopenfilename(filetypes = (("Weintek DTL file for same day",baseFileName),("All files","*.*"))) # samotny dialog na vyber okna
return file_path
# beh programu
def run(self):
self.window.mainloop()
# Load ini file
def LoadIni(self):
self.configObj= ConfigParser()
self.configObj.read(self.configSettings['configIniFile'])
section = self.configSettings['defaultSection']
# when ini file is empty or sections do not exist
if section not in self.configObj:
self.configObj.add_section(section)
self.configObj.set(section,'lang', self.configSettings['langDefault'])
self.configObj.set(section,'useMiddleEndian', str(self.configSettings['useMiddleEndianDefault']))
useMiddleEndianLocal = True # default ON
else: # ini file looks OK
useMiddleEndianLocal = (self.configObj.getboolean(section, 'useMiddleEndian'))
# naplnime useMiddleEndian, pouziva sa neskor pri inicializacii checkbuttonu
self.useMiddleEndianIntVar = tk.IntVar(value=int(useMiddleEndianLocal))
self.UpdateUseMiddleEndianVars()
# update global variable
def UpdateUseMiddleEndianVars(self):
common_vars.SetMiddleEndianUsage(bool(self.useMiddleEndianIntVar.get())) # setting the global variable
# save ini file
def SaveIni(self):
# deal with useMiddleEndian, different formats
section = self.configSettings['defaultSection']
useMiddleEndianBool = bool(self.useMiddleEndianIntVar.get())
self.configObj.set(section,'useMiddleEndian', str(useMiddleEndianBool)) # double type conversion, wants boolean in form of string
# write all configuration to file
with open(self.configSettings['configIniFile'], 'w') as output_file:
self.configObj.write(output_file)
# reads ini parameter
def GetIniParam(self, paramName, section = None):
if(section == None):
section = self.configSettings['defaultSection']
return self.configObj[section][paramName]
# when closing the window
def on_closing(self):
# save ini
self.SaveIni()
print('')
print('Closing application...')
self.window.destroy()