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

how to use eventGenerate in Tk

by tobbes (Acolyte)
on Oct 31, 2018 at 11:17 UTC ( [id://1224977]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to use Perl's Tk module for guis. From what I understand it should be possible to generate events programatically which will then will trigger any listeners on a widget. So i did the following:

use Tk; my $mw = new MainWindow; my $label = $mw -> Label(-text=>"Hello World") -> pack(); my $button = $mw -> Button(-text => "Quit"); $button->bind('<ButtonPress-1>' => \&myexit); $button->pack; MainLoop; $button->eventGenerate('<ButtonPress-1>'); sub myexit { exit; }

Here I create a window with a label and a button and whenever the button is clicked the program should exit. It works perfectly when actually clicking the button. When trying to generate a click, nothing happens. I have also read about virtual events, are these a special kind of events, so that I can generate only events of that type? What I wonder is whether I am unable to generate builtin events like keypress and such. Any help would be very welcome.

Hmm, I just realized that maybe MainLoop is halting the entire program, thus eventGenerate is never reached. But in that case eventGenerate seems rather pointless.

Replies are listed 'Best First'.
Re: how to use eventGenerate in Tk
by choroba (Cardinal) on Oct 31, 2018 at 12:19 UTC
    Yes, you MainLoop blocks the program, so putting Tk-related stuff below it makes no sense. You can for example schedule an event to happen one second after the application was started:
    $mw->after(1000 => sub { $button->eventGenerate('<ButtonPress-1>') }); MainLoop();

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      Thank you, so if I wish to draw a dynamic graph, then firing an event to make the graph redraw is not an option.
        Why, of course it is. You just need to fire it from an "after" or "repeat", or even a "fileevent" (when new data arrive).

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: how to use eventGenerate in Tk
by zentara (Archbishop) on Oct 31, 2018 at 12:29 UTC
    Hi, first off, you have a glaring error in your program flow. Once the line MainLoop is encountered, no further lines are processed until the MainLoop ( the Tk event loop system) is destroyed. So you never call eventGenerate until the Tk eventloop is gone, so it probably tries to run but throws an error.
    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = new MainWindow; my $label = $mw -> Label(-text=>"Hello World") -> pack(); my $button = $mw -> Button(-text => "Quit"); $button->bind('<ButtonPress-1>' => \&myexit); $button->pack; $button->eventGenerate('<ButtonPress-1>'); $mw->repeat(1000 => \&doit); sub doit { $button->focus; $button->eventGenerate('<ButtonPress-1>'); } MainLoop; sub myexit { print time."\n"; #exit; }

    I'm not really a human, but I play one on earth. ..... an animated JAPH

Log In?
Username:
Password:

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

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

    No recent polls found