Програма:
import tkinter as tk
def file_new():
save_or_not = tk.Tk()
save_or_not.geometry("150x70+400+250")
save_or_not.resizable(False, False)
save_or_not.grid_columnconfigure(0, minsize=75)
save_or_not.grid_columnconfigure(1, minsize=75)
saving_label = tk.Label(save_or_not, text="Save file?")
saving_label.grid(columnspan=2)
def without_saving():
save_or_not.destroy()
global text
text.delete('1.0', tk.END)
def saving():
file_save()
save_or_not.destroy()
global text
text.delete('1.0', tk.END)
yes_button = tk.Button(save_or_not, text="Yes", width=8, command=saving)
no_button = tk.Button(save_or_not, text="No", width=8, command=without_saving)
yes_button.grid(column=0, row=1)
no_button.grid(column=1, row=1)
def file_open():
file_name = filedialog.askopenfilename(initialdir='/',
title='Open file',
filetypes=(('Text Documents', '*.txt'),
('allfiles', '*.*')))
if file_name:
with open(file_name, 'r') as f:
text_open = f.read()
if text_open != tk.NONE:
text.delete(1.0, tk.END)
text.insert(tk.END, text_open)
else:
text.delete(1.0, tk.END)
def file_save():
file_name = filedialog.asksaveasfilename(initialdir='/',
title='Select file',
filetypes=(('Text Documents', '*.txt'), ('allfiles', '*.*')))
if file_name:
with open(file_name + ".txt", 'w') as f:
text_save = str(text.get(1.0, tk.END))
f.write(text_save + '\n')
def file_exit():
root.destroy()
def help_function():
help_window = tk.Tk()
help_window.geometry("300x70+400+250")
help_window.resizable(False, False)
help_label = tk.Label(help_window, text="Link to instructions\nhttps://www.wikihow.com/Use-Notepad")
help_label.pack()
def back():
help_window.destroy()
back_button = tk.Button(help_window, text="Back", command=back, width=10)
back_button.pack()
def about():
about_window = tk.Tk()
about_window.geometry("300x70+400+250")
about_window.resizable(False, False)
help_label = tk.Label(about_window, text="ItStep\nThanks for using!")
help_label.pack()
def back():
about_window.destroy()
back_button = tk.Button(about_window, text="Back", command=back, width=10)
back_button.pack()
def setting():
root = tk.Tk()
root.geometry("600x400+200+100")
root.title("Magician's diary")
root.iconbitmap("Note.ico")
root.minsize(200,100)
root.maxsize(1920,1080)
menu= tk.Menu(root)
root.config(menu=menu)
file_menu = tk.Menu(menu, tearoff=0)
file_menu.add_command(label='New', command=file_new)
file_menu.add_command(label='Open', command=file_open)
file_menu.add_command(label='Save as', command=file_save)
file_menu.add_command(label='Exit', command=file_exit)
menu.add_cascade(label='File', menu=file_menu)
help_menu = tk.Menu(menu, tearoff=0)
help_menu.add_command(label='Help', command= help_function)
help_menu.add_command(label='About', command=about)
menu.add_cascade(label='Help', menu=help_menu)
root.mainloop()
Answers & Comments
import tkinter as tk
from tkinter import filedialog
from tkinter import ttk
def file_new():
save_or_not = tk.Toplevel(root)
save_or_not.geometry("150x70+400+250")
save_or_not.resizable(False, False)
save_or_not.grid_columnconfigure(0, minsize=75)
save_or_not.grid_columnconfigure(1, minsize=75)
saving_label = tk.Label(save_or_not, text="Save file?")
saving_label.grid(columnspan=2)
def without_saving():
save_or_not.destroy()
text.delete('1.0', tk.END)
def saving():
file_save()
save_or_not.destroy()
text.delete('1.0', tk.END)
yes_button = tk.Button(save_or_not, text="Yes", width=8, command=saving)
no_button = tk.Button(save_or_not, text="No", width=8, command=without_saving)
yes_button.grid(column=0, row=1)
no_button.grid(column=1, row=1)
def file_open():
file_name = filedialog.askopenfilename(initialdir='/', title='Open file', filetypes=(('Text Documents', '*.txt'), ('all files', '*.*')))
if file_name:
with open(file_name, 'r') as f:
text_open = f.read()
if text_open != tk.NONE:
text.delete(1.0, tk.END)
text.insert(tk.END, text_open)
else:
text.delete(1.0, tk.END)
def file_save():
file_name = filedialog.asksaveasfilename(initialdir='/', title='Select file', filetypes=(('Text Documents', '*.txt'), ('all files', '*.*')))
if file_name:
with open(file_name + ".txt", 'w') as f:
text_save = str(text.get(1.0, tk.END))
f.write(text_save + '\n')
def file_exit():
root.destroy()
def help_function():
help_window = tk.Toplevel(root)
help_window.geometry("300x70+400+250")
help_window.resizable(False, False)
help_label = tk.Label(help_window, text="Link to instructions\nhttps://www.wikihow.com/Use-Notepad")
help_label.pack()
def back():
help_window.destroy()
back_button = tk.Button(help_window, text="Back", command=back, width=10)
back_button.pack()
def about():
about_window = tk.Toplevel(root)
about_window.geometry("300x70+400+250")
about_window.resizable(False, False)
help_label = tk.Label(about_window, text="ItStep\nThanks for using!")
help_label.pack()
def back():
about_window.destroy()
back_button = tk.Button(about_window, text="Back", command=back, width=10)
back_button.pack()
def open_settings():
def apply_settings():
font = font_combobox.get()
size = size_combobox.get()
style = style_combobox.get()
text.config(font=(font, size, style))
settings_window.destroy()
settings_window = tk.Toplevel(root)
settings_window.geometry("300x200+400+250")
settings_window.resizable(False, False)
font_label = tk.Label(settings_window, text="Font:")
font_label.pack()
font_combobox = ttk.Combobox(settings_window, values=["Calibri", "Arial", "Comic Sans MS", "Tahoma", "Verdana"])
font_combobox.pack()
size_label = tk.Label(settings_window, text="Size:")
size_label.pack()
size_combobox = ttk.Combobox(settings_window, values=["8", "10", "12", "14", "16"])
size_combobox.pack()
style_label = tk.Label(settings_window, text="Style:")
style_label.pack()
style_combobox = ttk.Combobox(settings_window, values=["normal", "bold", "italic"])
style_combobox.pack()
apply_button = tk.Button(settings_window, text="Apply", command=apply_settings)
apply_button.pack()
root = tk.Tk()
root.geometry("600x400+200+100")
root.title("Magician's diary")
# root.iconbitmap("Note.ico") # Поміняйте на шлях до власного файлу значка
root.minsize(200, 100)
root.maxsize(1920, 1080)
menu = tk.Menu(root)
root.config(menu=menu)
file_menu = tk.Menu(menu, tearoff=0)
file_menu.add_command(label='New', command=file_new)
file_menu.add_command(label='Open', command=file_open)
file_menu.add_command(label='Save as', command=file_save)
file_menu.add_command(label='Exit', command=file_exit)
menu.add_cascade(label='File', menu=file_menu)
settings_menu = tk.Menu(menu, tearoff=0)
settings_menu.add_command(label='Font Settings', command=open_settings)
menu.add_cascade(label='Settings', menu=settings_menu)
help_menu = tk.Menu(menu, tearoff=0)
help_menu.add_command(label='Help', command=help_function)
help_menu.add_command(label='About', command=about)
menu.add_cascade(label='Help', menu=help_menu)
text = tk.Text(root)
text.pack(fill=tk.BOTH, expand=True)
root.mainloop()
Будь ласка, постав найкращу відповідь