in reply to How to display Tk window without waiting for user input
G'day Special_K,
Unfortunately, you're completely on the wrong track with an infinite loop constantly checking for a status change. A change of status, however that is effected, is an event: you should allow Tk to respond to such an event.
I don't think popping up a window for the notification is the right way to go. You could perhaps use a Tk::Toplevel that holds the notification. That's going to be messy. If it obscures part of the main GUI, user intervention will be needed to move it or bring the main GUI back to the foreground. Then there's the question of how long the popup persists. You could use Tk::after to remove it automatically after a certain period of time: that needs to be long enough for the user to read it; but not so long that you that get a number of popups all piled on top of each other.
I would implement what you're trying to do with a status bar that notifies individual changes, some sort of window with a log of status changes as they occur, or perhaps even both. I've put together a short script to demonstrate both methods; changing the selected radiobutton simulates a status change.
#!/usr/bin/env perl use strict; use warnings; use Tk; my $mw = MainWindow::->new(-title => 'Status Notifications'); $mw->geometry('512x288+50+100'); my $status = 'A'; my $last_status = $status; my $status_msg; _set_status_msg($status, \$status_msg); my $status_bar_F = $mw->Frame( )->pack(-side => 'bottom', -fill => 'x'); $status_bar_F->Label( -textvariable => \$status_msg, -anchor => 'w', -relief => 'sunken', )->pack(-fill => 'x'); my $main_F = $mw->Frame( )->pack(-side => 'top', -fill => 'both', -expand => 1); my $text_F = $main_F->Frame( )->pack(-side => 'right', -fill => 'y'); my $status_text = $text_F->Text( -width => 50, )->pack(); $status_text->insert(end => "$status_msg\n"); my $radio_F = $main_F->Frame( )->pack(-side => 'left', -fill => 'both', -expand => 1); for my $status_letter ('A' .. 'F') { $radio_F->Radiobutton( -text => $status_letter, -variable => \$status, -value => $status_letter, -command => sub { if ($status ne $last_status) { $last_status = $status; _set_status_msg($status_letter, \$status_msg); $status_text->insert(end => "$status_msg\n"); } }, )->pack(-side => 'top', -anchor => 'w', -pady => 2); } MainLoop; sub _set_status_msg { my ($status, $msg_ref) = @_; $$msg_ref = 'Status: ' . $status . '; Changed at: ' . localtime(); return; }
That code is fully functional and should work as is. Pay particular attention to references; for example, \$status_msg, \$status, and $$msg_ref in various places.
— Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to display Tk window without waiting for user input
by Special_K (Pilgrim) on Jan 16, 2021 at 17:30 UTC | |
by haukex (Archbishop) on Jan 16, 2021 at 18:30 UTC | |
by kcott (Archbishop) on Jan 17, 2021 at 07:29 UTC |