简体   繁体   中英

Limiting entry on a tk widget

I have trouble finding a way to limit the entry length of entry widgets, I would like to limit it to 20 characters, ie when I click on a sequence or the other I would like to be able to edit it but stay in the 20 characters limit. In or order to keep the code light, should I use a regex, a loop or check the entry with an event?

Here is my code:

import Tkinter
from Tkinter import *
import tkFileDialog

root = Tkinter.Tk()

edit1    =StringVar()
edit2    =StringVar()
s = StringVar()


s = "GATACACGCGCGCGTATATATTACGCGCGCGATACA"



lb01=Label(root,text="sequence1")
lb01v=Entry(root,textvariable=edit1,width=20)
lb01v.delete(0, END)
lb01v.insert(0, s[6:20])

lb01.grid(sticky=W,row=1,column=1)
lb01v.grid(row=1,column=2)


lb02=Label(root,text="sequence2")
lb02v=Entry(root,textvariable=edit2,width=20)
lb02v.delete(0, END)
lb02v.insert(0, s[0:6])

lb02.grid(sticky=W,row=2,column=1)
lb02v.grid(row=2,column=2)

root.mainloop()

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()

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()

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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