Run the code for it to make sense, go to Appearance >> Font >> Size, No matter what, the font size turns to 48 and config.json has font size also equal to 48.
I tried logging Root.Size >> s in the function and and print the sizes while generating them in the for loop, here was the result: (generation) 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 (function call) (all cases) 48 (config.json) {"wordwrap": "char", "font": ["Helvetica", 48]}
code:
from tkinter import *
from tkinter import filedialog # , colorchooser, constants, ttk
# from typing import Literal, List
# import pickle as p
# import time as t
# import random as r
import json as js
# import math as m
# import sys, os
import tkinter.font
class Root:
def __init__(self):
self.root = Tk()
self.root.title('Deltae Text Editor')
self.root.iconbitmap('DeltaeTextEditorLogo.ico')
self.fonts = []
for name in sorted(tkinter.font.families()):
if name[0] != '@':
self.fonts.append(name)
self.text = Frame()
self.text.pack(fill=BOTH, expand=TRUE)
yScroll = Scrollbar(master=self.text, orient=VERTICAL)
yScroll.pack(side=RIGHT, fill=Y)
xScroll = Scrollbar(master=self.text, orient=HORIZONTAL)
xScroll.pack(side=BOTTOM, fill=X)
with open("config.json") as con:
self.config = js.load(con)
self.textbox = Text(master=self.text, font=self.config['font'], yscrollcommand=yScroll.set,
xscrollcommand=xScroll.set, wrap=self.config['wordwrap'], undo=TRUE)
self.textbox.pack(fill=BOTH, expand=TRUE)
yScroll.config(command=self.textbox.yview)
xScroll.config(command=self.textbox.xview)
self.menu = Menu(self.root)
self.root.config(menu=self.menu)
file_menu = Menu(self.menu, tearoff=False)
self.menu.add_cascade(label='File', menu=file_menu)
file_menu.add_command(label='Open', command=self.open)
file_menu.add_command(label='Save', command=self.save)
file_menu.add_command(label='Save as', command=self.save_as)
file_menu.add_command(label='New', command=self.new)
file_menu.add_separator()
file_menu.add_command(label='Exit', command=self.root.quit)
edit_menu = Menu(self.menu, tearoff=False)
self.menu.add_cascade(label='Edit', menu=edit_menu)
edit_menu.add_command(label='Cut', command=self.cut)
edit_menu.add_command(label='Copy', command=self.copy)
edit_menu.add_command(label='Paste', command=self.paste)
edit_menu.add_separator()
edit_menu.add_command(label='Undo', command=self.undo)
edit_menu.add_command(label='Redo', command=self.redo)
edit_menu = Menu(self.menu, tearoff=False)
self.menu.add_cascade(label='Appearance', menu=edit_menu)
Word = Menu(edit_menu, tearoff=False)
edit_menu.add_cascade(label='Wordwrap', menu=Word)
Word.add_radiobutton(label='None', command=self.none)
Word.add_radiobutton(label='Character', command=self.char)
Word.add_radiobutton(label='Word', command=self.word)
FontM = Menu(edit_menu, tearoff=False)
edit_menu.add_cascade(label='Font', menu=FontM)
SizeM = Menu(FontM, tearoff=False)
FontM.add_cascade(label='Size', menu=SizeM)
for size in range(8, 50, 2):
SizeM.add_radiobutton(label=str(size), command=lambda: self.Size(size))
FamilyM = Menu(FontM, tearoff=False)
FontM.add_cascade(label='Font Family', menu=FamilyM)
for font in self.fonts:
FamilyM.add_radiobutton(label=font, command=str(self.Family(font)))
self.file: str = ''
self.root.mainloop()
self.text = None
def open(self):
self.textbox.delete('1.0', END)
self.file = filedialog.askopenfilename(initialdir='/', title='Open File', filetypes=(('Text Files', '*.txt'),
('All Files', '*.*')))
try:
if self.file:
self.text = open(self.file, 'r')
self.textbox.insert(END, self.text.read())
except UnicodeDecodeError:
if self.file:
text = open(self.file, 'rb')
self.textbox.insert(END, str(text.read()))
finally:
self.text.close()
def save(self):
if self.file:
self.file = open(self.file, 'w')
self.file.write(self.textbox.get('1.0', END))
self.file.close()
else:
self.save_as()
def save_as(self):
self.file = filedialog.asksaveasfilename(initialdir='/', defaultextension='.*', title='save as', filetypes=((
'Text Files', '*.txt'), ('All Files', '*.*')))
if self.file:
try:
self.file = open(self.file, 'w')
self.file.write(self.textbox.get('1.0', END))
self.file.close()
except UnicodeEncodeError:
self.file = open(self.file, 'wb')
self.file.write(bytes(self.textbox.get('1.0', END)))
self.file.close()
def new(self):
self.file = ''
self.textbox.delete('1.0', END)
def cut(self):
pass
def copy(self):
pass
def paste(self):
pass
def undo(self):
pass
def redo(self):
pass
def none(self):
self.textbox.config(wrap=NONE)
with open('config.json', 'w') as con:
self.config['wordwrap'] = 'none'
js.dump(self.config, con)
def char(self):
self.textbox.config(wrap=CHAR)
with open('config.json', 'w') as con:
self.config['wordwrap'] = 'char'
js.dump(self.config, con)
def word(self):
self.textbox.config(wrap=WORD)
with open('config.json', 'w') as con:
self.config['wordwrap'] = 'word'
js.dump(self.config, con)
def Size(self, s: int):
# def Size(self, s: str):
# rewrite config to include s
# configure textbox s
# rewrite config json to include s
self.config['font'][1] = s
self.textbox.config(font=self.config['font'])
with open('config.json', 'w') as con:
js.dump(self.config, con)
def Family(self, f: str):
# def Size(self, f: str):
# rewrite config to include s
# configure textbox s
# rewrite config json to include s
pass
def main():
root = Root()
root.copyright = None
if __name__ == '__main__':
main()