-1

So I made an GUI in python and encryption program: This is the layout

layout=[[sg.Text('Enter message'),sg.Input(key='-IN-')],
    [sg.Text('Cryptation key'),sg.Input(key='-KEY-')],
    [sg.Text('Answer',key='-OUT-')],
    [sg.Button('OK'), sg.Button('EXIT')]
    ]

This is the while loop:

while True:
event, values=window.read()
if event is None or event == 'EXIT':
    break
mesaj=str('-IN-').upper()
cheie=str('-KEY-').upper()
key=generate_key(mesaj,cheie)
encrypt_text=encrypt_vigenere(mesaj,key)
window['-OUT-'].update(values[encrypt_text])

window.close()

Here is the function that I think is thee problem:

def encrypt_vigenere(message,key_word):
key = generate_key(message,key_word)

encrypted_text = ""

for i in range(0,len(message)):
    if message[i] == " ":
        encrypted_text += " "
    else:
        encryption_value = (alphabets.find(message[i]) + alphabets.find(key[i]))%26
        encrypted_text += alphabets[encryption_value]

return encrypted_text

And it gives me this error:

Traceback (most recent call last): File "C:/Users/flavius/Documents/python/games/urs_mina/main.py", line 62, in window['-OUT-'].update(values[encrypt_text]) KeyError: 'YSRX'

Every time I press OK ti shows the message above. Can someone explain to me why?

  • 2
    Because your `values` dictionary does not have an entry for `'YSRX'`. Why did you think it did? And by the way, saying `str('-IN-").upper()` is silly; that value (and '-KEY-') are already strings and already upper case. – Tim Roberts Apr 06 '21 at 05:33
  • 1
    Is it possible you just meant to say `window['-OUT-'].update( encrypt_text )`? – Tim Roberts Apr 06 '21 at 05:35
  • No. I tried to change the code and it gives me the same error, the only difference being: KeyError: 'MSRE'. Also, i tryed the same program but without the GUI and it works. But when I decomment the GUI it shows me the error. – flavius08_80 Apr 06 '21 at 06:02
  • I assume you understand that "YSRX" and "MSRE" are both the results of running your encryption scheme. Why do you think those values will be in the `values` dict? – Tim Roberts Apr 06 '21 at 06:06
  • I tryed this `def encryption(string, key): encrypt_text = [] for i in range(len(string)): x = (ord(string[i]) + ord(key[i])) % 26 x += ord('A') encrypt_text.append(chr(x)) return ("".join(encrypt_text)) ` It works when I comment the GUI and use main. But if I use the GUI it crashes. If I do it in main it encrypts just fime. – flavius08_80 Apr 06 '21 at 06:12
  • You don't understand what's going on here. Your encryption function is not the problem. The problem happens in `window['-OUT-'].update(values[encrypt_text])`, because you are trying to look up the entry in the `values` dictionary that has your encrypted text as a key, and there is no such value. What do you think that statement is doing? – Tim Roberts Apr 06 '21 at 06:22

1 Answers1

0

To get value of element sg.Input in event loop by

mesaj = values['-IN-'].upper()
cheie = values['-KEY-'].upper()

To update value of element sg.Output by

window['-OUT-'].update(encrypt_text)

window[key] for element, values[key] for values dictionary with element key.

If you get wrong arguments for function encrypt_vigenere, you may also get wrong result, IMO, it's not the problem here.

Jason Yang
  • 11,284
  • 2
  • 9
  • 23