本文介绍了如何删除文本小部件 tkinter 中的最后一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使以下代码工作:它的目的是删除名为 _textBox 的文本小部件的最后一个字符(作为键盘上的退格通常会这样做)...

I am attempting to make the following code work:It is meant to delete the last character of the text widget named _textBox (as a backspace on a keyboard would usually do)...

def addChar(_textBox, char):
    global charCount
    if charCount <= 15:
        if char == "backSpace":
            _textBox.delete(charCount, END)
            if charCount != 0:
                charCount = charCount - 1
        else:
            _textBox.insert(END, char)
            charCount = charCount + 1
    print(charCount)

似乎唯一的问题是代码的.delete()"部分...

It seems the only problem is the '.delete()' part of the code...

有谁知道我如何正确使用它来删除文本小部件中仅最后一个字符?

Does anybody know how i can use this properly to remove only the last character in the text widget?

提前致谢:)

推荐答案

大家好!

我已经按照 Pat 的建议重写了这部分代码,现在它运行得非常出色!如果有人想要的话,这里是:

I've followed Pat's Advice in rewriting the portion of code and it now works brilliantly!Here it is if anyone wants it:

def addChar(_textBox, char):
    global charCount, strToInsert
    if charCount == 0:
        strToInsert = ""
    if char == "backSpace":
        strToInsert = strToInsert[:-1]
        if charCount != 0:
            charCount = charCount - 1
    else:
        if charCount <= 15:
            strToInsert = strToInsert + char
            charCount = charCount + 1

    _textBox.delete("1.0", END)
    _textBox.insert(END, strToInsert)

仅供参考:我确实在另一个函数中设置了变量 charCoun...

FYI: I do set the variable charCoun in another function...

这篇关于如何删除文本小部件 tkinter 中的最后一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 17:52