Инструменты пользователя

Инструменты сайта


python:application:bloknot

Блокнот

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
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,  # отступ по оси x
         pady=10,  # отступ по оси y
         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)  # Создаем скролл по оси y
scroll.pack(fill=Y, side=LEFT)
t.config(yscrollcommand=scroll.set)
 
root.mainloop()

python/application/bloknot.txt · Последние изменения: 2023/01/12 12:18 (внешнее изменение)