Search code examples
pythonfunctiondictionarytkinterreplace

Problem in dictionaries and replace method (python)


I'm try to make a app witch take words and turn them to security form

it's ok with first part but in second part: i want to turn that word who truned into a security form trun it into a word again and its return incorrect like this : word: 'hnnh' sec form: 'yhhy' But when I try to turn it back, it writes the wrong word: 'nnnn'

and its the problem

code:

from tkinter import Tk,Button,Label,Entry

words ={'q':'0','w':'1','e':'2','r':'3','t':'4','y':'5','u':'%','i':'7','o':'8',
    'p':'9','a':'q','s':'w','d':'e','f':'r','g':'t','h':'y','j':'u','k':'i',
    'z':'a','x':'s','c':'d','v':'f','b':'g','n':'h','m':'j','l':'o',
    '1':'z','2':'x','3':'c','4':'v','5':'b','6':'m','7':'.','8':'#','9':'@','0':'!',
    '@':'$','#':':','!':'&',':':'*','.':'n',' ':'6'}


sec ={'0':'q','1':'w','2':'e','3':'r','4':'t','5':'y','%':'u','7':'i','8':'o',
    '9':'p','q':'a','w':'s','e':'d','r':'f','t':'g','y':'h','u':'j',
    'o':'l','a':'z','s':'x','d':'c','f':'v','g':'b','j':'m',
    'z':'1','x':'2','c':'3','v':'4','b':'5','m':'6','.':'7','#':'8','@':'9','!':'0',
    '$':'@',':':'#','&':'!','*':':','6':' ','n':'.','h':'n','i':'k'}
def word_to_sec():
    in_use_words = str(word_to_sec_ent.get())
    for char in words:
        if char in str(word_to_sec_ent.get()):
            in_use_words = in_use_words.replace(char,words[char])
    output = in_use_words  
    config_lbl.config(text= 'word to sec: {}'.format(output))
    return output

def sec_to_word():
    in_use_words = str(sec_to_word_ent.get())
    for chars in words:
        for char in words[chars]:
            if char in str(sec_to_word_ent.get()):
                in_use_words = in_use_words.replace(words[char],char)
    output = in_use_words  
    config_lbl.config(text= 'sec to word: {}'.format(output))
    return output

window = Tk()
window.title('sec_maker           made by: parsa')

word_to_sec_lbl = Label(window,text= 'enter word to convert into the sec form:          ')
word_to_sec_lbl.grid(row=1,column=1)
word_to_sec_ent = Entry(window)
word_to_sec_ent.grid(row=2,column=1)
word_to_sec_btn = Button(window,text='convert to sec form',command=word_to_sec)
word_to_sec_btn.grid(row=3,column=1)

sec_to_word_lbl = Label(window,text= 'enter sec form to convert into the word:')
sec_to_word_lbl.grid(row=1,column=3)
sec_to_word_ent = Entry(window)
sec_to_word_ent.grid(row=2,column=3)
sec_to_word_btn = Button(window,text='convert to word',command=sec_to_word)
sec_to_word_btn.grid(row=3,column=3)

config_lbl = Label(window,text='out put: ')
config_lbl.grid(row=4,column=2)

window.mainloop()

Solution

  • The solution I used was to remove the old functions and use this instead:

    from tkinter import Tk,Button,Label,Entry
    
    words = ['1','2','3','4','5','6','7','8','9','0','a','b','c','d','e','f','g',
        'h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
        '!','@','#','$','%','^','&','*','(',')','_','-','=','+','[',']','\\','/',"'",'"',
        ':',';','?','.',',','<','>','{','}','|',' '
        ]
    
    sec = ['z','x','c','v','b','m','.','#','@','!','q','g','d','e','2','r','t',
        'y','7','u','i','o','j','h','8','9','0','3','w','4','6','f','1','s','5','a',
        '&','$','%','\\','/',':',')','(','|','=','+',' ',';','[',"'",'"','l','k','p','^',
        '*','>','<','n',']','}','{','_','-',',','?',
        ]
    
    def word_to_sec():
        p=str(word_to_sec_ent.get())
        o={}
        c=''
        try:
            for i in range(100):
                o[i+1]=p[i]
                for f in range(67):
                    if words[f] in o[i+1]:
                        o[i+1]= o[i+1].replace(words[f],sec[f])
                        break
        except:pass
        try:
            for b in range(100):
    
                c = c+o[b+1]
        except:pass
        output = c  
        config_lbl.config(text= 'word to sec: {}'.format(output))
        return output
    
    def sec_to_word():
        p=str(sec_to_word_ent.get())
        o={}
        c=''
        try:
            for i in range(100):
                o[i+1]=p[i]
                for f in range(67):
                    if sec[f] in o[i+1]:
                        o[i+1]= o[i+1].replace(sec[f],words[f])
                        break
        except:pass
        try:
            for b in range(100):
    
                c = c+o[b+1]
        except:pass
        output = c  
        config_lbl.config(text= 'sec to word: {}'.format(output))
        return output
    
    
    def clear():
        config_lbl.config(text= 'out put: ')
    
    
    window = Tk()
    window.title('sec_maker           made by: parsa')
    
    word_to_sec_lbl = Label(window,text= 'enter word to convert into the sec form:          ')
    word_to_sec_lbl.grid(row=1,column=1)
    word_to_sec_ent = Entry(window)
    word_to_sec_ent.grid(row=2,column=1)
    word_to_sec_btn = Button(window,text='convert to sec form',command=word_to_sec)
    word_to_sec_btn.grid(row=3,column=1)
    
    sec_to_word_lbl = Label(window,text= 'enter sec form to convert into the word:')
    sec_to_word_lbl.grid(row=1,column=3)
    sec_to_word_ent = Entry(window)
    sec_to_word_ent.grid(row=2,column=3)
    sec_to_word_btn = Button(window,text='convert to word',command=sec_to_word)
    sec_to_word_btn.grid(row=3,column=3)
    
    config_lbl = Label(window,text='out put: ')
    config_lbl.grid(row=4,column=2)
    clear_btn = Button(window, text='clear',command=clear )
    clear_btn.grid(row=5,column=2)
    
    window.mainloop()