当我复制一些文字和粘贴(CRTL + V)它在一个Tkinter的条目,如果有选择的文本,它不会从条目中删除它。我在Linux(Mint)64位。
的Tkinter的复制粘贴到输入不会删除选定的文本
现在我粘贴 “d”(CTRL + V)到其上但结果是这样的:
第一:我想知道这是否是一个bug具体到Linux或这是它是如何应该是什么?
二:我在想一个解决方法为这个与validatecommand
但我得到了另外一个问题:
如果我要删除命令选定的文本,我必须知道选择的指数入口。否则,如果有直接后光标之前所选文本的多个实例,我不知道要删除哪一个,并用新的文本替换。因为光标可以是该选择的任一侧(取决于如果该人被拖动到左侧或从左向右上的文本的形式鼠标右)。
现在有没有办法让指数的选择在入门?或另一种方法来解决这个问题?
下面是一些代码有问题的例子:
import tkinter as tk
root = tk.Tk()
def validation(after_text, before_text, validation_text, cursor_index):
cursor_index = int(cursor_index)
print('cursor index:', cursor_index)
print('text after change:', after_text)
print('text before change:', before_text)
print('text in need of validation:', validation_text)
try:
selection = root.selection_get()
except:
selection = ''
print('selection:', selection)
# EXAMPLE:
# validation_text = 'd'
# before text = "bb"
# now if someone dragged from right to left on the 2nd b:
# cursor position will be 1 (imagine | as the cursor): 'b|b'
# cursor_index = 1
# after_text = 'bdb' --> but should be 'bd'
# now if someone dragged from left to right on the 2nd b:
# cursor position will be 2 (imagine | as the cursor): 'bb|'
# cursor_index = 2
# after_text = 'bbd' --> but should be 'bd'
# so there is no way for me to know which of these b's I have
# to replace with d based on cursor position alone. I have to
# know the index of selection itself in before_text to be
# able to replace the text properly.
# I don't know how to get that.
return True
txtvar = tk.StringVar(value = 'a-b-c-d-e')
entry = tk.Entry(root, textvariable = txtvar)
font = tk.font.Font(family = entry.cget('font'), size = -50)
entry.config(validate = 'all',
vcmd = (root.register(validation),'%P', '%s', '%S', '%i'),
font = font)
entry.pack()
root.mainloop()
在windows 7/python3中,粘贴按预期工作。 – Lafexlos
关于选择的东西,你可以看看[这个问题](https://stackoverflow.com/questions/4073468/how-do-i-get-a-selected-string-in-from-a-tkinter-text-框)。这两个答案(及其评论)都非常翔实。 – Lafexlos
@Lafexlos我已经得到了选择文本...看看我的尝试/除了。问题就像我解释的那样,并不是无法获取文本,它不知道文本从哪里来(文本在整个文本中的选择文本的索引)。 – ROAR