I use Spyder ide from Anaconda, I just play around with python and try to make the message encode/decode. I get many error from installing the library tkinter and base64. Eventually it runs but have to change to pip install tk/pybase64 instead. And after some trouble, there is new error that I completely don't know how to fix. Resources on the internet don't help me much.
ERROR: cannot import name 'messagebox' from 'tk'
import tk as tk
from tk import *
import pybase64
from tk import messagebox
tk.messagebox
root = Tk()
root.geometry("600x600")
root.title("TechVidvan Message Encryption and Decryption")
Msg = tk.StringVar()
key = tk.StringVar()
mode = tk.StringVar()
Result = tk.StringVar()
label = tk.Label(root, text='Enter Message', font=('Helvetica',10))
label.place(x=10,y=0)
mes = tk.Entry(root,textvariable=Msg, font=('calibre',10,'normal'))
mes.place(x=200,y=0)
label1 = tk.Label(root, text='e for encrypt and d for decrypt', font=('Helvetica',10))
label1.place(x=10,y=50)
l_mode = tk.Entry(root, textvariable=mode, font=('calibre',10,'normal'))
l_mode.place(x=200,y=50)
label2 = tk.Label(root, text='Enter key', font=('Helvetica',10))
label2.place(x=10,y=100)
l_key = tk.Entry(root, textvariable=key, font=('calibre',10,'normal'))
l_key.place(x=200,y=100)
label3 = tk.Label(root, text='Result', font=('Helvetica',10))
label3.place(x=10,y=150)
res = tk.Entry(root,textvariable=Result, font=('calibre',10,'normal'))
res.place(x=200,y=150)
#-------------------------------------------------------
def encode(key, msg):
enc = []
for i in range(len(msg)):
key_c = key[i % len(key)]
enc_c = chr((ord(msg[i])
ord(key_c)) % 256)
enc.append(enc_c)
return pybase64.urlsafe_b64encode("".join(enc).encode()).decode()
def decode(key, enc):
dec = []
enc = pybase64.urlsafe_b64decode(enc).decode()
for i in range(len(enc)):
key_c = key[i % len(key)]
dec_c = chr((256 ord(enc[i]) - ord(key_c)) % 256)
dec.append(dec_c)
return "".join(dec)
def Results():
msg = Msg.get()
k = key.get()
m = mode.get()
m.lower()
if (m == 'e'):
Result.set(encode(k, msg))
elif(m== 'd'):
Result.set(decode(k, msg))
else:
messagebox.showinfo('TechVidvan', 'Wrong mode entered. Try again.')
def qExit():
root.destroy()
def Reset():
Msg.set("")
key.set("")
mode.set("")
Result.set("")
btnshow = tk.Button(root, text='Show Message', foreground='green', command=Results)
btnshow.place(x=10,y=200)
btnreset = tk.Button(root, text='Reset', foreground='red', command=Reset)
btnshow.place(x=150,y=200)
btnexit = tk.Button(root, text='Exit', foreground='black', command=qExit)
btnshow.place(x=300,y=200)
root.mainloop()
CodePudding user response:
You don't need to install tkinter or base64 separately at all, they're both built-in. (If Tkinter support hasn't been built in to your Python installation, you will need to rebuild Python with Tkinter support, you can't just install it separately.)
- If you have indeed done
pip install tk, then you will have installed this package that is unrelated totkinter. You'll want to uninstall it. - Similarly, you don't need
pybase64to do base64 operations. Best uninstall it too. - Thirdly, make sure your script isn't called
tk.pyitself. - Finally, as mentioned above, the module is
tkinter, nottk. It's just common to doimport tkinter as tkfor brevity.- Also, using
*imports is generally not a good idea; you'll find your namespace polluted with all sorts of symbols you won't need or know where they're from.
- Also, using
CodePudding user response:
try to import tkinter, from tkinter import messagebox
you can also do the same with the rest of the imports
from tkinter import * as tk
import pybase64
from tkinter import messagebox
