简体   繁体   English

Python tkinter 文本小部件删除 function 无法正常工作

[英]Python tkinter text widget delete function doesn't work correctly

I am creating a calculator app and i added this backslash button to it and here is the function for the number buttons:我正在创建一个计算器应用程序,我向它添加了这个反斜杠按钮,这里是数字按钮的 function: 在此处输入图像描述

it works correctly and when i press a number it just adds it to the end of the current number.它工作正常,当我按下一个数字时,它只是将它添加到当前数字的末尾。 and this is one of the buttons:这是其中一个按钮: 在此处输入图像描述

so i created the backslash function which is this:所以我创建了反斜杠 function 这是: 在此处输入图像描述

but when i press it it removes the number from the beginning.但是当我按下它时,它会从头开始删除数字。 as an example we have this number: 76543, if i click on backslash it removes 7 instead of 3 and it will look like: 6543. i tried entt.delete(-1.0), entt.delete(END), entt.delete(END,END) and non of them worked.例如,我们有这个数字:76543,如果我单击反斜杠,它会删除 7 而不是 3,它看起来像:6543。我试过 entt.delete(-1.0)、entt.delete(END)、entt.delete( END,END) 但没有一个有效。 you guys know any solution?你们知道任何解决方案吗?

The first argument to the delete method must be the index immediately before the character to be deleted. delete方法的第一个参数必须是要删除的字符之前的索引。 If you want to delete the character immediately before the insertion cursor, you can use the index "insert-1c" (the insertion point, minus one character).如果要删除紧接插入cursor之前的字符,可以使用索引"insert-1c" (插入点,减去一个字符)。

entt.delete("insert-1c")

The Text widget is not a very good widget to use for this case.对于这种情况,文本小部件不是一个很好的小部件。 However for what you need you can use standard substrings to do the same job.但是,对于您需要的内容,您可以使用标准子字符串来完成相同的工作。

def back_slash_click():
    txt = entt.get('1.0', END)
    entt.delete('1.0', END)
    end.insert('1.0', txt[:-2])

Notes: Text widgets automatically add ' \n ' to the end of the get() which is why i removed 2 characters from txt.注意:文本小部件会自动将“ \n ”添加到 get() 的末尾,这就是我从 txt 中删除 2 个字符的原因。

Text.index(END) always returns the line count +1 due to the automatically added ' \n ' Text.index(END) 由于自动添加的 ' \n ',总是返回行数 +1

Better Answer更好的答案

The numbers for the index are line.character: it is possible to not delete and reinsert the text but you would need to get the text, count the number of lines and the number of characters in the last line then take away 2 from the final character number索引的数字是 line.character:可以不删除并重新插入文本,但您需要获取文本,计算行数和最后一行中的字符数,然后从最后一行中减去 2字符数

You can easily find the last line with ind = int(widget.index(END))-1 and get the last line and get the amount of characters with len(widget.get(ind, END))您可以使用ind = int(widget.index(END))-1轻松找到最后一行并获取最后一行并使用len(widget.get(ind, END))获取字符数

This can be used in this function to do the same job.这可以用在这个 function 中来做同样的工作。

def delete_last_char(widget):
    ind = float(widget.index(END))-1.0 ##get the last line
    txt = len(widget.get(ind, END))-2 ##get the character count and -2
    rmv = float(f'{int(ind)}.{txt}') ## format this into an accepted float
    widget.delete(rmv, END) ##delete that index range from the widget

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM