eoin has asked for the wisdom of the Perl Monks concerning the following question:
Hey monks i've recently been working with Tk and Crypt in a GUI for an ecryption application. I've got a few problems as I'm not very familiar with Tk. I've done my best with this first draft of the code but I can't really get my head around how I'm going to get the encryptyion key people insert into the textbox in the popup window. Also I don't think that the "DialogBox" method is the best way to obain a popup window and if it is I'm doing it wrong.
So far I've proofed the encryption code and the main window code but its the in between that I'm having problems with.
I'd really apprieciate any help you can give. The code is inside.
use strict; use warnings; use Tk; use Crypt::CBC; use Crypt::DES; my($key); my $mw = new MainWindow; $mw->geometry('800x600'); my $menu = $mw->Menu(-menuitems => &menubar_menuitems() ); $mw->configure(-menu => $menu); my $tpfrme = $mw->Frame; my $mdfrme = $mw->Frame(-height => '60'); my $bmfrme = $mw->Frame; my($text1) = $tpfrme->Scrolled('TextUndo', -height => '1', -width => '1', -scrollbars => 'osoe', ); my $encrypt_button = $mdfrme->Button(-text=>"Encrypt", -height=>'1', -command=>[\&OnEncrypt] ); my $get_key_button = $mdfrme->Button(-text=>"Set Encryption Key", -height=>'1', -command=>[\&get_key] ); my $decrypt_button = $mdfrme->Button(-text=>"Decrypt", -height=>'1', -command=>[\&OnDecrypt] ); my($text2) = $bmfrme->Scrolled('TextUndo', -height => '1', -width => '1', -scrollbars => 'osoe', ); $mw->bind('Tk::TextUndo', '<Control-Key-o>', sub { OnFileLoad(); } ); $mw->bind('Tk::TextUndo', '<Control-Key-s>', sub { OnFileSave(); } ); $mw->bind('<Control-Key-q>', sub { OnExit(); } ); $mw->bind('Tk::TextUndo', '<Control-Key-s>', sub { OnEncrypt(); } ); $mw->bind('Tk::TextUndo', '<Control-Key-s>', sub { OnDecrypt(); } ); $mw->bind('<Control-Key-a>', sub { OnAbout(); } ); $tpfrme ->pack(qw/-side top -fill both -expand 1 + /); $mdfrme ->pack(qw/-side top -fill both -expand 0 + /); $bmfrme ->pack(qw/-side bottom -fill both -expand 1 + /); $text1 ->pack(qw/-side top -fill both -expand 1 + /); $encrypt_button->pack(qw/-side left + /); $get_key_button->pack(qw/ -anchor cen +ter/); $decrypt_button->pack(qw/-side right + /); $text2 ->pack(qw/-side top -fill both -expand 1 + /); MainLoop; sub menubar_menuitems{ return [ map ['cascade', $_->[0], -tearoff=> 0, -menuitems => $_->[1]], ['~File', &file_menuitems() ], ['~Actions', &actions_menuitems()], ['~Help', &help_menuitems() ], ]; } sub file_menuitems { return [ [qw/command ~Open -accelerator Ctrl-o/, -command=>[\&OnFileLoad]], [qw/command ~Save -accelerator Ctrl-s/, -command=>[\&OnFileSave]], '', [qw/command ~Exit -accelerator Ctrl-q/, -command=>[\&OnExit]] ]; } sub actions_menuitems { return [ [qw/command ~Encrypt -accelerator Ctrl-e/, -command=>[\&OnEncrypt]], [qw/command ~Decrypt -accelerator Ctrl-d/, -command=>[\&OnDecrypt]], ]; } sub help_menuitems { return [ [qw/command ~About -accelerator Ctrl-a/, -command=>[\&OnAbout] ] ]; } sub OnFileLoad { $text1->FileLoadPopup(); } sub OnFileSave { $text2->FileSaveAsPopup(); } sub OnExit { exit 0; } sub OnEncrypt { my $text = $text1->get(0.1, 'end') ; my $encrypted_data = encrypt_string($text) ; my $widget = $text2->Subwidget("text") ; tie *STDOUT, ref $widget, $widget; print $encrypted_data; } sub OnDecrypt { my $text = $text1->get(0.1, 'end') ; my $decrypted_data = decrypt_string($text) ; my $widget = $text2->Subwidget("text") ; tie *STDOUT, ref $widget, $widget; print $decrypted_data; } sub encrypt_string { my $cipher = new_cipher() ; my $string = shift ; my $encryp_data = $cipher->encrypt($string); $cipher = "" ; return $encryp_data ; } sub decrypt_string { my $cipher = new_cipher() ; my $string = shift ; my $decryp_data = $cipher->decrypt($string); $cipher = "" ; return $decryp_data ; } sub new_cipher { my $key = get_key(); my $cipher = Crypt::CBC->new( {'key' => "$key", 'cipher' => 'DES', 'iv' => '$KJh#(}q', 'regenerate_key' => 0, # default true 'padding' => 'space', 'prepend_iv' => 0 }); return $cipher; } sub get_key { my $getkeybox = $mw->DialogBox(-title=>"Set Encryption Key", -buttons=>["OK"] ); $getkeybox->add('Label', -anchor => 'w', -justify => 'left', -text => qq(Please enter your Encryption Key))->pa +ck(qw/-side top -anchor w/); my $keybox = $getkeybox->add('Scrolled', 'TextUndo', -height => '10' )->pack(qw/-side top -fill both -expand +1 -anchor w/); my $result = $getkeybox->show(); $key = $keybox->get(0.1, 'end') unless(undef $result); }
If everything seems to be going well, you obviously don't know what the hell is going on.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: popup values in Tk
by pg (Canon) on Oct 19, 2003 at 16:40 UTC | |
by eoin (Monk) on Oct 20, 2003 at 17:36 UTC | |
by eoin (Monk) on Oct 21, 2003 at 20:22 UTC | |
|
Re: popup values in Tk
by ptkdb (Monk) on Oct 19, 2003 at 18:59 UTC | |
|
Re: popup values in Tk
by PodMaster (Abbot) on Oct 19, 2003 at 16:23 UTC |