如何删除文本小部件 tkinter 中的最后一个字符

编程入门 行业动态 更新时间:2024-10-21 16:34:48
本文介绍了如何删除文本小部件 tkinter 中的最后一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试使以下代码工作:它的目的是删除名为 _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 中的最后一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 06:24:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390721.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:部件   字符   文本   tkinter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!