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.

  • The taking of text from the top textbox sending it to the encrypting sub and sending it back.
  • Creating the popup window with a textbox inside.
  • Taking a value from the textbox.

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); }


I'm very thankful of any and all help.

All the Best, Eoin...

If everything seems to be going well, you obviously don't know what the hell is going on.


In reply to popup values in Tk by eoin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.