problem with Tk

Peter Otten __peter__ at web.de
Thu Nov 6 19:29:57 EST 2003


Lucas Raab wrote:

> In browsing through the Python module index, I noticed the rotor
> encryption module. It piqued my interest and I made a Tk app out of it.
> The only problem is whenever I input a string to be encrypted I get the
> error message:
> 
> Exception in Tkinter callback
> Traceback (most recent call last):
>     File: "C:\PYTHON23\lib\lib-tk\Tkinter.py", line 1345 in __call__
>         return self.funcs(*args)
> TypeError: encrypt() takes exactly 1 argument (0 given)
> 
> Now is this bad programming on my part or something else? I'm running
> Python 2.3.2 on a Win98 machine if that is of any relevance.
> 
> See the attachment for the code.

Please don't put your code into an attachment next time and try to provide
the exact script you actually ran. I've made as few changes as possible:

import Tkinter
import rotor

class Tkencryptor:

    def encrypt(self):
        # with your current design, there is no need for self
        self.rt = rotor.newrotor('key', 12)
        self.e=entry.get()
        print self.rt.encrypt(self.e)
    def decrypt(self):
        pass

encryptor = Tkencryptor()

win=Tkinter.Tk()
frame = Tkinter.Frame()
#quit button
quit_button = Tkinter.Button(frame, text='Quit', command=frame.quit)
quit_button.grid(row=1, column=0)
#encrypt button
encrypt = Tkinter.Button(frame, text='Encrypt', command=encryptor.encrypt)
encrypt.grid(row=1, column=2)
#decrypt button
decrypt_button = Tkinter.Button(frame, text='Decrypt')
decrypt_button.grid(row=1, column=3, sticky=Tkinter.S)
#entry box
entry = Tkinter.Entry()
entry.pack()
#pack frame
frame.pack()

win.mainloop()

The significant change is the command parameter of the encrypt button.
There are two ways of calling a method with only a self parameter:

instance.method() # corresponds to command=instance.method

or 

Class.method(instance) # command=Class.method

As the command is called from inside Tkinter, which has no way of knowing
the Tkencryptor instance, only the first form works.


Peter

PS: Did you notice the deprecation warning?




More information about the Python-list mailing list