Limiting entry on a tk widget

后端 未结 4 732
隐瞒了意图╮
隐瞒了意图╮ 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: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()
    

提交回复
热议问题