I am getting { } on the end of my output. What is causing this and how do I remove it?
I am trying my hand at a simple gui that encrypts and decrypts messages with a "secret number"/key identifier in the same window for fun. So far it works and I am pretty happy with it. My only issue is I am getting the output return in my crypt() function of what I want followed by {}. Using the normal print() function, I do not have that issue. However, using the GUI it does. I imagine it is the package tkinter doing this?
My code:
from tkinter import *
import tkinter.messagebox
def crypt():
so = int(nu1.get())
inp = str(nu2.get().upper())
old_dic = {chr(i): i * (int(so)+int(so))for i in range(ord("A"), ord("A") + 26)}
if len(inp) >= 1:
bit = list(inp)
spell = list(map(old_dic.get, bit))
spell = spell[::-1]
ans1 = (*spell, "")
blank4.insert(0, ans1)
else:
print("Improper input.")
main = Tk()
Label(main, text = "Enter secret number:").grid(row=0)
Label(main, text = "Cryptify:").grid(row=1)
Label(main, text = "Encrypted output:").grid(row=2)
Button(main, text='Show', command=crypt).grid(row=3, column=1, sticky=W, pady=4)
nu1 = Entry(main)
nu2 = Entry(main)
blank4 = Entry(main)
nu1.grid(row=0, column=1)
nu2.grid(row=1, column=1)
blank4.grid(row=2, column=1)
mainloop()
Output for the Encryption I am getting looks like this:
Secret Number: 45
Cryptify: Hello
Encrypted Output: 7110 6840 6840 6210 6480 {}
Output should look like this:
Secret Number: 45
Cryptify: Hello
Encrypted Output: 7110 6840 6840 6210 6480
The reason is quite simple, tkinter
uses tcl
and it does not know what python list
or tuple
are, and it is tcl
way of reading tuples/lists by putting it inside {}
like you noticed, so convert it to a string, like:
ans1 = (*map(str,spell), "")
blank4.insert(0, ' '.join(ans1))
Similarly do so for all the places where this is seen. Sorry, I had posted the answer a while back, internet got off :/