简体   繁体   English

无法在验证命令中将条目作为参数传递

[英]Cannot pass an entry as argument in validatecommand

I'm usign Python 3.x with Tkinter;我使用 Python 3.x 和 Tkinter; I would like to check that the value of a tkinter.Entry is a number by calling a function called "is_valid_entry" and passing all args via validatecommand.我想通过调用名为“is_valid_entry”的 function 并通过 validatecommand 传递所有参数来检查 tkinter.Entry 的值是一个数字。 I would like to use the same function for other entries too.我也想对其他条目使用相同的 function。 The problem is that inside is_valid_entry I can't clean the entry text with self.delete(0,END) because self is seen as str instead of tkinter.Entry.问题是在 is_valid_entry 内部我无法使用 self.delete(0,END) 清除条目文本,因为 self 被视为 str 而不是 tkinter.Entry。 I hope I made it understandable, thanks for the help!我希望我能理解它,谢谢你的帮助!

Here's the code:这是代码:

from tkinter import *
from tkinter import ttk
from tkinter import filedialog as fd

import tkinter as tk

window = tk.Tk()

window.geometry("600x600")

window.title("Hello Stackoverflow")

window.resizable(False,False)

def isfloat(value):
    try:
        float(value)
        return True
    except ValueError:
        return False

def is_valid_entry(self,value):
    if (value.isnumeric() or isfloat(value)):        
        return True
    else:
        tk.messagebox.showerror(title="Error!", message="Value must be a number!")
        print(type(self))

        self.delete(0,END) # I'd like to clean the entry text but self is type string now not type tkinter.Entry 

        return False

e2=tk.Entry(window,width=20)
e2.grid(row=6,column=2,padx=5)

print(type(e2))
okayCommand = e2.register(is_valid_entry)

e2.config(validate='focusout',validatecommand=(okayCommand,e2,'%P'))

if __name__ == "__main__":
    window.mainloop()

I tried to use a function to check if an entry text is a valid number.我尝试使用 function 来检查输入文本是否为有效数字。 I registered the function and configured the entry to call this function in case of 'focusout' via validatecommand.我注册了 function 并将条目配置为通过 validatecommand 在“focusout”的情况下调用此 function。 I would like to pass the entry as well (self) as args of validatecommand so that when the function is executed, in case of invalid number, the text in the entry is cleared.我也想将条目(self)作为 validatecommand 的参数传递,以便在执行 function 时,如果数字无效,条目中的文本将被清除。 The entry param inside the function is seen as a str instead of a tkinter.Entry. function 中的条目参数被视为 str 而不是 tkinter.Entry。

A workaround is to store the widgets in a dictionary with a string key and pass that key in the config setting.解决方法是将小部件存储在具有字符串键的字典中,并在config设置中传递该键。

def is_valid_entry(value, widgetname):
    if (value.isnumeric() or isfloat(value)):        
        return True
    else:
        tk.messagebox.showerror(title="Error!", message="Value must be a number!")
        
        mywidgets[widgetname].delete(0,END) 

        return False

e2=tk.Entry(window,width=20)
e2.grid(row=6,column=2,padx=5)

mywidgets = dict()
mywidgets["e2"] = e2

print(type(e2))
okayCommand = e2.register(is_valid_entry)

e2.config(validate='focusout',validatecommand=(okayCommand,'%P','e2'))

The problem is that inside is_valid_entry I can't clean the entry text with self.delete(0,END)问题是在 is_valid_entry 里面我不能用 self.delete(0,END) 清除条目文本

self.delete(0,END)

to:到:

e2.delete(0,END)

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

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