Creating Multiple MainWindows

Occasionally, you may need to create multiple MainWindows in the same application. Typically, having two MainWindows in one application isn’t always a wise idea, since bitmaps and images aren’t shared between MainWindows, and some Tk modules are unreliable when shared. But we’re going to explore it anyway, because it’s an interesting exercise. To show how it’s done, we’ll start with an advanced “Hello Worlds” program, so named because it uses two MainWindows.

You might be wondering what useful programs might exist that take advantage of more than one screen. Nonlinear digital video editing software is a great example. A fancy high-resolution screen displays the movie in progress, and secondary screens contain editing controls. Or, any application that requires lots of screen real estate can spread its windows across multiple screens. We won’t be doing any of that fancy stuff, as you’ll see.

Our little program begins typically enough, importing all the required Tk symbols, subroutines, variables, classes, widgets, and methods, then opening the first MainWindow, $mw1, in the normal fashion.[4]

#!/usr/local/bin/perl -w
#
# Advanced Hello World program using two MainWindows.

use Tk;
use subs qw/beep/;
use strict;

my $mw1 = MainWindow->new;

By default, MainWindow opens its window on the display pointed to by $ENV{DISPLAY}, which in Unix is normally :0 (assuming you haven’t pointed it elsewhere). Technically, a display specification consists of three fields: ...

Get Mastering Perl/Tk now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.