我正在尝试使用选项菜单更改按钮小部件的字体,以选择不同的字体和大小。我选择字体和大小并获取它们并将它们存储在元组newFont中。当我测试我的代码并打印元组时,我会根据选项菜单中选择的选项获得('Helvetica','13'),('Times','112')等。我对为什么按钮字体的字体和大小不会改变感到困惑。
from tkinter import *
def manipulateFont(*args):
newFont = (font.get(), fontSize.get())
return newFont
window = Tk()
open = Button(window, text="Open", command=open, font=manipulateFont)
"""Option Menu widget for font"""
font = StringVar(window)
font.set("Times")
font.trace("w", manipulateFont)
fontOptions = OptionMenu(window, font, "Arial", "Times", "Helvetica")
"""Option Menu widget for font size"""
fontSize = StringVar(window)
fontSize.set("12")
fontSize.trace("w", manipulateFont)
fontSizeOptions = OptionMenu(window, fontSize, "12", "13", "14", "15")
open.pack(side=TOP)
fontOptions.pack(side=TOP)
fontSizeOptions.pack(side=TOP)