Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Monitor dimensions

by biochris (Beadle)
on Sep 02, 2005 at 18:50 UTC ( #488738=perlquestion: print w/replies, xml ) Need Help??

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

Dear monks, I am writing a Perl/Tk script (or at least I am trying to), and I would like the dimensions of the main window to be in reference to the dimensions of the monitor. Is there any function (of MainWindow probably) that would give me the width and height of my monitor? Thanks very much.

Replies are listed 'Best First'.
Re: Monitor dimensions
by chester (Hermit) on Sep 02, 2005 at 19:19 UTC
    use strict; use warnings; use Tk; my $main = MainWindow->new(); my ($width, $height) = ($main->screenwidth(), $main->screenheight()); print "Width: $width\nHeight: $height";
      Thank you so much. You have given me my hope back for Perl/Tk.
Re: Monitor dimensions
by halley (Prior) on Sep 02, 2005 at 19:36 UTC
    Be careful with terminology: I read your request (the dimensions of the monitor) as a request for physical dimensions, like a 19" flatscreen. I expect you really want pixel area, like 1280x1024 pixels.

    Also be careful with assumptions: not all setups use just one monitor, and the combined surface area is not always a simple rectangle. Thus the best window dimensions for a given application is not at all consistent or obvious. Add to that the concerns for various trays and other edge-warts, and you may not even be able to accurately request the usable desktop region in one shot.

    --
    [ e d @ h a l l e y . c c ]

      Any suggestions? I will sure take your advice :)

        Make your application window(s) big enough to do the job, and no bigger. That's my advice, and I'm sticking to it.

        --
        [ e d @ h a l l e y . c c ]

Re: Monitor dimensions
by bmann (Priest) on Sep 02, 2005 at 19:28 UTC

    The aptly named screenwidth and screenheight methods of the MainWindow will give you that info. They are documented in Tk::Wm. While the documentation for Tk is good, it takes some searching to find relevant info. I own a copy of "Mastering Perl/Tk", and it's a good reference to have on hand.

    Here's a quick example:

    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new; $mw->Label( -text => $mw->screenheight . " x ". $mw->screenwidth )->p +ack; MainLoop;
    On this computer, it gives me "1280 x 1024"
      Thank you very much. I tried using just Internet resources, but it looks like I have to get the resource book ($$). I really appreciate your response though :)
        To be honest, I didn't use the book this time. I had a dim recollection of "screenheight" so I grepped the Tk directory and found Wm.pm and Wm.pod.

        If you're looking at buying the book, I got mine with the Perl CD Bookshelf v3 - includes paper Perl in a nutshell and digital "Programming Perl" (3rd edition), "Perl Cookbook", "Mastering Perl/Tk", "Perl and LWP", and "Perl and XML", all for about the price of two books. I'd check ebay.

        The fourth version is available now, but it drops the LWP, Tk and XML books in favor of merlyn's latest book and "Mastering Regular Expressions". It also has a newer version of the cookbook.

        Good luck!

        Update: A quick search on ebay shows several copies for under $20 US, including shipping.

Re: Monitor dimensions
by aplonis (Pilgrim) on Sep 03, 2005 at 23:03 UTC

    I use the below for a Pane widget which is to hold a Label widget which is to hold a multi-line string for display in a pop-up window.

    # Calculate dimensions of message for pane, etc. Then # configure widget to that height and width. sub size_widget_for_string { my ($wgt, $msg, $font) = @_; # Measure widest line of string for given font. my @msg = split "\n", $msg; my $hght = 1.5 * $wgt->fontMetrics($font, -linespace); my $wdth = 0; foreach (@msg) { my $line_wdth = $wgt->fontMeasure($font, " $_ "); $wdth = $line_wdth if $wdth < $line_wdth; $hght += $wgt->fontMetrics($font, -linespace); } # Limit size to reasonable maximums. my $max_hght = 0.6 * $wgt->screenheight; my $max_wdth = 0.9 * $wgt->screenwidth; $hght = $max_hght if $hght > $max_hght; $wdth = $max_wdth if $wdth > $max_wdth; # Configure the widget. $wgt->configure( -width => $wdth, -height => $hght ); }

    Synopsis:

    size_widget_for_string($pane, $any_old_string, 'courier');
      The pane widget was exactly what I was looking for! How did you know that? Thank you very much :) I am a novice perl monk.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://488738]
Approved by bmann
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2021-12-01 01:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?