from tkinter import *
import tkinter.filedialog
from tkinter import ttk
class TextEditor:
@staticmethod
def quit_app(event=None):
root.quit()
def change_font(self, event=None):
print(text_font.get())
def open_file(self, event=None):
txt_file = tkinter.filedialog.askopenfilename(parent=root, initialdir="C:/Users/Cesar/PycharmProjects")
if txt_file:
self.text_area.delete(1.0, END)
with open(txt_file) as _file:
self.text_area.insert(1.0, _file.read())
root.updata_idletasks()
def save_file(self, event=None):
file = tkinter.filedialog.asksaveasfile(mode='w')
if file != None:
data = self.text_area.get(1.0, END + '-1c')
file.write(data)
file.close()
def __init__(self, root):
self.text_to_write = ""
root.title("Text Editor")
root.geometry("600x500")
frame = Frame(root, width=600, height=500)
scrollbar = Scrollbar(frame)
self.text_area = Text(frame, width=600, height=500, yscrollcommand=scrollbar.set, padx=10, pady=10)
scrollbar.config(command=self.text_area.yview)
scrollbar.pack(side=RIGHT, fill="y")
self.text_area.pack(side="left", fill="both", expand=True)
frame.pack()
the_menu = Menu(root)
# ---------- file menu -------------
file_menu = Menu(the_menu, tearoff=0)
file_menu.add_command(label="Open",command=self.open_file)
file_menu.add_command(label="Save", command=self.save_file)
file_menu.add_separator()
file_menu.add_command(label="Quit", command=self.quit_app())
the_menu.add_cascade(label="File", menu=file_menu)
# ---------- format menu and font menu--------
"""
font_menu = Menu(the_menu, tearoff=0)
text_font = StringVar()
text_font.set("Times")
def change_font():
style = ttk.Style()
style.configure(self.text_area, font = text_font)
print("Font picked: ", text_font.get())
font_menu = Menu(the_menu, tearoff=0)
font_menu.add_radiobutton(label="Times", variable=text_font, command=change_font)
font_menu.add_radiobutton(label="Arial", variable=text_font, command=change_font)
font_menu.add_radiobutton(label="Consoles", variable=text_font, command=change_font)
font_menu.add_radiobutton(label="Courier", variable=text_font, command=change_font)
font_menu.add_radiobutton(label="Tahoma", variable=text_font, command=change_font)
the_menu.add_cascade(label="Fonts", menu=font_menu)
"""
root.config(menu=the_menu)
root = Tk()
text_edit = TextEditor(root)
root.mainloop()
我将字体菜单的代码放在注释中以确保程序正常工作
答案 0 :(得分:0)
ttk.Style
用于设置来自ttk的对象的样式,Text
不是。{1}}。要在tkinter.Text
小部件上设置字体,您需要使用tkinter.font.Font
。
import tkinter.font
#...
def __init__(self):
# ...
# ---------- format menu and font menu--------
self.text_font = StringVar()
self.text_font.set("Times")
font_menu = Menu(the_menu, tearoff=0)
self.fonts = {}
for font in ("Times","Arial", "Consoles", "Courier", "Tahoma"):
self.fonts[font] = tkinter.font.Font(font=font)
font_menu.add_radiobutton(label=font, variable=self.text_font, command=self.change_font)
the_menu.add_cascade(label="Fonts", menu=font_menu)
root.config(menu=the_menu)
def change_font(self):
self.text_area.config(font = self.fonts[self.text_font.get()])
BTW滚动的Text小部件内置在ScrolledText小部件的tkinter中。