[英]How do I create font style and font size in my notepad?
I have created a notepad using python. I want to create a feature which can change the font size and also the font style.我使用 python 创建了一个记事本。我想创建一个可以更改字体大小和字体样式的功能。 I have tried various options but they have failed.
我尝试了各种选择,但都失败了。 My notepad is fully made up with python's tkinter module.
我的记事本完全由 python 的 tkinter 模块组成。 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()
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.创建 fonts、colors 和各种 styles 是通过创建标签名称并为其定义标签属性,然后在将文本插入
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()
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.
它使用 tkinter.font class 更新后,将在使用该类型字体的任何地方更改文本。
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.如果我自己编写此应用程序,我会使用更多面向 object 的方法,但您的方法有效。
The same approach that I have used to change the font size, will also work for the font family.我用来更改字体大小的相同方法也适用于字体系列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.