Блокнот
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
theme_colors = {
'dark':{
'text_bg':"#343D46",
'text_fg':"#ffffff",
'cursor': "#EDA756",
'select_bg':"#4E5A65"
},
'light':{
'text_bg': "#ffffff",
'text_fg': "#000000",
'cursor': "#8000ff",
'select_bg': "#777777"
}
}
def changeTheme(theme):
t['bg'] = theme_colors[theme]['text_bg']
t['fg'] = theme_colors[theme]['text_fg']
t['insertbackground'] = theme_colors[theme]['cursor']
t['selectbackground'] = theme_colors[theme]['select_bg']
def aboutProgramm():
mes = '''
Данный блокнот моя личная разработка, в ней отрабатываются навыки использования языка python
'''
messagebox.showinfo(title='О программе', message=mes)
def customQuit():
response = messagebox.askyesno(title='Выход', message='Закрыть программу')
if response:
root.destroy()
def openFile():
file_pass = filedialog.askopenfilename(
title = "Выбрать файл",
filetypes = (("текстовые документы","*.txt"),("все файлы","*.*")),
initialdir="D:\\docker\\python\\app2\\files"
)
if file_pass:
t.delete(1.0, END)
file = open(file_pass, encoding='utf-8')
text = file.read()
t.insert('1.0', text)
def saveFile():
file_pass = filedialog.asksaveasfilename(
filetypes = (
("текстовые документы (*.txt)", "*.txt"), ("все файлы", "*.*")
)
)
f = open(file_pass,'w', encoding='utf-8')
text = t.get('1.0', END)
f.write(text)
f.close()
root = Tk()
root.geometry('400x400')
main_menu = Menu(root)
root.config(menu=main_menu)
file_menu = Menu(main_menu, tearoff=0)
file_menu.add_command(label='Открыть', command=openFile)
file_menu.add_command(label='Сохранить', command=saveFile)
file_menu.add_separator()
file_menu.add_command(label='Выход', command=customQuit)
main_menu.add_cascade(label='File', menu=file_menu)
theme_menu = Menu(main_menu, tearoff=0)
theme_menu_sub = Menu(theme_menu, tearoff=0)
theme_menu_sub.add_command(label='Светлая тема', command = lambda : changeTheme('light'))
theme_menu_sub.add_command(label='Темная тема', command = lambda : changeTheme('dark'))
theme_menu.add_cascade(label='Темы', menu=theme_menu_sub)
theme_menu.add_command(label='О программе', command=aboutProgramm)
main_menu.add_cascade(label='Разное', menu=theme_menu)
f_text = Frame(root)
f_text.pack(fill=BOTH, expand=1)
t = Text(f_text,
bg=theme_colors['dark']['text_bg'],
fg=theme_colors['dark']['text_fg'],
padx=10,
pady=10,
wrap=WORD,
insertbackground=theme_colors['dark']['cursor'],
selectbackground=theme_colors['dark']['select_bg'],
font=("Courier New", 12),
spacing3=10,
width=30)
t.pack(fill=BOTH, expand=1, side=LEFT)
scroll = Scrollbar(f_text, command=t.yview)
scroll.pack(fill=Y, side=LEFT)
t.config(yscrollcommand=scroll.set)
root.mainloop()
