import tkinter as tk
from tkinter import filedialog as tkfd
from tkinter import scrolledtext as tkst
def file_types():
file_exts = (('ini file', '*.ini'),
('all files', '*.*'))
return file_exts
def load_file():
opts_load = tkfd.askopenfilename(
title='Load Options File', filetypes=file_types())
try:
with open(opts_load, 'r', encoding='utf-8') as opts_file_inp:
opts_data = opts_file_inp.read().rstrip('\n').splitlines(True)
except FileNotFoundError:
return
for item in opts_data:
if item.startswith('[') and item.endswith(']\n'):
opts_item = item.replace('[', '', 1).replace(']', '', -1)
# .find returns the index number of a substring, -1 = not found
elif item.find(' = ') < item.find(' : ') and item.find(' = ') >= 0:
sep_char.set(' = ')
opts_item = item.replace(sep_char.get(), ' ', 1)
elif item.find(' : ') < item.find(' = ') and item.find(' : ') == -1:
sep_char.set(' = ')
opts_item = item.replace(sep_char.get(), ' ', 1)
else:
sep_char.set(' : ')
opts_item = item.replace(sep_char.get(), ' ', 1)
opts_tbox.insert('end', opts_item)
def save_file():
opts_list: list = []
opts_data = opts_tbox.get('1.0', 'end').rstrip('\n').splitlines(True)
for item in opts_data:
if ' ' in item:
opts_item = item.replace(' ', sep_char.get(), 1)
elif item != '\n':
if item.endswith('\n'):
# Add a square bracket to start + end of list item
opts_item = f'[{item[:-1]}]{item[-1:]}'
else:
opts_item = f'[{item}]'
else:
opts_item = item
opts_list.append(opts_item)
opts_save = tkfd.asksaveasfilename(
title='Save Options File', filetypes=file_types(), defaultextension='.ini')
try:
with open(opts_save, 'w', encoding='utf-8') as opts_file_out:
opts_file_out.writelines(opts_list)
except FileNotFoundError:
return
# Create main window
root = tk.Tk()
win_width = 1000
win_height = 500
# Position window on screen
horiz_pos = int(root.winfo_screenwidth()/2 - win_width/2)
vert_pos = int(root.winfo_screenheight()/2.5 - win_height/2)
root.geometry(f'{win_width}x{win_height}+{horiz_pos}+{vert_pos}')
root.title('Configuration File Creator')
sep_char = tk.StringVar()
sep_char.set(' = ')
menu_bar = tk.Menu(root)
menu_bar.config(font=('Arial', 11))
file_menu = tk.Menu(menu_bar, font=('Arial', 11), tearoff=0)
file_menu.add_command(label='Load File', command=load_file)
file_menu.add_command(label='Save File', command=save_file)
file_menu.add_command(label='Exit', command=root.destroy)
menu_bar.add_cascade(label='File', menu=file_menu)
seperator_menu = tk.Menu(menu_bar, font=('Arial', 11), tearoff=0)
seperator_menu.add_radiobutton(
label='Equals Sign', variable=sep_char, value=' = ')
seperator_menu.add_radiobutton(label='Colon', variable=sep_char, value=' : ')
menu_bar.add_cascade(label='Seperator', menu=seperator_menu)
root.config(bg='#00004d', menu=menu_bar)
# Configure the grid layout
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
opts_tbox = tkst.ScrolledText(root, width=79, height=20, relief='flat')
opts_tbox.config(bg='#00004d', fg='#ffffff', font=(
'Arial', 14), insertbackground='#ffffff')
opts_tbox.grid(column=0, row=0, padx=10, pady=5, sticky='nsew')
opts_tbox.focus_set()
# Loop until user exits program
root.mainloop()