如果全部选中并按下删除或退格按钮,则入口Tkinter不允许删除数据

时间:2021-06-07 02:26:54

I have written a code for entry widget which doesnot allow alphabets and limits the number of digits to 7. But i am not able to select all in the entry box and delete them using delete or backspace keys, could somebody help me on this.

我已经为入口小部件编写了一个代码,它不允许使用字母并将数字位数限制为7.但是我无法在输入框中选择所有并使用删除或退格键删除它们,有人可以帮我解决这个问题。

My code snippet:

我的代码片段:

self.DelayLabel = ttk.Label(self) self.DelayLabel["text"] = "timeout" vcmd = (root.register(self.IntLength_Delay), '%P', '%S") self.Delay = ttk.Entry(self, width = '5', validate = 'key', validatecommand = vcmd)

self.DelayLabel = ttk.Label(self)self.DelayLabel [“text”] =“timeout”vcmd =(root.register(self.IntLength_Delay),'%P','%S“)self.Delay = ttk。条目(self,width ='5',validate ='key',validatecommand = vcmd)

def IntLenght_Delay(self,value,text):
   if text in '0123456789':
       if len(value)<7:
          return True
       else:
           return False
   else:
       return False

2 个解决方案

#1


0  

Follow the logic. let's say you've entered "987". You now select it and try to delete it. In your validation function text (the current value) will be "987". Your code isn't prepared for that so it will fail the first if statement. Since it fails, validation returns False, disallowing the edit.

遵循逻辑。假设您输入了“987”。您现在选择它并尝试删除它。在验证功能中,文本(当前值)将为“987”。您的代码没有为此做好准备,因此它将使第一个if语句失败。由于它失败,验证返回False,不允许编辑。

You need to be prepared for what Tkinter passes to your function (a long string in the case of a deletion), and you need to explicitly allow an empty value after the edit.

您需要为Tkinter传递给您的函数(删除时为长字符串)做好准备,并且需要在编辑后显式允许空值。

#2


0  

Answering an older question here, but I was having an extremely similar problem and wound up finding a decent solution. @Bryan Oakley's answer is useful, but doesn't provide an example, which is what I aim to do here.

在这里回答一个较老的问题,但我遇到了一个非常相似的问题,并找到了一个不错的解决方案。 @Bryan Oakley的答案很有用,但没有提供一个例子,这是我打算在这里做的。

After importing Tkinter or tkinter (depending on your Python version) as tk, first define the initial validation command:

在将Tkinter或tkinter(取决于您的Python版本)导入为tk之后,首先定义初始验证命令:

val_cmd = (master.register(self.validate), '%P')  # master is root in thie case.

Next, whenever you have an entry box needing validation, I found this to be a nice way to write it out:

接下来,每当你有一个需要验证的输入框时,我发现这是一个很好的写出来的方法:

self.entry = tk.Entry(your_frame_of_choice)
self.entry.insert('end', 100)  # Inserts 100 as an initial value.
self.entry.config(validate='key', validatecommand=val_cmd)

This inserts a value before the validation begins. I originally had something like self.entry = tk.Entry(your_frame_of_choice, validate='key', validatecommand=val_cmd), but various iterations of the validation function that I toyed with rejected the insert code if the validation came before the insert. The one listed below doesn't care, but I've kept the entry code this way as I find it more aesthetically pleasing.

这会在验证开始之前插入一个值。我最初有类似self.entry = tk.Entry(your_frame_of_choice,validate ='key',validatecommand = val_cmd),但我玩的验证函数的各种迭代在插入之前验证时拒绝了插入代码。下面列出的那个并不关心,但我保持这样的入门代码,因为我发现它更美观。

Finally, the actual function:

最后,实际功能:

def validate(self, input_text):
    if not input_text:
        return True
    try:
        float(input_text)
        return True
    except ValueError:
        return False

This checks for floats, but you could easily change it to ints, as you prefer. And, most importantly, it allows you to delete and write over any highlighted text in the entry box.

这会检查浮动,但您可以根据需要轻松将其更改为整数。而且,最重要的是,它允许您删除和写入输入框中任何突出显示的文本。

I'm sure the initial problem is long gone, but I hope someone in the future finds this to be helpful!

我确信最初的问题早已不复存在,但我希望将来有人认为这有用!

#1


0  

Follow the logic. let's say you've entered "987". You now select it and try to delete it. In your validation function text (the current value) will be "987". Your code isn't prepared for that so it will fail the first if statement. Since it fails, validation returns False, disallowing the edit.

遵循逻辑。假设您输入了“987”。您现在选择它并尝试删除它。在验证功能中,文本(当前值)将为“987”。您的代码没有为此做好准备,因此它将使第一个if语句失败。由于它失败,验证返回False,不允许编辑。

You need to be prepared for what Tkinter passes to your function (a long string in the case of a deletion), and you need to explicitly allow an empty value after the edit.

您需要为Tkinter传递给您的函数(删除时为长字符串)做好准备,并且需要在编辑后显式允许空值。

#2


0  

Answering an older question here, but I was having an extremely similar problem and wound up finding a decent solution. @Bryan Oakley's answer is useful, but doesn't provide an example, which is what I aim to do here.

在这里回答一个较老的问题,但我遇到了一个非常相似的问题,并找到了一个不错的解决方案。 @Bryan Oakley的答案很有用,但没有提供一个例子,这是我打算在这里做的。

After importing Tkinter or tkinter (depending on your Python version) as tk, first define the initial validation command:

在将Tkinter或tkinter(取决于您的Python版本)导入为tk之后,首先定义初始验证命令:

val_cmd = (master.register(self.validate), '%P')  # master is root in thie case.

Next, whenever you have an entry box needing validation, I found this to be a nice way to write it out:

接下来,每当你有一个需要验证的输入框时,我发现这是一个很好的写出来的方法:

self.entry = tk.Entry(your_frame_of_choice)
self.entry.insert('end', 100)  # Inserts 100 as an initial value.
self.entry.config(validate='key', validatecommand=val_cmd)

This inserts a value before the validation begins. I originally had something like self.entry = tk.Entry(your_frame_of_choice, validate='key', validatecommand=val_cmd), but various iterations of the validation function that I toyed with rejected the insert code if the validation came before the insert. The one listed below doesn't care, but I've kept the entry code this way as I find it more aesthetically pleasing.

这会在验证开始之前插入一个值。我最初有类似self.entry = tk.Entry(your_frame_of_choice,validate ='key',validatecommand = val_cmd),但我玩的验证函数的各种迭代在插入之前验证时拒绝了插入代码。下面列出的那个并不关心,但我保持这样的入门代码,因为我发现它更美观。

Finally, the actual function:

最后,实际功能:

def validate(self, input_text):
    if not input_text:
        return True
    try:
        float(input_text)
        return True
    except ValueError:
        return False

This checks for floats, but you could easily change it to ints, as you prefer. And, most importantly, it allows you to delete and write over any highlighted text in the entry box.

这会检查浮动,但您可以根据需要轻松将其更改为整数。而且,最重要的是,它允许您删除和写入输入框中任何突出显示的文本。

I'm sure the initial problem is long gone, but I hope someone in the future finds this to be helpful!

我确信最初的问题早已不复存在,但我希望将来有人认为这有用!