0

所以我创建了一个 Python 文件,用户可以在其中打开字数统计,它将显示字符数和单词数。启用后,它还会在该选项附近放置一个复选标记。每当用户单击该选项两次时,它都会删除复选标记。但是,它并没有停止计算单词和字符,我该怎么做呢?

这是字数统计功能的代码:

# Word Count Function
def DeclareWordCount():

    # Turn of Word Count if the User Unchecks the Option in the Tools Menu

    # Get data in textbox - turns into a string
    TextContent = TextBox.get("1.0", END)
    # String to number 
    CharactersInTextBox = len(TextContent)    
    WordsInTextBox = len(TextContent.split()) 
    # Config in Status Bar
    StatusBar.config(text=str(CharactersInTextBox-1) + " Characters, " + str(WordsInTextBox) + " Words, ")

def InitWordCount():
    DeclareWordCount()
    StatusBar.after(1, InitWordCount)

这是菜单的代码:

# Check Marks for Options in Tools Menu
WordWrap_CheckMark = BooleanVar()
WordWrap_CheckMark.set(False)

ToolsMenu = Menu(MenuBar, tearoff=False)
MenuBar.add_cascade(label="Tools", menu=ToolsMenu, underline=0)
ToolsMenu.add_checkbutton(label="Word Count", onvalue=1, offvalue=0, variable=WordCount_CheckMark, command=InitWordCount)

当复选标记不存在时,如何让状态栏停止显示字数和字符数?

4

1 回答 1

0

您应该执行字数统计并.after()仅在WordWrap_CheckMark设置为时调用True

def InitWordCount():
    if WordWrap_CheckMark.get():
        DeclareWordCount()
        StatusBar.after(1, InitWordCount)
    else:
        StatusBar.config(text="")

请注意,该行中有错字:

ToolsMenu.add_checkbutton(..., variable=WordCount_CheckMark, ...)

它应该是

ToolsMenu.add_checkbutton(..., variable=WordWrap_CheckMark, ...)
于 2021-03-10T09:15:15.373 回答