Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

WxPerl and drag and dropping files

by james28909 (Deacon)
on Jun 03, 2014 at 07:17 UTC ( [id://1088346]=perlquestion: print w/replies, xml ) Need Help??

james28909 has asked for the wisdom of the Perl Monks concerning the following question:

I have been doing a little studying but i am stuck on somethings, Events for one (or some events). I was able to get the button events down in no time it seemed, but now, I am having trouble dragging and dropping a file onto my gui. I want it to be able to accept any file from the top window, and I am having trouble with it and have spent about 20 hours on this and still havent figured it out (tho i am a little closer i think, hopefully LOL).

Another thing i am trying to figure out is how to share a variable from a subroutine and pass it to print in the listbox?

Anyway, here is my example code

use strict; use warnings; use Wx; use Wx qw(:everything); use Wx::Event qw(EVT_BUTTON); use Wx::Event qw(EVT_DROP_FILES); use File::Slurp; use Digest::MD5; ###################################################################### +######## my $app = Wx::SimpleApp->new; my $frame = Wx::Frame->new(undef, -1, 'test tool', wxDefaultPosition, Wx::Size->new( 700, 400 ), wxDEFAULT_DIALOG_STYLE | wxSTAY_ON_TOP ); my $file = $_; my ( $x, $y ); EVT_DROP_FILES( $x, $y ); tool($frame); ###################################################################### +######### sub tool { my ($self) = @_; my $noteBook = Wx::Notebook->new( $self, -1, wxDefaultPosition, wxDefaultSize ) +; my $window1 = Wx::Panel->new( $noteBook, wxID_ANY ); my $window2 = Wx::Panel->new( $noteBook, wxID_ANY ); my $window3 = Wx::Panel->new( $noteBook, wxID_ANY ); $noteBook->AddPage( $window1, "First", 1, 0 ); $noteBook->AddPage( $window2, "Second", 0, 1 ); $noteBook->AddPage( $window3, "Third", 0, 2 ); my $button1 =Wx::Button->new( $window1, -1, "Test1", Wx::Point->ne +w( 50, 300 ),wxDefaultSize ); EVT_BUTTON( $window1, $button1, \&test1 ); my $button2 = Wx::Button->new( $window1, -1, "Test2", Wx::Point->n +ew( 175, 300 ),wxDefaultSize ); EVT_BUTTON( $window1, $button2, \&test2 ); my $lb1 = $window1->{my_listbox} = Wx::ListBox->new( $window1, wxID_ANY, wxDefaultPosition, Wx::Size->new( 300, 295 ) ); } 1; $frame->Show(1); $app->SetTopWindow($frame); $app->MainLoop;


On line 19-21 it gives error "cant call connect on undefined value at Event.pm". I looked in Event.pm ofcourse i see "connect" at line 83, I try to make sense of it, but i am still somewhat to new to really understand what he has going on in there lol. Also if i try to put "$_" in EVT_DROP_FILES(line 21) I get "to many arguments for EVT_DROP_FILES".

the main thing is i still dont fully understand events and i am sure this is easy as pie, but i am probably just over thinking it as usual.
Any help would surely be appreciated :)

Replies are listed 'Best First'.
Re: WxPerl and drag and dropping files
by Anonymous Monk on Jun 03, 2014 at 08:18 UTC

    $file $x $y are three undefined variables -- wx will complain

    This works for me on windows

    $frame->DragAcceptFiles(1); EVT_DROP_FILES( $frame, \&doit ); sub doit { warn "@_\n" }

    Why that?

    Another thing i am trying to figure out is how to share a variable from a subroutine and pass it to print in the listbox?

    What "subroutine", what variable where and for what purpose? The same principles apply to wxPerl, see write Tk callbacks all lexically scoped and not-memory leaking with no nested subs ever :) avoid nested subs and closures because nested named subs because they're closures

    You register event handlers UPON widget objects (wxWindow a wxObject), so each event handler gets as first argument that wxObject ... there is even a global Wx::wxTheApp() which you can also use as a stash (better if the frames keep track of their own data) ... more words and/or more code gets more clear answer

      i cant believe it was that easy, i was so close lol. let me read those links you posted, and thanks for pointing this out :)
      You have to forgive me. I havent got around to actually testing this until just now. Now the problem seems to be me not being able to actually associate the dropped file with a filehandle.
      I tried the following code :
      $frame->DragAcceptFiles(1); EVT_DROP_FILES( $frame, \&doit ); sub doit { #warn "@_\n" open ( our $file, '<', @_ ) or die "$!"; \&otherSub($file); }
      And ofcourse it didnt work and has sent me into a downwards spiral of searching in google and trying everything i could find lol, but i still couldnt find an example that made me have that "ah-ha" moment.
      I use the our statement so the dropped file will be available throught the script. would this be the correct concept? I understand the way i am trying to associate the dropped file with the filehandle isnt correct and any insight will be very much appreciated :)

        So, how did it not work?

        Also, what are the contents of @_ in sub doit?

        Also, what is this code supposed to do:

        \&otherSub($file);

        Maybe you just want to call othersub?

        otherSub($file);

        If you are passing $file as parameter already, there is no need to make it into a global variable. Just use my $file and pass the filehandle around as a parameter.

        ... jokes ...

        I like you, you're a funny human, and we seem to visit at same time of day :)

        See your problem is you're making stuff up and expecting the computer to know what you mean; stop that please

        finally got back around to it this evening >:D
        but still no drag and drop joy for me :(

        here is what i am trying to do:
        use diagnostics; use warnings; use Wx; use Wx::Event qw(EVT_BUTTON); use Wx::Event qw(EVT_DROP_FILES); use File::Slurp; use Digest::MD5; use File::Path; ###################################################################### +######### my $app = Wx::SimpleApp->new; my $frame = Wx::Frame->new( undef, -1, 'test tool', wxDefaultPosition, Wx::Size->new(700,400), wxDEFAULT_DIALOG_STYLE|wxSTAY_ON_TOP ); $frame->DragAcceptFiles(1); EVT_DROP_FILES( $frame, \&extract ); ###################################################################### +######### my $noteBook = Wx::Notebook->new($frame, -1, wxDefaultPosition +, wxDefaultSize); my $window1 = Wx::Panel->new($noteBook, wxID_ANY); my $window2 = Wx::Panel->new($noteBook, wxID_ANY); my $window3 = Wx::Panel->new($noteBook, wxID_ANY); $noteBook->AddPage($window1, "First", 1, 0); $noteBook->AddPage($window2, "Second", 0, 1); $noteBook->AddPage($window3, "Third", 0, 2); $frame->Show(1); $app->SetTopWindow($frame); $app->MainLoop; ###################################################################### +######### sub extract { $_= shift; print "@_\n"; #<------- prints: "Wx::DropFilesEvent=SCALAR(0x3131024) +" #please read below the code for the question pertaining + to this. open (my $data, '<', "@_") or die "cannot open $data $!"; my $fileLocation = ''; my $fileSize = ''; my $fileName = ''; my $chunk = ''; seek ( $data, 0x04, 0 ); read ( $data, my $count, 0x04 ); $count =~ s/(.)/sprintf("%02x",ord($1))/egs; my $entryCount = hex($count); print "\nEntry count in file is: $entryCount\n\n"; seek ( $data, 0x00, 0 ); seek( $data, 0x10, 0 ) or die "cannot seek $data $!"; for (my $i=1; $i <= $entryCount; $i++) { read( $data, $fileLocation, 0x08 ); read( $data, $fileSize, 0x08 ); read( $data, $fileName, 0x20 ); $fileLocation =~ s/(.)/sprintf("%02x",ord($1))/egs; $fileSize =~ s/(.)/sprintf("%02x",ord($1))/egs; $fileName =~ s/\0+$//; open( my $extractedData, '>', "extracted/$fileName" ) or die "Cann +ot open $fileName $!"; sysseek( $data, hex($fileLocation), 0 ); sysread( $data, $chunk, hex($fileSize) ); syswrite( $extractedData, $chunk ); close( $extractedData ); } my $dirname = "extracted/"; my @md5s = read_file("md5"); my $md5s = join( '', @md5s ); my $filesize = ''; open( my $buf, '<', "extracted/sdk_version" ) or die "cannot open $buf + $!"; seek( $buf, 0x00, 0 ); read( $buf, my $sdk, 0x03 ); foreach my $file (<$dirname/*>) { next if -d $file; open( my $FILE, $file ); binmode($FILE); $filesize = -s $FILE; $file =~ s{.*/}{}; my $md5 = Digest::MD5->new->addfile($FILE)->hexdigest; if ( $md5s =~ $md5 ) { print "$md5 Match! $sdk $file $filesize\n"; } else { print "WARNING !\n"; } } }
        The problem i am having is what you said in your earlier post anonomous monk, it does print what you said, but i cannot for the life of me figure out why it is not working. i get "Use of uninitialized value $data", or "trying to seek closed filehandle" and its because its not associating the dropped file with anything and thus not even opening the file.

        Ive tried to assign it a variable as you can see in open statement, but that does not work for some reason, and its because it does not have the filepath or filename of the file, instead $data has "Wx::DropFilesEvent=SCALAR(0x3131024)" inside of it and that ofcourse is not going to work and is why it is giving me these errors.

        I also have a very hard time understanding wxperl and wxwidgets in general especially when it comes to certain functions/events, tho i did get buttons figured out sort of. I have gotten very busy recently and havent really had time to delve into it like i should, but i have searched the modules inside of WX but everything in there is like ancient egyptian hieroglyphics. if you could school me at what i am doing wrong, and maybe show me an example i would greatly appreciate it. This is just a small personal project i am working on and it will help me finish certain tasks quicker if i could get this working :)

        p.s. This is just one revision, ive tried abunch of different things to no avail and always end up with the same error as no such file or directory, or use of uninitialized value.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1088346]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-23 19:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found