-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConverterGUI.py
872 lines (755 loc) · 36.1 KB
/
ConverterGUI.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
#!/usr/bin/python3
try:
import time
import os
import os.path
import sys
import platform
import webbrowser
import configparser
import logging
except Exception as e:
print("Failed to import standard Python libraries.\n Please check your Python version and install.\n Exception: %s" % (str(e)))
try:
import PIL.Image
except Exception as e:
print("Failed to import Python Image Library\n Please install it with pip using 'pip install pillow'. (Shouldn't be a much of a problem.) \n Exception: %s" % (str(e)))
try:
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
from tkinter import PhotoImage
import tkinter.scrolledtext as tkst
except Exception as e:
print("Failed to import tkinter\n %s" % (str(e)))
sys.exit(1)
try:
from convlib import *
except Exception as e:
messagebox.showerror("Fatal error", "Unable to load convlib. Please redownload this program or download convlib from http://github.com/ZanyLeonic/Converter\nException: {}".format(str(e)))
print("Unable to load convlib. Please redownload this program or download convlib from http://github.com/ZanyLeonic/Converter\nException: {}".format(str(e)))
sys.exit(1)
# App info
appname="Converter"
author="Leo Durrant (2016/17)"
buliddate="27/09/17"
version="0.21a"
release="alpha"
# App resources
configfilename="data//config_gui.ini"
configfilereadname=r"data/config_gui.ini"
progicon=r"data/images/converter.ico"
abouticon=r"data/images/about_con.gif"
# Some import error thingys
licenseabout="""
Converter
Copyright (C) 2016, 2017 Leo Durrant
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
# Standard window settings
windowwidth=300
windowheight=250
# App settings and variables
bLoggingenabled=1
bCheckforupdatesonstartup=1
bAppendtxtwhensaving=1
onlineversioninfourl="http://zanyleonic.github.io/UpdateInfo/Converter/version.ver"
latestversionurl="http://zanyleonic.github.io/UpdateInfo/Converter/latest.url"
websiteurl="http://leonic.no-ip.biz/"
githuburl="http://github.com/ZanyLeonic/Converter/"
patchnotesurl="http://zanyleonic.github.io/UpdateInfo/Converter/latest.info"
logger=logging.getLogger("[Converter]")
conversionsaved=0
appfrozen=False
# Check if the app is frozen
if getattr(sys, 'frozen', False):
appfrozen = True
if appfrozen == True and iolib.isexe64bit() == True:
onlineversioninfourl="http://zanyleonic.github.io/UpdateInfo/Converter/win64/version.ver"
latestversionurl="http://zanyleonic.github.io/UpdateInfo/Converter/win64/latest.url"
patchnotesurl="http://zanyleonic.github.io/UpdateInfo/Converter/win64/latest.info"
elif appfrozen == True and iolib.isexe64bit() == False:
onlineversioninfourl="http://zanyleonic.github.io/UpdateInfo/Converter/win32/version.ver"
latestversionurl="http://zanyleonic.github.io/UpdateInfo/Converter/win32/latest.url"
patchnotesurl="http://zanyleonic.github.io/UpdateInfo/Converter/win32/latest.info"
#cx_freeze
if appfrozen == True:
# Using cx_freeze
currentdir=os.path.dirname(sys.executable)
else:
# Running as a script.
currentdir=os.path.dirname(os.path.realpath(__file__))
systemos=iolib.systemos()
patchnotesopen=False
def checkmethod(option, window):
selected=str(option)
if selected == "()":
messagebox.showerror("Error", "Please select an option.")
logger.error("User chose option {}. But {} is an invaild option!".format(selected, selected))
elif "0" in selected:
window.destroy()
createconvertwindow(selected)
elif "1" in selected:
window.destroy()
createconvertwindow(selected)
elif "2" in selected:
window.destroy()
createconvertwindow(selected)
elif "3" in selected:
window.destroy()
createconvertwindow(selected)
elif "4" in selected:
window.destroy()
createconvertwindow(selected)
elif "5" in selected:
messagebox.showinfo("Information", "Every seperate hexadecimal has to be on a new line in order to be converted correctly.")
window.destroy()
createconvertwindow(selected)
else:
logger.error("User chose option {}. But {} is an invaild option!".format(selected, selected))
messagebox.showerror("Error", "{} is an invaild option, please try again.".format(selected))
def empty():
print("", end="")
def convert(mode, inputcon, window):
converted=""
window.destroy()
if "0" in mode:
try:
print(inputcon)
converted=conlib.text2binary(inputcon)
createresultwindow(mode, converted)
except Exception as e:
logger.error("Error when converting %s to mode %s\n Exception: %s" % (inputcon, mode, str(e)))
messagebox.showerror("Error", "Error when converting %s to mode %s\n Exception: %s." % (inputcon, mode, str(e)))
createmainwindow()
elif "1" in mode:
try:
converted=conlib.binary2text(inputcon)
createresultwindow(mode, converted)
except Exception as e:
logger.error("Error when converting %s to mode %s\n Exception: %s" % (inputcon, mode, str(e)))
messagebox.showerror("Error", "Error when converting %s to mode %s\n Exception: %s." % (inputcon, mode, str(e)))
createmainwindow()
elif "2" in mode:
try:
converted=conlib.int2binary(inputcon)
createresultwindow(mode, converted)
except Exception as e:
logger.error("Error when converting %s to mode %s\n Exception: %s" % (inputcon, mode, str(e)))
messagebox.showerror("Error", "Error when converting %s to mode %s\n Exception: %s." % (inputcon, mode, str(e)))
createmainwindow()
elif "3" in mode:
try:
converted=conlib.binary2int(inputcon)
createresultwindow(mode, converted)
except Exception as e:
logger.error("Error when converting %s to mode %s\n Exception: %s" % (inputcon, mode, str(e)))
messagebox.showerror("Error", "Error when converting %s to mode %s\n Exception: %s." % (inputcon, mode, str(e)))
createmainwindow()
elif "4" in mode:
try:
converted=conlib.text2hexdec(inputcon)
createresultwindow(mode, converted)
except Exception as e:
logger.error("Error when converting %s to mode %s\n Exception: %s" % (inputcon, mode, str(e)))
messagebox.showerror("Error", "Error when converting %s to mode %s\n Exception: %s." % (inputcon, mode, str(e)))
createmainwindow()
elif "5" in mode:
try:
inputcon=inputcon.strip()
converted=conlib.hexdec2text(inputcon)
createresultwindow(mode, converted)
except Exception as e:
logger.error("Error when converting %s to mode %s\n Exception: %s" % (inputcon, mode, str(e)))
messagebox.showerror("Error", "Error when converting %s to mode %s\n Exception: %s." % (inputcon, mode, str(e)))
createmainwindow()
else:
logger.error("Error when converting %s to mode %s\n Exception: %s" % (inputcon, mode, str(e)))
messagebox.showerror("Error", "Error when converting %s to mode %s\n Exception: %s" % (inputcon, mode, "invaild operation."))
def showsettings(window):
logger.info("Opening settings window from window")
window.destroy()
createsettingwindow()
def showabout():
logger.info("Opening about window from window")
createaboutwindow()
def initializesetting(section, valuename, checkbutton):
value=False
try:
value=int(readvaluefromconfig(section, valuename))
except Exception as e:
logger.error("Invaild value '%s' from section '%s'.\n Exception: %s" % (valuename, section, str(e)))
messagebox.showerror("Error", "Invaild value '%s' from section '%s'.\n Exception: %s" % (valuename, section, str(e)))
if value == 1:
checkbutton.select()
elif value == 0:
checkbutton.deselect()
else:
logger.error("Invaild value '%s' from section '%s'." % (valuename, section))
messagebox.showerror("Error", "Invaild value '%s' from section '%s'." % (valuename, section))
def readvaluefromconfig(section, valuename):
try:
config = configparser.ConfigParser()
config.read(configfilereadname)
try:
val = config[section][valuename]
return val
except Exception as e:
logger.error("Cannot find value %s in %s. Check %s.\n Exception: %s\n Attempting to create..." % (valuename, section, configfilereadname, str(e)))
messagebox.showerror("Error", "Cannot find value %s in %s. Check %s.\n Exception: %s\n Attempting to create..." % (valuename, section, configfilereadname, str(e)))
createconfig()
return "null"
except Exception as e:
logger.error("Cannot read %s.\n Exception: %s" % (configfilereadname, str(e)))
messagebox.showerror("Error", "Cannot read %s.\n Exception: %s" % (configfilereadname, str(e)))
return "null"
def setvalueinconfig(section, key, value):
config = configparser.ConfigParser()
try:
config.read(configfilereadname)
config.set(section, key, value)
with open(configfilename, 'w') as configfile:
config.write(configfile)
except Exception as e:
logger.error("Unhandled error occurred while writing the config. Using default settings.\n Exception: %s" % (str(e)))
messagebox.showerror("Error", "Unhandled error occurred while writing the config. Using default settings.\n Exception: %s" % (str(e)))
def loadconfig():
if os.path.exists(configfilereadname) == True:
try:
config = configparser.ConfigParser()
config.read(configfilereadname)
try:
bLoggingenabled = readvaluefromconfig('general', 'bLoggingenabled')
if bLoggingenabled == "null":
logging.info
bLoggingenabled = 1
bAppendtxtwhensaving = readvaluefromconfig('general', 'bAppendtxtwhensaving')
bCheckforupdatesonstartup = readvaluefromconfig('updater', 'bCheckforupdatesonstartup')
except Exception as e:
bLoggingenabled = 1
bCheckforupdatesonstartup = 1
logger.error("Cannot find option value(s) in config.ini.\n Exception: %s" % (str(e)))
print("Configuration file out of date! Rebuilding...")
createconfig()
except Exception as e:
createconfig()
else:
createconfig()
def createconfig():
config = configparser.ConfigParser()
config['general'] = {'bLoggingenabled': '1', 'bAppendtxtwhensaving' : '1'}
config['updater'] = {'bCheckforupdatesonstartup': '1'}
try:
with open(configfilename, 'w') as configfile:
config.write(configfile)
except Exception as e:
logger.error("Unhandled error occurred while writing the config. Using default settings.\n Exception: %s" % (str(e)))
messagebox.showerror("Error", "Unhandled error occurred while writing the config. Using default settings.\n Exception: %s" % (str(e)))
def setloggingoptions():
while True:
if os.path.exists("data//logs") == True:
try:
logfilename = 'data//logs//log_gui ({}).log'.format(time.strftime("%d-%m-%Y"))
handler = logging.FileHandler(logfilename)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
if bLoggingenabled == 0:
logging.basicConfig()
else:
logging.basicConfig(filename=logfilename, level=logging.DEBUG)
logger.addHandler(handler)
logger.handlers.pop()
logger.debug('Started %s at %s on %s', appname, time.strftime("%H:%M:%S"), time.strftime("%d/%m/%Y"))
logger.info('Running on {} version {} {}.'.format(platform.system(), platform.release(), platform.machine()))
# cx_freeze?
if appfrozen == True:
logger.info("Currently running the frozen version. Compiled by cx_freeze.")
else:
logger.info("Currently running from the Python script.")
logger.info("%s version (%s %s) started in directory: %s", appname, version, release, currentdir)
break
except Exception as e:
messagebox.showerror("Error", "Cannot create log.\n Exception: %s\nTrying to create logs folder..." % (str(e)))
try:
os.mkdir("data//logs")
except Exception as e:
messagebox.showerror("Error", "Cannot create logs folder.\n Exception: %s" % (str(e)))
else:
os.mkdir("data//logs")
def savefile(master, text):
try:
filename = filedialog.asksaveasfilename(filetypes=(("Text files", "*.txt"),
("All files", "*.*") ))
if bAppendtxtwhensaving == 1:
filename = filename + ".txt"
if not filename == "":
txtcontent = iolib.writetotextfile(text, filename)
if txtcontent == 1:
print("Something went wrong while saving : %s.\n Does this user have to approrite permissions to the file?" % (filename))
messagebox.showerror("Error", "Something went wrong while saving to: %s.\n Do you have the approrite permissions to the file?" % (filename))
else:
conversionsaved=1
print("Saved conversion to %s." % (filename))
messagebox.showinfo("Saved", "Saved conversion to %s." % (filename))
# print(txtcontent) # debugging purposes.
else:
# print("passed") # debugging purposes.
pass
except Exception as e:
print("Error while loading file picker.\n %s." % (str(e)))
messagebox.showerror("Error", "Error while loading file save.\n %s." % (str(e)))
filename = 1
return filename
def openfile(master, textbox):
try:
filename = filedialog.askopenfilename(filetypes=(("Text files", "*.txt"),
("All files", "*.*") ))
if not filename == "":
txtcontent = iolib.readfromtextfile(filename)
if txtcontent == 3:
print("Something went wrong while reading: %s.\n Does this user have to approrite permissions to the file?" % (filename))
messagebox.showerror("Error", "Something went wrong while reading: %s.\n Do you have to approrite permissions to the file?" % (filename))
else:
textbox.insert(INSERT, txtcontent)
# print(txtcontent) # debugging purposes.
else:
# print("passed") # debugging purposes.
pass
except Exception as e:
print("Error while loading file picker.\n %s." % (str(e)))
messagebox.showerror("Error", "Error while loading file picker.\n %s." % (str(e)))
filename = 1
return filename
def copytoclipboard(window, content):
try:
window.clipboard_clear()
window.clipboard_append(content)
messagebox.showinfo("Success", "Content copied to clipboard.")
except Exception as e:
print("Failed to copy '%s' to the clipboard.\n Exception: %s" % (content, str(e)))
messagebox.showerror("Error", "Failed to copy '%s' to the clipboard.\n Exception: %s." % (content, str(e)))
def exittowindow(window, nextwindow=0):
window.destroy()
if nextwindow == 0:
createmainwindow()
def exitapp(window):
window.destroy()
sys.exit(0)
def checkforupdates(supressinfo=True):
status=0
try:
print("Checking for updates...", end=" ")
onlineversion=iolib.readonlinefile(onlineversioninfourl)
downloadurl=iolib.readonlinefile(latestversionurl)
patchnotes=iolib.readonlinefile(patchnotesurl)
except Exception as e:
print("Failed!")
status=1
logger.error("Failed to check for updates.\n Exception: %s" % (str(e)))
if supressinfo == False:
messagebox.showerror("Check for updates", "Failed to check for updates.\n Exception: {}".format(str(e)), icon='info')
if status == 0:
logger.info("Comparing onlineversion(%s) to local version(%s)." % (onlineversion, version))
if not onlineversion==version:
logger.info("Version %s is avaiable." % (onlineversion))
createupdateinfowindow("Version {} is available".format(onlineversion), patchnotes, onlineversion, downloadurl)
elif onlineversion==version:
if supressinfo == False:
messagebox.showinfo("Check for updates", "{} is up to date at version {}.".format(appname, version))
logger.info("%s is up to date. Version (%s)" % (appname, version))
def restart(window):
window.destroy()
createmainwindow()
def end(window):
window.destroy()
sys.exit(0)
def closepatchnotes(window):
global patchnotesopen
patchnotesopen=False
window.destroy()
def closewindowandopenurl(window, downloadurl):
window.destroy()
webbrowser.open(downloadurl)
def createupdateinfowindow(title, text, onversion, downloadurl):
if systemos == 0:
windowwidth=385
windowheight=93
elif systemos == 1:
windowwidth=385
windowheight=93
else:
windowwidth=385
windowheight=93
abouticon1=r"data/images/about_con.gif"
licensewindow = Toplevel()
licensewindow.title("{}: {}".format(appname, title))
try:
if systemos == 0: # Check if the user is using Windows...
licensewindow.iconbitmap(progicon)
else: # If not...
ico = PhotoImage(file=abouticon1)
licensewindow.tk.call('wm', 'iconphoto', licensewindow._w, ico)
except Exception as e:
messagebox.showerror("Error", "Error while setting program icon\n {}.".format(str(e)))
licensewindow.geometry("{}x{}".format(windowwidth,windowheight))
choicebox = Frame(licensewindow)
updatelabel = Label(licensewindow, text="Version {} is available.\nDo you want to download it now? (Current version: {})".format(onversion, version))
btn1 = Button(choicebox, text="Patch notes", command=lambda: createlicensewindow("Patch notes (Version {})".format(onversion), text, True))
btn2 = Button(choicebox, text="Yes", command=lambda: closewindowandopenurl(licensewindow, downloadurl))
btn3 = Button(choicebox, text="No", command=lambda: licensewindow.destroy())
btn1.grid(row=0, column=3, sticky=N+S+W+E, padx=2, pady=5)
btn2.grid(row=0, column=1, sticky=N+S+W+E, padx=2, pady=5)
btn3.grid(row=0, column=2, sticky=N+S+W+E, padx=2, pady=5)
choicebox.pack(side=BOTTOM)
updatelabel.pack()
def createlicensewindow(title, text, patchnotes=False):
global patchnotesopen
if systemos == 0:
windowwidth=670
windowheight=375
elif systemos == 1:
windowwidth=670
windowheight=350
else:
windowwidth=670
windowheight=350
abouticon1=r"data/images/about_con.gif"
licensewindow = Toplevel()
licensewindow.title("{}: {}".format(appname, title))
if patchnotesopen==True:
licensewindow.destroy()
else:
try:
if systemos == 0: # Check if the user is using Windows...
licensewindow.iconbitmap(progicon)
else: # If not...
ico = PhotoImage(file=abouticon1)
licensewindow.tk.call('wm', 'iconphoto', licensewindow._w, ico)
except Exception as e:
messagebox.showerror("Error", "Error while setting program icon\n {}.".format(str(e)))
licensewindow.geometry("{}x{}".format(windowwidth,windowheight))
if patchnotes == True:
if patchnotesopen==False:
patchnotesopen=True
licensewindow.protocol("WM_DELETE_WINDOW", lambda: closepatchnotes(licensewindow))
aboutbox = tkst.ScrolledText(licensewindow, width=str(windowwidth), height=str(windowheight))
aboutbox.insert(INSERT, text)
aboutbox.pack(side = TOP)
aboutbox.mainloop()
def createmainwindow():
windowwidth=300
windowheight=250
mainwindow = Toplevel()
mainwindow.title(appname)
try:
if systemos == 0: # Check if the user is using Windows...
mainwindow.iconbitmap(progicon)
else: # If not...
ico = PhotoImage(file=abouticon)
mainwindow.tk.call('wm', 'iconphoto', mainwindow._w, ico)
except Exception as e:
messagebox.showerror("Error", "Error while setting program icon\n {}.".format(str(e)))
mainwindow.geometry("{}x{}".format(windowwidth,windowheight))
mainwindow.resizable(width=False, height=False)
panel1 = Frame(mainwindow)
panel2 = Frame(mainwindow)
label1txt = StringVar()
label1 = Label(panel1, textvariable=label1txt, relief=SUNKEN, width=str(windowwidth))
msb = Listbox(panel1, width=str(windowwidth))
btn1 = Button(mainwindow, text="Continue", command=lambda: checkmethod(msb.curselection(), mainwindow), width=str(windowwidth))
btn2 = Button(panel2, text="Settings", command=lambda: showsettings(mainwindow), width=20)
btn3 = Button(panel2, text="About", command=showabout, width=25)
label1txt.set("Welcome to %s version %s.\n Please select an option below." % (appname, version))
msb.insert(1, "Convert text to binary")
msb.insert(2, "Convert binary to text")
msb.insert(3, "Convert integer to binary")
msb.insert(4, "Convert binary to integer")
msb.insert(5, "Convert text and integer to hexadecimal")
msb.insert(6, "Convert hexadecminal to text and integer")
panel1.pack(side = TOP)
label1.pack(side = TOP)
msb.pack(side = TOP)
btn1.pack(side = TOP)
panel2.pack(side = TOP)
btn2.pack(side = LEFT)
btn3.pack(side = RIGHT)
mainwindow.protocol("WM_DELETE_WINDOW", lambda: exitapp(mainwindow))
mainwindow.mainloop()
def createconvertwindow(mode):
windowwidth=414
windowheight=475
convertwindow = Toplevel()
modedesc = "null"
if "0" in mode:
modedesc="Convert text to binary"
elif "1" in mode:
modedesc="Convert binary to text"
elif "2" in mode:
modedesc="Convert integer to binary"
elif "3" in mode:
modedesc="Convert binary to integer"
elif "4" in mode:
modedesc="Convert text and integer to hexadecimal"
elif "5" in mode:
modedesc="Convert hexadecimal to text and integer"
else:
modedesc="Unknown or inproper implmented mode."
txtcontent = ""
filename = ""
convertwindow.title("%s: %s" % (appname, modedesc))
try:
if systemos == 0:
convertwindow.iconbitmap(progicon)
else:
ico = PhotoImage(file=abouticon)
convertwindow.tk.call('wm', 'iconphoto', convertwindow._w, ico)
except Exception as e:
messagebox.showerror("Error", "Error while setting program icon\n {}.".format(str(e)))
convertwindow.geometry("{}x{}".format(windowwidth,windowheight))
convertwindow.resizable(width=False, height=False)
panel1 = Frame(convertwindow)
panel2 = Frame(convertwindow)
con1txt = StringVar()
con1 = Label(panel1, textvariable=con1txt, relief=SUNKEN, width=str(windowwidth))
conbox = tkst.ScrolledText(panel1, width=str(windowwidth))
btn1 = Button(convertwindow, text="Continue", command=lambda: convert(mode, conbox.get("1.0", END), convertwindow), width=str(windowwidth))
btn2 = Button(convertwindow, text="Browse...", command=lambda: openfile(convertwindow, conbox), width=str(windowwidth))
con1txt.set("Mode: {}\n Select a text file or type some into the textbox.".format(modedesc))
panel1.pack(side = TOP)
con1.pack()
conbox.pack(side = TOP)
btn1.pack(side = TOP)
panel2.pack(side = TOP)
btn2.pack(side = BOTTOM)
convertwindow.protocol("WM_DELETE_WINDOW", lambda: exittowindow(convertwindow))
convertwindow.mainloop()
def createresultwindow(mode, txt):
windowwidth=414
windowheight=545
resultwindow = Toplevel()
modedesc = "null"
conversionsaved=0
txtcontent = ""
filename = ""
if "0" in mode:
modedesc="Convert text to binary"
elif "1" in mode:
modedesc="Convert binary to text"
elif "2" in mode:
modedesc="Convert integer to binary"
elif "3" in mode:
modedesc="Convert binary to integer"
elif "4" in mode:
modedesc="Convert text and integer to hexadecimal"
elif "5" in mode:
modedesc="Convert hexadecimal to text and integer"
else:
modedesc="Unknown or inproper implmented mode."
resultwindow.title("%s: %s" % (appname, modedesc))
try:
if systemos == 0:
resultwindow.iconbitmap(progicon)
else:
ico = PhotoImage(file=abouticon)
resultwindow.tk.call('wm', 'iconphoto', resultwindow._w, ico)
except Exception as e:
messagebox.showerror("Error", "Error while setting program icon\n {}.".format(str(e)))
resultwindow.geometry("{}x{}".format(windowwidth, windowheight))
resultwindow.resizable(width=False, height=False)
panel1 = Frame(resultwindow)
con1txt = StringVar()
con1 = Label(panel1, textvariable=con1txt, relief=SUNKEN, width=str(windowwidth))
conbox = tkst.ScrolledText(panel1, width=str(windowwidth))
btn1 = Button(resultwindow, text="Convert again", command=lambda: restart(resultwindow), width=str(windowwidth))
btn2 = Button(resultwindow, text="Copy to clipboard", command=lambda: copytoclipboard(resultwindow, conbox.get("1.0", END)), width=str(windowwidth))
btn3 = Button(resultwindow, text="Save", command=lambda: savefile(resultwindow, conbox.get("1.0", END)), width=str(windowwidth))
btn4 = Button(resultwindow, text="Quit", command=lambda: end(resultwindow), width=str(windowwidth))
conbox.insert(INSERT, txt)
con1txt.set("Mode: {}\n The conversion has finished. Copy the text in the box below\n or click save.".format(modedesc))
panel1.pack(side = TOP)
con1.pack()
conbox.pack(side = TOP)
btn1.pack(side = TOP)
btn2.pack(side = TOP)
btn3.pack(side = TOP)
btn4.pack(side = TOP)
resultwindow.protocol("WM_DELETE_WINDOW", lambda: exittowindow(resultwindow))
resultwindow.mainloop()
def createsettingwindow():
windowwidth=300
windowheight=275
settingwindow = Toplevel()
settingwindow.title("{}: Settings".format(appname))
try:
if systemos == 0:
settingwindow.iconbitmap(progicon)
else:
ico = PhotoImage(file=abouticon)
settingwindow.tk.call('wm', 'iconphoto', settingwindow._w, ico)
except Exception as e:
messagebox.showerror("Error", "Error while setting program icon\n {}.".format(str(e)))
settingwindow.geometry("{}x{}".format(windowwidth,windowheight))
settingwindow.resizable(width=False, height=False)
panel1 = Frame(settingwindow)
panel2 = Frame(settingwindow)
group1 = LabelFrame(settingwindow, text="General", padx=5, pady=5)
group2 = LabelFrame(settingwindow, text="Updater", padx=5, pady=5)
con1txt = StringVar()
con2txt = StringVar()
check1val = IntVar()
check2val = IntVar()
check3val = IntVar()
con1txt.set("Modify the settings below to change\n the way the program works.")
con2txt.set("* These settings require {} to be\n restarted for full effect.".format(appname))
checkbox1 = Checkbutton(group1, text="Create log*", variable=check1val, width=str(windowwidth), command=lambda: setvalueinconfig("general", "bLoggingenabled", str(check1val.get())))
checkbox2 = Checkbutton(group2, text="Check for updates on startup", variable=check2val, width=str(windowwidth), command=lambda: setvalueinconfig("updater", "bCheckforupdatesonstartup", str(check2val.get())))
checkbox3 = Checkbutton(group1, text="Append '.txt' when saving files", variable=check3val, width=str(windowwidth), command=lambda: setvalueinconfig("general", "bappendtxtwhensaving", str(check3val.get())))
btn1 = Button(panel2, text="Close", command=lambda: restart(settingwindow), width=str(windowwidth))
con1 = Label(panel1, textvariable=con1txt, relief=SUNKEN, width=str(windowwidth))
con2 = Label(panel2, textvariable=con2txt, width=str(windowwidth))
initializesetting("general","bLoggingenabled", checkbox1)
initializesetting("updater","bCheckforupdatesonstartup", checkbox2)
initializesetting("general","bappendtxtwhensaving", checkbox3)
panel1.pack(side=TOP)
group1.pack(side=TOP, padx=10, pady=10)
group2.pack(side=TOP, padx=10, pady=10)
panel2.pack(side=BOTTOM)
con1.pack()
con2.pack()
checkbox1.pack(side=TOP)
checkbox2.pack(side=LEFT)
checkbox3.pack(side=LEFT)
btn1.pack(side=BOTTOM)
settingwindow.protocol("WM_DELETE_WINDOW", empty)
settingwindow.mainloop()
def createaboutwindow():
if systemos == 0:
windowwidth=475
windowheight=375
elif systemos == 1:
windowwidth=625
windowheight=375
else:
windowwidth=625
windowheight=375
aboutwindow = Toplevel()
aboutwindow.title("{}: About".format(appname))
try:
if systemos == 0:
aboutwindow.iconbitmap(progicon)
else:
ico = PhotoImage(file=abouticon)
aboutwindow.tk.call('wm', 'iconphoto', aboutwindow._w, ico)
except Exception as e:
messagebox.showerror("Error", "Error while setting program icon\n {}.".format(str(e)))
aboutwindow.geometry("{}x{}".format(windowwidth,windowheight))
aboutwindow.resizable(width=False, height=False)
try:
# Load the correct open source text file for corrisponding modes.
if appfrozen == False:
opensourcetext=iolib.readfromtextfile("data/text/opensourcelibraries.txt")
else:
opensourcetext=iolib.readfromtextfile("data/text/opensourcelibraries_cx.txt")
legalinfotext=iolib.readfromtextfile("data/text/legalinfo.txt")
except Exception as e:
messagebox.showerror("Error", "Error while reading about text files.\n {}.".format(str(e)))
# Initialize the variable...
appflavourinfo=""
# Then decide what it is going to hold.
if appfrozen == True and iolib.isexe64bit() == True and iolib.systemos() == 0:
appflavourinfo="a 64-bit executable on Windows."
elif appfrozen == True and iolib.isexe64bit() == False and iolib.systemos() == 0:
appflavourinfo="a 32-bit executable on Windows."
elif appfrozen == False and iolib.isexe64bit() == True and iolib.systemos() == 0:
appflavourinfo="a script running on a 64-bit interpreter on Windows."
elif appfrozen == False and iolib.isexe64bit() == False and iolib.systemos() == 0:
appflavourinfo="a script running on a 32-bit interpreter on Windows."
elif appfrozen == True and iolib.isexe64bit() == True and iolib.systemos() == 1:
appflavourinfo="a 64-bit executable on Linux."
elif appfrozen == True and iolib.isexe64bit() == False and iolib.systemos() == 1:
appflavourinfo="a 32-bit executable on Linux."
elif appfrozen == False and iolib.isexe64bit() == True and iolib.systemos() == 1:
appflavourinfo="a script running on a 64-bit interpreter on Linux."
elif appfrozen == False and iolib.isexe64bit() == False and iolib.systemos() == 1:
appflavourinfo="a script running on a 32-bit interpreter on Linux."
elif appfrozen == True and iolib.isexe64bit() == True and iolib.systemos() == 2:
appflavourinfo="a 64-bit executable on MacOS."
elif appfrozen == True and iolib.isexe64bit() == False and iolib.systemos() == 2:
appflavourinfo="a 32-bit executable on MacOS."
elif appfrozen == False and iolib.isexe64bit() == True and iolib.systemos() == 2:
appflavourinfo="a script running on a 64-bit interpreter on MacOS."
elif appfrozen == False and iolib.isexe64bit() == False and iolib.systemos() == 2:
appflavourinfo="a script running on a 32-bit interpreter on MacOS."
elif appfrozen == True and iolib.isexe64bit() == True and iolib.systemos() == 3:
appflavourinfo="a 64-bit executable on Unknown."
elif appfrozen == True and iolib.isexe64bit() == False and iolib.systemos() == 3:
appflavourinfo="a 32-bit executable on Unknown."
elif appfrozen == False and iolib.isexe64bit() == True and iolib.systemos() == 3:
appflavourinfo="a script running on a 64-bit interpreter on Unknown."
elif appfrozen == False and iolib.isexe64bit() == False and iolib.systemos() == 3:
appflavourinfo="a script running on a 32-bit interpreter on Unknown."
else:
appflavourinfo="an Unknown mode on an Unknown OS."
panel1=Frame(aboutwindow, padx=5, pady=5, width=str(windowwidth))
links=LabelFrame(aboutwindow, padx=5, pady=5, width=str(windowwidth), height=10, text="")
lbt_logo=PhotoImage(file=abouticon)
logolabel=Label(panel1, image=lbt_logo, text="")
about_name=Label(panel1, text=appname)
about_license=Label(panel1, text="This product '{}' , '{}'\n and '{}' are all under the GPLv3 license.\nClick the 'Legal info' button below for more infomation.".format(appname, conlib.appname, iolib.appname))
about_verinfo=Label(panel1, text="Version: {} ({})".format(version, release), width=str(windowwidth))
about_appbuildinfo=Label(panel1, text="Written by {} on the {}".format(author, buliddate))
about_appflavour=Label(panel1, text="Running as {}".format(appflavourinfo))
about_conlib=Label(panel1, text="{} {} ({}) by {}. \nBuilt on {}.".format(conlib.appname, conlib.version, conlib.release, conlib.author, conlib.buliddate), width=str(windowwidth))
about_iolib=Label(panel1, text="{} {} ({}) by {}. \nBuilt on {}.".format(iolib.appname, iolib.version, iolib.release, iolib.author, iolib.buliddate), width=str(windowwidth))
about_extrainfo=Label(panel1, text="Please report any bugs to the issues tracker on the\n Github repo and tell me what I can improve on this app.\n All feedback apperciated!", width=str(windowwidth))
btn1=Button(links, text="Legal info", command=lambda: createlicensewindow("Legal info", legalinfotext))
btn2=Button(links, text="Github", command=lambda: iolib.openinwebbrowser(githuburl))
btn3=Button(links, text="Website", command=lambda: iolib.openinwebbrowser(websiteurl))
btn4=Button(links, text="Check for updates", command=lambda: checkforupdates(False))
btn5=Button(links, text="Help", command=empty)
btn6=Button(links, text="Open source libraries", command=lambda: createlicensewindow("Open source", opensourcetext))
panel1.pack(padx=20, pady=5)
links.pack(padx=5, pady=5, side=BOTTOM)
logolabel.pack()
about_name.pack()
about_verinfo.pack()
about_license.pack()
about_appbuildinfo.pack()
about_appflavour.pack()
about_conlib.pack()
about_iolib.pack()
about_extrainfo.pack()
btn1.pack(side=LEFT, padx=1)
btn2.pack(side=LEFT, padx=1)
btn3.pack(side=LEFT, padx=1)
btn4.pack(side=LEFT, padx=1)
btn5.pack(side=LEFT, padx=1)
btn6.pack(side=LEFT, padx=1)
aboutwindow.mainloop()
print("Starting...")
# Main window for messageboxes.
root = Tk()
root.title("{}".format(appname))
try:
if systemos == 0:
root.iconbitmap(progicon)
else:
ico = PhotoImage(file=abouticon)
root.tk.call('wm', 'iconphoto', root._w, ico)
except Exception as e:
root.withdraw()
messagebox.showerror("Error", "Error while setting program icon\n {}.".format(str(e)))
root.withdraw()
setloggingoptions()
loadconfig()
checkforupdates()
createmainwindow()