Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I know what you mean. It gets even more gruesome when you want to dynamically recompose your GUI (changing labels, changing the input widgets available according to the setting of a dropdown etc) as you have multiple objects to pass to subroutines. E.g. if you want to change the labels on 5 text fields, and remove 2 other textfields and their labels, based on a drop down setting, you have 9 separate widgets to update, and unless you start doing fiddly things like getting a list of children from the parent, you have to pass them all in).

I tend to put the Tk object itself, and widgets that I know I'm going to have to twiddle, in a hash called something like %tkObjects, and pass that to any piece of code that requires GUI update functionality. If you're coding from an OO perspective, you can make that hash an attribute of an object, and consider that the object 'has a gui'. You then end up with code that looks like:

sub update_button { my $self = shift; my $buttonobj = $self->{tkobjects}->{submitbutton}; $buttonobj->#do something with the button }

From a non OO point of view, you might consider that your subroutines are doing two things: doing the necessary processing, and updating the GUI.

That technique is on dodgy ground for the principle of narrowest scope for variables & objects, but conforms with laziness and impatience nicely :)

Alternatively (and I hate myself for saying this), you could make certain very carefully chosen objects global.

I'm certain there are better ways, but those are the two I've used.

Update: You could also put all the GUI construction and update in one place, and update the UI according to the return values of subroutine calls. That way is perhaps neater from a design point of view, but can rapidly end up with a really huge main gui function that's difficult to maintain. So thats a third, equally vile, approach that I've tried.


"If there is such a phenomenon as absolute evil, it consists in treating another human being as a thing."

John Brunner, "The Shockwave Rider".

In reply to Re: OO circularity by g0n
in thread OO circularity by wfsp

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2023-12-10 21:05 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (41 votes). Check out past polls.