I have a checkbox toolbar menu option in tkinter. Whenever, I click on the option, it enables word wrap and puts a check mark next to it.
# Toggle Word Wrap Function
def ToggleWordWrap(*args):
# If there is no word wrap then add word wrap
if TextBox.cget("wrap") == "none":
TextBox.configure(wrap="word")
# If there is word wrap then take out word wrap
elif TextBox.cget("wrap") == "word":
TextBox.configure(wrap="none")
# Check Marks for Options in Tools Menu
WordWrap_CheckMark = BooleanVar()
WordWrap_CheckMark.set(False)
# Tools Option for Menu Bar
ToolsOption = Menu(MenuBar, tearoff=False)
MenuBar.add_cascade(label="Tools", menu=ToolsOption, underline=0)
ToolsOption.add_command(label="Word Count")
ToolsOption.add_checkbutton(label="Toggle Word Wrap", onvalue=1, offvalue=0, variable=WordWrap_CheckMark, command=ToggleWordWrap)
I also decided that I should add a keyboard binding Alt-Z to the function.
# Toggle Word Wrap Function
def ToggleWordWrap(*args):
# If there is no word wrap then add word wrap
if TextBox.cget("wrap") == "none":
TextBox.configure(wrap="word")
# If there is word wrap then take out word wrap
elif TextBox.cget("wrap") == "word":
TextBox.configure(wrap="none")
root.bind("<Alt-Key-z>", ToggleWordWrap)
# Check Marks for Options in Tools Menu
WordWrap_CheckMark = BooleanVar()
WordWrap_CheckMark.set(False)
# Tools Option for Menu Bar
ToolsOption = Menu(MenuBar, tearoff=False)
MenuBar.add_cascade(label="Tools", menu=ToolsOption, underline=0)
ToolsOption.add_command(label="Word Count")
ToolsOption.add_checkbutton(label="Toggle Word Wrap", onvalue=1, offvalue=0, variable=WordWrap_CheckMark, command=ToggleWordWrap, accelerator="Alt-Z")
Whenever I use the keyboard binding, it does not turn the check mark on. How would I fix this?