python-3.x - 字体更改菜单按钮不会在 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()
解决方案
要将参数传递给命令,您必须使用 lambda 函数或部分对象或类似的。
font_menu.add_command(label="Times New Roman", command=lambda: change_font("Times New Roman"))
推荐阅读
- php - PHP - 防止站点镜像
- mysql - 如何根据 MySQL 中的最大日期仅选择左表的一条记录?
- javascript - 带有动态条目的 Webpack
- go - 通过 TCP 读取字节并在 Go 中编码为 ISO-8859-9
- security - PingFederate OpenId/OAUTH2.0 与 Weblogic 10.3.6 集成
- c# - 仅数字 0-9 的正则表达式 c# 接受特殊字符 \n 符号
- c# - 循环遍历列表框项目并针对每个项目触发操作
- microsoft-graph-api - 什么是“Microsoft Graph API”的 $Filter 查询中“替换多个 OR 语句”的“替代运算符”
- swift - 使用 firebase ios 电子邮件登录
- ruby-on-rails - 从 init.d 服务脚本启动 rails 并加载正确版本的 ruby