Limiting entry on a tk widget

后端 未结 4 733
隐瞒了意图╮
隐瞒了意图╮ 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 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.

提交回复
热议问题