Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Wx with Tk

by Khariton (Sexton)
on Nov 08, 2010 at 13:37 UTC ( #870108=perlquestion: print w/replies, xml ) Need Help??
Khariton has asked for the wisdom of the Perl Monks concerning the following question:

I use as base GUI Tk.
But for file dialog I use Wx. It can work without parent structure (I saw only warning about null variable).
May be I can using only Wx, but I use Canvas from Tk and this thing is major for my program. I find no canvas in Wx(may be I search bad). And I need compatability with Strawberry Perl on Windows and GNU Perl on Linux. Now it works fine.
How I can use in one program menus, dialogs and other GUI things from Wx and only Canvas from Tk. I understand: this two interfaces are different but may be present method for resolve this problem? This is fantastic idea but faint hope present in my head.

Replies are listed 'Best First'.
Re: Wx with Tk
by zentara (Archbishop) on Nov 08, 2010 at 16:16 UTC
    Yes you can mix 2 or more eventloop systems by making one loop the master controlling loop, and all others, as slaves. The slaves get pumped by a timer call in your master loop, which tells each looping system, to do one loop. You will need to call this timer often, to make all the widgets seem responsive.

    I don't have Wx going, but here is an example mixing Gtk2 and Tk

    #!/usr/bin/perl -w use strict; use Gtk2; use Tk; #setup Tk loop my $mw = MainWindow->new(-title=>'Tk Window'); my $count_tk = 0; my $labtk = $mw->Label(-textvariable =>\$count_tk)->pack; #setup Gtk2 loop Gtk2->init; my $count_gtk = 0; my $window = Gtk2::Window->new('toplevel'); $window->set_title('Gtk2 Window'); my $glabel = Gtk2::Label->new("This is a Gtk2 Label $count_gtk"); $window->add($glabel); $window->show_all; # make Tk loop the master, but you could make Gtk2 master if desired # the lower the repeat rate, i.e. 1 ms, # will give more cpu time to the gtk2 loop # this is sometimes called manually pumping the event loop my $tktimer = $mw->repeat(10, sub{ $count_gtk++; $glabel->set_text("This is a Gtk2 Label $count_gtk"); Gtk2->main_iteration while Gtk2->events_pending; $count_tk++; }); $mw->Button(-text=>' Tk control Quit ', -command => sub{exit} )->pack(); MainLoop; #######################################
    The same principle should work for you with Tk and Wx. Just find the Wx command to execute 1 loop, and manually call it from Tk.... or vice/versa

    But in all honesty, you should be able to find a set of widgets, all from the same toolkit, so you wouldn't need this hackery.

    Why can't you use the Canvas from Wx?

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      I can't answer for long times...
      You spent some time to me and big thanks for you.
      This is interesting example. It must help me for level up integration of Tk and Wx. But I need one window with Wx GUI and Tk-like canvas functions with drag-n-drop objects.
      I see Wx has GL-canvas but I needn't GL. How it works with videocards without 3d acceleration? It's slowly with software 3D emultion? I need only moving 2x object on canvas (pictures, dashed lines...).
      You can see my software page to understand what my program doing and my wishes to this project.
      p.s.: Also I can't find good full manuals about Wx on Perl...
      I find many problems with good documentated Tk on Linux and many new problems when I port it to Windows. With this new problems on Wx I can stop converting my soft to pure Wx for long long time with curved road of probes and mistakes. And may be this times will turn left...
      p.p.s.:Sorry for my skill of English. I learn it and can't speak fluently...
        Interpreting your English, was a good lesson for me, in how to convey information. I thought "this must be an intelligent person", to get that message across without knowing English. :-)

        Anyways, to your problem. I am almost sure there is a plain WxCanvas, the GLCanvas is probably an extra type. Google for "Wx canvas".

        So, why not just use the WxCanvas, instead of a Tk::Canvas? I think you are mistaken to believe that Wx dosn't have a 2d canvas widget. So google for "WxPerl canvas", and you can be pure Wx. The only reason to keep Tk is if you have some complex Tk::Canvas code that you want to use, but don't quite want to rewrite it in Wx. ;-)

        But if you want to go that mixed route, just setup a timer in Wx

        use Tk qw/MainLoop tkinit DoOneEvent exit DONT_WAIT ALL_EVENTS/; #.... setup your Tk code without a MainLoop statement #then in your Wx timer, put $mw->DoOneEvent( DONT_WAIT | ALL_EVENTS ); # to make Tk responsive

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh
Re: Wx with Tk
by Anonymous Monk on Nov 08, 2010 at 15:50 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://870108]
Approved by McDarren
[marto]: good morning all

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2018-05-21 07:41 GMT
Find Nodes?
    Voting Booth?