3
\$\begingroup\$

I realize this won't stop people who know what they're doing, just a project I decided to undertake while I'm trying to learn Python and Tkinter. Would love some input!

import Tkinter
import base64

class crypt_mod(Tkinter.Tk):
    def __init__(self, parent):
        Tkinter.Tk.__init__(self, parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid()

        plain_label = Tkinter.Label(text='Plain Text:')
        plain_label.grid(column=0, row=0, sticky='E')

        self.ptxt = Tkinter.StringVar()
        self.plain_text = Tkinter.Entry(self, textvariable=self.ptxt, width=50)
        self.plain_text.grid(column=1, row=0, padx=5, pady=5)
        self.plain_text.bind("<Return>", self.encrypt)

        self.enc_button = Tkinter.Button(self, text='Encrypt', command=self.encrypt, relief='groove')
        self.enc_button.grid(column=2, row=0, padx=5, pady=5)

        self.dec_button = Tkinter.Button(self, text='Decrypt', command=self.decrypt, relief='groove')
        self.dec_button.grid(column=2, row=1, padx=5, pady=5)

        plain_label = Tkinter.Label(text='Base64:')
        plain_label.grid(column=0, row=1, sticky='E')

        self.etxt = Tkinter.StringVar()
        self.e_text = Tkinter.Entry(self, textvariable=self.etxt, width=50)
        self.e_text.grid(column=1, row=1, padx=5, pady=5)

        self.clear_button = Tkinter.Button(self, text='Clear Both', command=self.clear_both, relief='groove')
        self.clear_button.grid(column=1, row=2, padx=5, pady=5)

        self.htxt = Tkinter.StringVar()
        self.history = Tkinter.Text(width=50, height=10, wrap='word', spacing1=2, font=('courier',8), state='disabled')
        self.history.grid(column=0, row=3, columnspan=3, padx=5, pady=5)

        self.clear_history_button = Tkinter.Button(self, text='Clear History', command=self.clear_history, relief='groove')
        self.clear_history_button.grid(column=1, row=4, padx=5, pady=5)

    def encrypt(self):
        self.etxt.set(base64.b64encode(str(self.ptxt.get())))
        self.history.config(state='normal')
        self.history.insert(Tkinter.END, 'encrypt:\n' + base64.b64encode(str(self.ptxt.get())) + '\n')
        self.history.config(state='disabled')
        self.history.see(Tkinter.END)
        #print "encrypt click!"

    def decrypt(self):
        self.ptxt.set(base64.b64decode(str(self.etxt.get())))
        self.history.config(state='normal')
        self.history.insert(Tkinter.END, 'decrypt:\n' + base64.b64decode(str(self.etxt.get())) + '\n')
        self.history.config(state='disabled')
        self.history.see(Tkinter.END)
        #print "decrypt click!"

    def clear_both(self):
        self.etxt.set('')
        self.ptxt.set('')

    def clear_history(self):
        self.history.config(state='normal')
        self.history.delete(0.0, Tkinter.END)
        self.history.config(state='disabled')
        pass

if __name__ == "__main__":
    app = crypt_mod(None)
    app.title('Base64 Crypto')
    app.mainloop()
\$\endgroup\$
2
\$\begingroup\$
import Tkinter
import base64

class crypt_mod(Tkinter.Tk):

Do you know about PEP8? It's a style guide for Python that lots of Python developers follow. One of the things it says is that class names should use the CapWords convention, eg. CryptMod. Note that Tkinter doesn't necessary follow those rules, as it's a really old module.

    def __init__(self, parent):
        Tkinter.Tk.__init__(self, parent)
        self.parent = parent
        self.initialize()

self.parent seems to be unused.

    def initialize(self):
        self.grid()
            ....

Not much to say here: the usual GUI building code.

    def encrypt(self):
        self.etxt.set(base64.b64encode(str(self.ptxt.get())))
        self.history.config(state='normal')
        self.history.insert(Tkinter.END, 'encrypt:\n' + base64.b64encode(str(self.ptxt.get())) + '\n')
        self.history.config(state='disabled')
        self.history.see(Tkinter.END)
        #print "encrypt click!"

Avoid creating twice your encrypted text: your code would be clearer if you had a encrypted variable. It's not terribly important here, but could be in future projects where you will have more lines of code. Consider using format() (recommended in Python 3) instead of building your strings by concatenation: "decrypt:\n {} \n".format(encrypted). (Those comments also apply to decrypt.)

\$\endgroup\$
  • \$\begingroup\$ No, I was unaware of PEP8, but now I have a new obsession. :P Seems like the best time to learn to be Pythonic is at the start! Thanks for all the tips, will apply PEP8 to all future projects. \$\endgroup\$ – Refrax May 14 '13 at 2:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.