Limiting entry on a tk widget

后端 未结 4 722
隐瞒了意图╮
隐瞒了意图╮ 2020-12-17 06:24

I have trouble finding a way to limit the entry length of entry widgets, I would like to limit it to 20 characters, i.e. when I click on a sequence or the other I would like

相关标签:
4条回答
  • 2020-12-17 07:20

    I know its too late to add any answers to this, just found a simpler way to represent what Wabara had answered. This will help if you need multiple entry limits and each to a user-defined length limit. Here's a code working on Python 3.6.5:

    def main():
        pass
    
    if __name__ == '__main__':
        main()
    
    from tkinter import *
    
    def limit_entry(str_var,length):
        def callback(str_var):
            c = str_var.get()[0:length]
            str_var.set(c)
        str_var.trace("w", lambda name, index, mode, str_var=str_var: callback(str_var))
    
    root = Tk()
    
    abc = StringVar()
    xyz = StringVar()
    
    limit_entry(abc,3)
    limit_entry(xyz,5)
    
    e1 = Entry(root, textvariable=abc)
    e2 = Entry(root, textvariable=xyz)
    
    e1.pack()
    e2.pack()
    root.mainloop()
    
    0 讨论(0)
  • 2020-12-17 07:20

    I will start off by making an alphabet to measure from. The alphabet is a string and has 26 letters meaning its too long for our use. we want 20 letters only, so our output should be "A" thru "T" only. I would define a function to make it happen and dump each string thru it that I would want cut to 20 characters or less. I am making the below code in such a way that it takes as an input any string that is called it takes that input in and processes it to 20 characters in length only...

    def twenty(z):
        a = z[0:20]
        return a
    
    alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    

    so to execute our newly made code, we need only call the print twenty command with the variable we want cut to 20 characters in the parenthesis.

    print twenty(alphabet)
    
    -----------------------------------------------------------------
    OUTPUT:
    ABCDEFGHIJKLMNOPQRST
    

    So you see, it worked, we input the entire alphabet into the program and it cut the string down to 20 letters only. now every time in your code you want to cut text down to 20 letters, just run the command twenty(variable) and it will make sure you have no more letters than that.

    Explanation: def twenty is to define a function with one input that you can call on over and over simply by typing twenty(variable) the next line is a = z[0:20] Meaning call variable "a" to equal the input from position 0 to position 20 and dont worry about anything past that. return command is how you get an output from the def function. anytime you create a def function, you should end it with a line.

    0 讨论(0)
  • 2020-12-17 07:23

    The simplest solution is to put a trace on the variable. When the trace fires, check the length of the value and then delete any characters that exceed the limit.

    If you don't like that solution, Tkinter also has built-in facilities to do input validation on entry widgets. This is a somewhat under-documented feature of Tkinter. For an example, see my answer to the question Python/Tkinter: Interactively validating Entry widget content

    0 讨论(0)
  • 2020-12-17 07:28

    Ok I did try with the trace variable, on a short piece of test code , this is excactly what I was searching for !! I like the fact you can prototype so easily in Python ;)

    def main():
        pass
    
    if __name__ == '__main__':
        main()
    
    from Tkinter import *
    
    def callback(sv):
        c = sv.get()[0:9]
        print "c=" , c
        sv.set(c)
    
    root = Tk()
    sv = StringVar()
    sv.trace("w", lambda name, index, mode, sv=sv: callback(sv))
    e = Entry(root, textvariable=sv)
    e.pack()
    root.mainloop()
    
    0 讨论(0)
提交回复
热议问题