I have created a notepad using python. I want to create a feature which can change the font size and also the font style. I have tried various options but they have failed. My notepad is fully made up with python's tkinter module. I have also tried methods like file handling but it doesn't work. Please help me out. Here is the code:
from tkinter import *
import tkinter.messagebox as mb
from tkinter.filedialog import askopenfilename, asksaveasfilename
import os
def newFile():
global file
root.title("Untitled - Notepad")
file = None
textArea.delete(1.0, END)
def openFile():
global file
file = askopenfilename(defaultextension=".txt", filetypes=[("All Files", "*.*"), ("Text Documents", "*.txt")])
if file == "":
file = None
else:
root.title(os.path.basename(file) " - Notepad")
textArea.delete(1.0, END)
f = open(file)
textArea.insert(1.0, f.read())
f.close()
def save():
global file
if file == None:
file = asksaveasfilename(initialfile="Untitled.txt", defaultextension=".txt", filetypes=[("All Files", "*.*"), ("Text Documents", "*.txt")])
if file == "":
file = None
else:
f = open(file, "w")
f.write(textArea.get(1.0, END))
f.close()
root.title(os.path.basename(file) " - Notepad")
else:
f = open(file, "w")
f.write(textArea.get(1.0, END))
f.close()
def quitFile():
root.destroy()
def cut():
textArea.event_generate(("<<Cut>>"))
def copy():
textArea.event_generate(("<<Copy>>"))
def paste():
textArea.event_generate(("<<Paste>>"))
def changeStyle():
pass
def info():
mb.showinfo("About Notepad", '''Notepad
Version - 1.1.1
Developer - Sourabh Sontakke''')
def changeSize():
f = open('size.txt', 'w')
f.write(str(size.get()))
f.close()
print(size.get())
def changeSizeWindow():
global size
TextSize = Tk()
TextSize.geometry("400x300")
TextSize.title("Change Size")
size = StringVar()
Label(TextSize, text="Enter the font size you want:", font="lucida 15 bold").pack()
Entry(TextSize, textvariable=size, font="lucida 15").pack(padx=40)
Button(TextSize, text="Apply", command=changeSize).pack()
TextSize.mainloop()
if __name__ == "__main__":
root = Tk()
root.title("Untitled - Notepad")
root.geometry("600x400")
ScrollBar = Scrollbar(root)
ScrollBar.pack(fill=Y, side=RIGHT)
a = 20
textArea = Text(root, font=f"lucida {a}", yscrollcommand=ScrollBar.set)
file = None
textArea.pack(expand=True,fill="both")
ScrollBar.config(command=textArea.yview)
MenuBar = Menu(root)
FileMenu = Menu(MenuBar, tearoff=0)
FileMenu.add_command(label="New File", command=newFile)
FileMenu.add_command(label="Open File", command=openFile)
FileMenu.add_command(label="Save", command=save)
FileMenu.add_separator()
FileMenu.add_command(label="Quit", command=quitFile)
MenuBar.add_cascade(label="File", menu=FileMenu)
EditMenu = Menu(MenuBar, tearoff=0)
EditMenu.add_command(label="Cut", command=cut)
EditMenu.add_command(label="Copy", command=copy)
EditMenu.add_command(label="Paste", command=paste)
EditMenu.add_command(label="Font Size", command=changeSizeWindow)
EditMenu.add_command(label="Font Style", command=paste)
MenuBar.add_cascade(label="Edit", menu=EditMenu)
HelpMenu = Menu(MenuBar, tearoff=0)
HelpMenu.add_command(label="About", command=info)
MenuBar.add_cascade(label="Help", menu=HelpMenu)
root.config(menu=MenuBar)
root.mainloop()
CodePudding user response:
Creating fonts, colors with various styles is achieved by creating tag names and defining tag attributes to them, then using those tag names when inserting text into Text
object.
Here is an example.
import tkinter as tk
master = tk.Tk()
text = tk.Text(master, undo = 1)
text.grid(row = 0, column = 0, sticky = tk.NSEW)
#
# make tag name with any letter or word
# Use tag_config to define all required attributes
# Use tag_add to create it
# Apply tag name with insert(pos, message, tag) format
#
text.tag_config( "A", font = "Consolas 12 italic", foreground = "red")
text.tag_add("A", "end")
text.insert( "end", "This is in Font consolas 12 italic. color = red\n", "A" )
text.tag_config( "B", font = "Consolas 12 overstrike", foreground = "Blue")
text.tag_add("B", "end")
text.insert( "end", "This is in Font consolas 12 overstrike. color = blue\n", "B" )
text.tag_config( "C", font = "Times 20 bold", foreground = "green")
text.tag_add("C", "end")
text.insert( "end", "This is in Font Times 20 bold. color = green\n", "C" )
text.tag_config( "D", font = "Times 20 normal", foreground = "black", background = "yellow")
text.tag_add("D", "end")
# Using some of the thousands of characters in python fonts
for a in ["♔", "♕", "♖", "♗", "♘", "♙", "♚", "♛", "♜", "♝", "♞", "♟"]:
text.insert( "end", a, "D")
master.mainloop()
CodePudding user response:
The below modification of your code shows how to change the font of your notepad application. It uses the tkinter.font class which when updated, will change the text everywhere a font of that type has been used.
from tkinter import *
import tkinter.messagebox as mb
from tkinter.filedialog import askopenfilename, asksaveasfilename
from tkinter.font import Font
import os
font_size = 12
font_family = 'lucida'
def newFile():
global file
root.title("Untitled - Notepad")
file = None
textArea.delete(1.0, END)
def openFile():
global file
file = askopenfilename(defaultextension=".txt", filetypes=[("All Files", "*.*"), ("Text Documents", "*.txt")])
if file == "":
file = None
else:
root.title(os.path.basename(file) " - Notepad")
textArea.delete(1.0, END)
f = open(file)
textArea.insert(1.0, f.read())
f.close()
def save():
global file
if file == None:
file = asksaveasfilename(initialfile="Untitled.txt", defaultextension=".txt", filetypes=[("All Files", "*.*"), ("Text Documents", "*.txt")])
if file == "":
file = None
else:
f = open(file, "w")
f.write(textArea.get(1.0, END))
f.close()
root.title(os.path.basename(file) " - Notepad")
else:
f = open(file, "w")
f.write(textArea.get(1.0, END))
f.close()
def quitFile():
root.destroy()
def cut():
textArea.event_generate(("<<Cut>>"))
def copy():
textArea.event_generate(("<<Copy>>"))
def paste():
textArea.event_generate(("<<Paste>>"))
def changeStyle():
pass
def info():
mb.showinfo("About Notepad", '''Notepad
Version - 1.1.1
Developer - Sourabh Sontakke''')
def changeSizeWindow():
def changeSize():
global font_size
font_size = size.get()
NotePad_font.configure(size=font_size)
TextSize.destroy()
TextSize = Toplevel()
TextSize.geometry("400x300")
TextSize.title("Change Size")
size = StringVar()
size.set(NotePad_font.cget('size'))
Label(TextSize, text="Enter the font size you want:", font="lucida 15 bold").pack()
Entry(TextSize, textvariable=size, font="lucida 15").pack(padx=40)
Button(TextSize, text="Apply", command=changeSize).pack()
def changeSize():
changeSizeWindow()
if __name__ == "__main__":
root = Tk()
root.title("Untitled - Notepad")
root.geometry("600x400")
NotePad_font = Font(root,family=font_family,size=font_size)
ScrollBar = Scrollbar(root)
ScrollBar.pack(fill=Y, side=RIGHT)
textArea = Text(root, font=NotePad_font, yscrollcommand=ScrollBar.set)
file = None
textArea.pack(expand=True,fill="both")
ScrollBar.config(command=textArea.yview)
MenuBar = Menu(root)
FileMenu = Menu(MenuBar, tearoff=0)
FileMenu.add_command(label="New File", command=newFile)
FileMenu.add_command(label="Open File", command=openFile)
FileMenu.add_command(label="Save", command=save)
FileMenu.add_separator()
FileMenu.add_command(label="Quit", command=quitFile)
MenuBar.add_cascade(label="File", menu=FileMenu)
EditMenu = Menu(MenuBar, tearoff=0)
EditMenu.add_command(label="Cut", command=cut)
EditMenu.add_command(label="Copy", command=copy)
EditMenu.add_command(label="Paste", command=paste)
EditMenu.add_command(label="Font Size", command=changeSize)
EditMenu.add_command(label="Font Style", command=paste)
MenuBar.add_cascade(label="Edit", menu=EditMenu)
HelpMenu = Menu(MenuBar, tearoff=0)
HelpMenu.add_command(label="About", command=info)
MenuBar.add_cascade(label="Help", menu=HelpMenu)
root.config(menu=MenuBar)
root.mainloop()
If I were to write this application myself, I'd use a more object oriented approach but your method works.
The same approach that I have used to change the font size, will also work for the font family.