首页 > 解决方案 > 字体更改菜单按钮不会在 tkinter 文本编辑器中切换字体

问题描述

在这里的第一篇文章,我正在开发一个非常基本的文本编辑器,作为 python 中的初学者项目,我已经设法让它的大部分工作,但我只是不知道如何制作字体和字体大小菜单按钮工作。起初,我只有一个按钮,可以将文本切换为大小为 12 的 Times New Roman,而不是默认的 Tkinter 字体,这很有效,但是当我添加了独立更改字体和字体大小的功能时,它只会默认为 Calibri 大小 12(它们都是代码中添加的最后一个按钮),我无法更改字体或大小。有任何想法吗?

问题代码:

def change_font(fontname):
    global font
    font = fontname
    text_info.configure(font=(font, font_size))

def change_font_size(size):
    global font_size
    font_size = size
    text_info.configure(font=(font, font_size))

font_menu = Menu(menubar, tearoff=0)
font_menu.add_command(label="Times New Roman", command=change_font("Times New Roman"))
font_menu.add_command(label="Calibri", command=change_font("Calibri"))
menubar.add_cascade(label="Font", menu=font_menu)

font_size_menu = Menu(menubar, tearoff=0)
for i in range(8, 13):
    font_size_menu.add_command(label=str(i), command=change_font_size(i))
menubar.add_cascade(label="Font Size", menu=font_size_menu)

完整代码:

from tkinter import *

def text_opener():
    try:
        f = open(text_name, "r+")
    except FileNotFoundError:
        f = open(text_name, "a+")
        f.close()
        f = open(text_name, "r+")
    return f

text_name = input("What is the name of your .txt file? ") + ".txt"
text = text_opener()
root = Tk()

# intended default font and font size
font = "Arial"
font_size = 11

root.title("Text Editor")
root.geometry("640x360")
root.resizable(width=False, height=False)
scrollbar = Scrollbar(root)
scrollbar.pack(side=RIGHT, fill=Y)
text_info = Text(root, yscrollcommand=scrollbar.set, wrap=WORD)
scrollbar.config(command=text_info.yview)
text_info.pack(fill=BOTH)
text_info.insert(INSERT, text.read())
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)

def save():
    text.seek(0)
    text.write(text_info.get("1.0", 'end-1c'))
    text.truncate()

filemenu.add_command(label="Save", command=save)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)

# problem code below
def change_font(fontname):
    global font
    font = fontname
    text_info.configure(font=(font, font_size))

def change_font_size(size):
    global font_size
    font_size = size
    text_info.configure(font=(font, font_size))

font_menu = Menu(menubar, tearoff=0)
font_menu.add_command(label="Times New Roman", command=change_font("Times New Roman"))
font_menu.add_command(label="Calibri", command=change_font("Calibri"))
menubar.add_cascade(label="Font", menu=font_menu)

font_size_menu = Menu(menubar, tearoff=0)
for i in range(8, 13):
    font_size_menu.add_command(label=str(i), command=change_font_size(i))
menubar.add_cascade(label="Font Size", menu=font_size_menu)

root.config(menu=menubar)
root.lift()
root.attributes('-topmost', True)
root.after_idle(root.attributes, '-topmost', False)
root.mainloop()
text.close()

标签: python-3.xtkinterfonts

解决方案


要将参数传递给命令,您必须使用 lambda 函数或部分对象或类似的。

font_menu.add_command(label="Times New Roman", command=lambda: change_font("Times New Roman"))

推荐阅读