in reply to Basic questions of Tk programming

When you use Tk; the library sets up an environment which is able to wait for events. It does not start the event loop until your program hands over control, which you are supposed to do with MainLoop;

Calling sleep has the same effect: It hands over control. Tk paints the screen and ... waits. It does not get any events, though. Instead, your Initialize routine wakes up and continues, happy to use the Tk screen. The program use the Tk screen drawing capability, but not the Tk event loop.

This is an infinite loop: The call to MainLoop; never happens. You'll notice that there is an error message when you close the window:

Usage $widget->update(...)
Tk::Label=HASH(0x5f5632326bd8) is not a Tk object at jmclifford.pl line 56.

Replies are listed 'Best First'.
Re^2: Basic questions of Tk programming
by Discipulus (Canon) on Jan 24, 2025 at 09:48 UTC
    Hello haj and bravo,

    when I read your reply it made sense. I was surprised to read:

    > Calling sleep has the same effect: It hands over control. Tk paints the screen and ... waits. It does not get any events, though. Instead, your Initialize routine wakes up and continues, happy to use the Tk screen. The program use the Tk screen drawing capability, but not the Tk event loop. So should be pack geometry manager to draw the screen.This can be reduced to:

    ## BAD CODE !! use strict; use warnings; use Tk; my $number = 1; my $mw = MainWindow->new; my $label = $mw->Label(-textvariable =>\$number)->pack(); # sleep 5; MainLoop; while (1){ $number++; $label->update; sleep 1;}

    ..without the never called MainLoop

    By other hand, uncommenting the sleep 5; MainLoop; line pack does not draw the screen, so the screen drawing capability cannot be proved by me.

    For sure is buggy code, but can be useful to inspect the internal machineries of Tk

    The error: Tk::Label=HASH(0x39a2828) is not a Tk object at textlabel_reduced.pl line 11. only shows closing the the program with the X but no sending CTRL-C in the console.

    A final note: perlmonks is the last place on earth (<- thanks to English native ;) last remaining place on earth to get support on perl Tk :)

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      It's not the sleep that draws the screen, it's the ->update!

      "A final note: perlmonks is the last place on earth to get support on perl Tk :)"
      This makes my sad. I think I have been quite helpful.

        Sorry tybalt89! marto is right below.. I intended last remaining place to get support on perl Tk.

        Also and without adulation, after zentara retired you are the best Tk guru here around.

        About the update ..yes for sure, but I supposed pack not sleep

        ..sorry for the miswriting!

        L*

        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      "A final note: perlmonks is the last place on earth to get support on perl Tk :)"

      Perhaps this is lost in translation, while not a Tk user myself I do see Tk threads reasonably often with much help & advice given. For clarity, by the statement above do you mean that once someone posts here they don't go anywhere else for Tk help?