TypeError:ord()需要一个字符,但是找到了长度为5的字符串

时间:2019-07-16 20:24:54

标签: python-3.x tkinter logical-operators

我需要对4种字符进行整形,用数字加上数字并不会返回错误,但是使用字母可以返回标题中的该错误。我需要限制从“ 0到9”和“ A到D”。 在limiteUsuario()方法中,一切正常。

from tkinter import *
import tkinter as tk

class loginUser:
    def __init__(self, window, master=None):   
        self.wind = window
        self.wind.title("System F2T")

        #Definicoes de fonte p/ o layout de login
        self.fonteTitulo = ("Arial","10","bold")
        self.fontePadrao = ("Arial", "10")

        self.var = StringVar() #create the var first before you assign them
        self.var2 = StringVar()

        #Labels e campos de texto do sistema de login
        self.userLabel = Label(text="Digite seu usuário:", font=self.fontePadrao,bg="#000",fg="#FFF").place(x=27,y=60)
        self.user = Entry(textvariable=self.var, font=self.fontePadrao,bg="#FFF",fg="#000")
        self.user.place(x=140,y=60,width=110)

        self.senhaLabel = Label(text="Digite sua senha:", font=self.fontePadrao,bg="#000",fg="#FFF").place(x=29,y=90)
        self.senha = Entry(textvariable=self.var2, font=self.fontePadrao,bg="#FFF",fg="#000")
        self.senha.place(x=140,y=90,width=110)

        self.max_user = 1
        self.var.trace("w", self.limiteUsuario)
        self.max_senha = 4
        self.var2.trace("w", self.limiteSenha)

    def limiteUsuario(self,*args):
        u = self.var.get()
        if len(u) == 1 and not 65<=ord(u)<=68 and not 48<=ord(u)<=57: # you can also use *if not u in "ABCD"*
            self.var.set("")
        elif len(u) > 1:
            if not 65<=ord(u[-1])<=68: # retirar ultimo caracter caso nao seja digito
                self.var.set(u[:-1])
            else: # aproveitar apenas os primeiros 5 chars
                self.var.set(u[:self.max_user])

    def limiteSenha(self,*args):
        text = self.var2.get()
        text = ''.join(char for char in text if char in 'ABCD')
        if len(text) == 4 and not 65<=ord(text)<=68 and not 48<=ord(text)<=57: # you can also use *if not u in "ABCD"*
            self.var2.set("")
        elif len(text) > 4:
            if not 65<=ord(text[-1])<=68: # retirar ultimo caracter caso nao seja digito
                self.var2.set(text[:-1])
            else: # aproveitar apenas os primeiros 5 chars
                self.var2.set(text[:self.max_senha])
        print(self.var2.set(text))

if __name__ == "__main__":
    root = Tk()
    root['bg'] = "#000"
    loginUser(root)
    #Tamanho da janela
    root.geometry("330x200")
    root.mainloop()

1 个答案:

答案 0 :(得分:0)

ord()只能转换单个字符-如ord("a")-但您的字符串包含许多字符-如ord("BXA7D")

您可以使用for循环单独处理每个字符

text = "BXA7D"  # text = self.var2.get()
text = ''.join(char for char in text if char in 'ABCD')
print(text)  # self.var2.set(text)

# BAD

或更长时间

text = "BXA7D"  # text = self.var2.get()
result = []

for char in text:
    if char in 'ABCD':
        result.append(char)

text = ''.join(result)
print(text)  # self.var2.set(text)

# BAD

编辑:我不知道您想要什么结果。如果有ABCD0123456789,请删除所有字符串,或者仅删除字符ABCD0123456789

如果有字符ABCD0123456789或切成5个字符,则删除所有字符串。

def limiteSenha(self,*args):
    s = self.var2.get()
    if len(s) > 5:
        for char in s:
            if char not in 'ABCD0123456789':
                s = ""
                break # exit for-loop because there is no need to check rest
        self.var2.set(s[:self.max_senha])
    elif len(s) == 5:
        if not 65<=ord(s[-1])<=68: # retirar ultimo caracter caso nao seja digito
            self.var2.set(s[:-1])
        else: # aproveitar apenas os primeiros 5 chars
            self.var2.set(s[:self.max_senha])