# -*- coding: utf-8 -*- import tkinter as tk import tkinter.ttk as ttk from tkinter import font from tkinter import filedialog root = tk.Tk() root.title("tk34.py") ##################################### def insert(event): #text.config(state = tk.NORMAL) text.insert(tk.END, entry.get()+"\n") entry.delete(0, tk.END) #text.config(state = tk.DISABLED) def bold(event): text.tag_add('b1', tk.SEL_FIRST, tk.SEL_LAST) newfont = font.Font(weight = 'bold', slant = 'italic') text.tag_config('b1', font = newfont) def italic(event): text.tag_add('i1', tk.SEL_FIRST, tk.SEL_LAST) newfont = font.Font(slant = 'italic') text.tag_config('i1', font = newfont) def underline(event): text.tag_add('u1', tk.SEL_FIRST, tk.SEL_LAST) newfont = font.Font(underline = 1) text.tag_config('u1', font = newfont) def overstrike(event): text.tag_add('o1', tk.SEL_FIRST, tk.SEL_LAST) newfont = font.Font(overstrike = 1) text.tag_config('o1', font = newfont) def size(event, s): fs = [10,12,14,16,20,24,36] for i in fs: if i==s: text.tag_add('s'+str(i), tk.SEL_FIRST, tk.SEL_LAST) newfont = font.Font(size = s) text.tag_config('s'+str(i), font = newfont) break def newfile(): text.delete(0.0, tk.END) def savefile(): filename = filedialog.asksaveasfilename(title = "Save") try: file = open(filename, 'w', encoding = "utf-8") file.write(text.get(0.0, tk.END)) except: pass finally: file.close() def openfile(): filename = filedialog.askopenfilename(title = 'Open') try: with open(filename, 'r', encoding = "utf-8") as file: content = file.read() text.delete(0.0, tk.END) text.insert(0.0, content) except: pass def exitprogram(): root.destroy() # ------------------------------------------------------- bFrame = ttk.Frame(root) bFrame.pack(side = tk.TOP) # --------- about tag --------- # ttk.Style().configure('a.TButton', width = 3) bBold = ttk.Button(bFrame, text = "B", style = 'a.TButton') bBold.grid(row = 0, column = 0) bItalic = ttk.Button(bFrame, text = 'I', style = 'a.TButton') bItalic.grid(row = 0, column = 1) bUnderline = ttk.Button(bFrame, text = 'U', style = 'a.TButton') bUnderline.grid(row = 0, column = 2) bOverstrike = ttk.Button(bFrame, text = 'O', style = 'a.TButton') bOverstrike.grid(row = 0, column = 3) # Button for chinging size bSize = ttk.Menubutton(bFrame, text = 'S', style = 'a.TButton') bSize.menu = tk.Menu(bSize, tearoff = 0) bSize['menu'] = bSize.menu bSize.menu.add_command(label = '10', command = lambda: size("<1>",10)) bSize.menu.add_command(label = '12', command = lambda: size("<1>",12)) bSize.menu.add_command(label = '14', command = lambda: size("<1>",14)) bSize.menu.add_command(label = '16', command = lambda: size("<1>",16)) bSize.menu.add_command(label = '20', command = lambda: size("<1>",20)) bSize.menu.add_command(label = '24', command = lambda: size("<1>",24)) bSize.menu.add_command(label = '36', command = lambda: size("<1>",36)) bSize.grid(row = 0, column = 4) bBold.bind("<1>", bold) bItalic.bind("<1>", italic) bUnderline.bind("<1>", underline) bOverstrike.bind("<1>", overstrike) ## north Frame -------------------- north = ttk.Frame(root) north.pack(side = tk.TOP, fill = tk.BOTH, expand = True) text = tk.Text(north, width = 50, height = 20) text.pack(side = tk.LEFT, fill = tk.BOTH, expand = True) # Scrollbar sc = ttk.Scrollbar(north) sc.pack(side = tk.LEFT, fill = tk.BOTH) sc.config(command = text.yview) text.config(yscrollcommand = sc.set) ## south Frame south = ttk.Frame(root) south.pack(fill = tk.BOTH, expand = True) entry = ttk.Entry(south) btn = ttk.Button(south, text = 'Submit') entry.pack(side = tk.LEFT, fill = tk.X, expand = True) btn.pack(side = tk.LEFT, fill=tk.X) btn.bind("<1>", insert) entry.bind("", insert) """ text.insert(tk.END, "Hi, there.......\n") text.insert(tk.END, "See ya...") text.tag_add('t1', '1.1', '1.10') text.tag_config('t1', background = 'lightgreen', font = 'Harriton 14 bold italic') """ menubar = tk.Menu(root) menu_file = tk.Menu(menubar, tearoff = 0) menu_file.add_command(label = 'New', command = newfile) menu_file.add_command(label = 'Open', command = openfile) menu_file.add_command(label = 'Save', command = savefile) menu_file.add_command(label = 'Exit', command = exitprogram) menubar.add_cascade(label = 'File', menu = menu_file) root.config(menu = menubar) ##################################### root.mainloop()