Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

call a sub from Tcl code in Tcl::pTk

by Anonymous Monk
on Apr 28, 2021 at 21:14 UTC ( #11131823=perlquestion: print w/replies, xml ) Need Help??

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


I use Tcl::pTk to write Tk Guis instead of outdated Perl/Tk. The module tcl::pTk has the following interesting feature:

my $int = $mw->interp; $int->Eval("..."); _do_something{}

to write directly Tcl code in my Perl script and allow me to use some interesting Tk features not available directly through the module. Is there a way to call a Perl subrutine, in my example _do_something, from that Eval?

Replies are listed 'Best First'.
Re: call a sub from Tcl code in Tcl::pTk
by jcb (Parson) on Apr 29, 2021 at 03:49 UTC

    I would not call Perl/Tk outdated.

    The major difference between Tk and Tcl::pTk is that Tk embeds a ported version of Tk that does not require a Tcl interpreter, while Tcl::pTk embeds a Tcl interpreter which, in turn, uses a native Tk extension. It is worth noting that the Tcl-free Tk port used in Tk has also been adapted to other languages; if I recall correctly, Python's tk module also uses it.

    The Perl community is generally very much in favor of TIMTOWTDI and deriding alternatives to your chosen solution is unlikely to earn you any help.

Re: call a sub from Tcl code in Tcl::pTk
by LanX (Cardinal) on Apr 29, 2021 at 11:01 UTC
    Could you explain why you want this?

    The way to call Perl from Tk is to trigger callbacks, like pressed buttons.

    Tcl is used to create the Tk GUI and can also create triggers.

    So there are ways, but this smells like a XY Problem

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re: call a sub from Tcl code in Tcl::pTk
by Marshall (Canon) on Apr 29, 2021 at 04:46 UTC
    I don't understand your question.
    What is it that you think that Tk doesn't do, that you want to do?
    Use as much verbiage as you want to explain some specific need.
Re: call a sub from Tcl code in Tcl::pTk
by Anonymous Monk on Apr 29, 2021 at 13:28 UTC

    Very sorry if I caused any trouble, maybe I missed to specify "outdated from a Tk, especially macOS-related, perspective".

    We work on an app using Tk on macOS, and Perl/Tk is - in our terms - outdated. It runs with 'hacks' (needs XQuartz) and the UI it produces is non-Acqua compatible, looks like old-fashioned UI and this makes Perl/Tk, in our modest opinion, a no-go in the macOS world (okay, you can define it not as outdated, happy to learn another definition that better suits this group). Tcl::pTk gives instead access to the modern Tcl/Tk framework which is actively maintained and in line with UI conventions and styles on macOS. No more than this.

    My question is pretty straightforward: from a portion of code written diretcly in Tcl inside $int->Eval("...") we want to call a subrutine which lives in the normal Perl code. A typical case would be to manipulate macOS typical menu entries such as Preferences, that can not be done - as far as we know directly from Perl i.e. Tcl::pTk:

    my $int = $mw->interp; my $menuPATH = $int->widget($menubar);#getting Tk name $int->Eval(" $menuPATH add cascade -menu [menu $] proc tk::mac::ShowPreferences {} {#CALL SUBRUTINE IN PERL +CODE THAT CREATES THE COMPLEX MENU WINDOW TO SET APP PREFERENCES"} ");

    Does it make more sense now

    PS: Python standard UI framework is Tkinter which uses the same principle of Tcl::pTk, i.e. it links to the original Tcl/Tk framework.

    Thank you

      Your question reveals an inside-out approach.

      The ->Eval method is meant for exceptional stuff which can't be done with normal Perl methods.

      The normal approach is to use standard pTk->methods to build menus and windows.

      If your special wish is even possible I doubt that you'll get a quick answer here.


      Anyway ... if you just want to generate Tcl code from a Perl method inside your template you can do something like

      ->Eval(<<"__TCL__"); ... some Tcl Code @{[ my_perl_sub() ]} ... more Tcl Code __TCL__

      the result from my_perl_sub() is inserted as string then, which will be interpreted as Tcl code.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        Thank you LanX for your answer. I agree that ->Eval is to be used for exceptional cases where pTk->methods are not available. The case presented above is (should be) such a cases, since there are no Perl or pTk methods to manipulate the macOS menu. Such methods are instead available in the Tcl/Tk.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11131823]
Approved by GrandFather
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2021-05-11 05:23 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (113 votes). Check out past polls.