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

Greets. I'm trying to determine if it is possible to pop up a canvas when you press a mouse button, but do so such that the canvas has a transparent background. In other words, if the canvas has no objects on it, pressing a mouse button would have no visible effect (assuming you turned off the window decoration, too).

Why do this? I'd like to create a popup menu that used canvas objects - circles, arcs, buttons, etc. - as virtual menu buttons, such that I can set their position, color, shape, etc. as I see fit. But, I'd like the canvas background to be transparent, such that the canvas objects appear to overlay whatever was on screen before the canvas was put into place.

I'm done some digging around, and, so far as I can tell, what I'm looking for isn't possible. Can one of you learned folk (hopefully) prove me wrong about that, or perhaps suggest another way of achieving the same goal?

I thank you all in advance...

Replies are listed 'Best First'.
Re: Perl/Tk and Transparent Canvases
by graff (Chancellor) on Aug 02, 2005 at 04:29 UTC
    In case you haven't tried out the "widget" demo script that comes with the Tk modules, just run "widget" from a command line, and check out the last item in the "Canvases" section of the demo main window ("8. Tiles and transparent images"); the commentary and sample code provided there should get you started.

    You might need to use the "place" geometry manager (perldoc Tk::place), in order to get your canvas to sit on top of other widgets, or maybe the "tile" arrangement shown in the widget demo will suffice.

    Binding the mapping/unmapping of the overlay canvas (or tile) to mouse button events should be doable (Tk::bind, Tk::events), but maybe you want to think a little more about how the user is going to control things and make selections from the popup menu. (Will you need to hold the mouse button down while moving it to the object to be selected then release to select, or will consecutive clicks toggle the presence of the overlay canvas and make the selection?)

      Many thanks for the detailed response...

      I am aware of how to put transparent objects on the canvas, but what I'm looking for is a way to make the canvas itself (which, now that I think about it, means the canvas and the Mainwindow) transparent. In other words, if you open a window with a transparent background and no window decoration, you'd see... nothing. I want the graphical equivalent of a piece of plate glass, so that I can "paint" control elements on it, and have it pop up such that the control elements appear to overlay whatever is on the screen.

      Now, a Menu widget lets you pop up Menu buttons that can be made to appear near the cursor location, so maybe what I'm really looking for is a custom widget derived from the Menu widget. Hmmmm....

        Um, if you're talking about your transparent canvas (or your popup menu) being its own separate app, distinct from other apps (or the user's "desktop") that make up the things that are always visible, then you might run into some issues with input focus.

        I'm a little rusty on the details for X (and I have only limited knowledge of MS-windows), but as a rule, any user input event (mouse click or keystroke) is normally available to only one app: the one that currently holds input focus, which is typically one whose window is on top. So if your "plate glass" interface is always on top, it might be tough to allow the user to interact directly with anything underneath it. (Or perhaps that is your intention?)

        If your "always-visible" background and your "usually invisible" control widgets are all part of the same Tk app, then I believe there is a way to handle the input events such that widgets at lower layers get a chance to see them (but I've never tried that, so I'm not sure).

Re: Perl/Tk and Transparent Canvases
by bcarroll (Pilgrim) on Jul 07, 2014 at 02:05 UTC
    Have you looked at Tk::Zinc?

    TkZinc is another Canvas which proposes many new functions, some based on openGL

    Since the 3.2.2 version, TkZinc also offers as a runtime option, the support for openGL rendering, giving access to features such as antialiasing, transparency, color gradients and even a new, openGL oriented, item type triangles.

    TkZinc is available as source in tar.gz format or as Debian or RedHat/Mandrake packages at http://freshmeat.net/projects/zincisnotcanvas/

    UPDATE: TkZinc is broken if your Tk version is newer than 802.03

    Tk::Zinc object version 804.03 does not match bootstrap parameter 804. +032 at C:/Perl64/lib/DynaLoader.pm line 214. Compilation failed in require at -e line 1. BEGIN failed--compilation aborted at -e line 1.

    I was able to workaround this issue (I have Tk 804.032) by changing the following line in .../site/lib/Tk/Zinc.pm

    bootstrap Tk::Zinc $Tk::VERSION;

    to
    bootstrap Tk::Zinc '804.03';

    Now no errors are reported when I run a script that includes use Tk::Zinc;

    WARNING: I do not know if making this change will cause other issues though.